Installation
Make sure you have Git installed bash sudo apt install git
Verify with bash git --version
Create a Repository
-
Create a new repo
git init --initial-branch=main
-
Write a simple “Hello World” program
#include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; }
-
Compile it
g++ hello.cpp
-
Make sure it runs
./a.out
Then see what has changed in the repo with this command
git status
-
Now stage the source code
git add hello.cpp
-
Commit the changes
# -m adds a commit message, make sure it is informative! git commit -m "Initial Commit"
-
Create a git ignore file
echo "a.out" > .gitignore
-
To view the history of the repo
git log
Shows who made what changes and when
-
Push the repo to remote repository
git remote set-url origin sample_URL.com && git push
-
Get the latest updates from the remote repository, but do not modify head
git fetch
-
Get the latest updates from the remote repository
git pull
Cloning Repositories
Say you need to collaborate with another developer. In this case you will clone their repo onto your system so that you can make your changes. And you will do this on a separate branch
-
Clone the Repo
git clone collaborator_URL.com # Create a new Branch for your changes git checkout -b Your_Branch
-
Make your changes and check what files you modified
git status
-
Check if your files have been tracked and then Commit and Push
git add <files you created> or git add . #check your working area to make sure the files are tracked git status #push all your changes git commit -a -m "Created files X,Y,Z and changed A,B,C" # You would ideally be working on your own branch: Denoted as Your_Branch git push origin Your_Branch
-
Validate your changes with
git log