Git Handbook

Danuka Praneeth
4 min readMay 7, 2021


This is collection of most frequently used git commits with explanations to some of the git concepts.

Basic git commands

#When working with your own repo
git clone {repo url} //get remote repo
git add . //add changes to local
git commit -m "Commit message" //add commit
git push origin master //git push to remote master
git push origin branch //git push to remote branch
git pull origin master //git pull from remote master
git pull origin branch //git pull from remote branch
#When working with a copy of original (first folk the original repo)
git remote -v //show URLs of remote repositories [your remote copy as origin and original remote repo as upstream]
git remote add upstream {url} //add the upstream URL#Create new branch
git branch -a //list all the branches
git branch //list the branches
git branch -b branch-name //create new branch from current branch, and switch the current branch to new branch
git branch branch-name //create new branch from current branch
git checkout branch-name //change the current working branch
#To push the local branch to remote
git push origin branch-name //push the branch to remote
#Merge branch A to branch B
git checkout branch B
git merge branch A
#Delete branches
git branch -d branch-name //delete branch from local
git push origin :branch-name //delete branch from remote
git remote add upstream {url of upstream repo}

Initialize Git repo

# Standard way to initialize with a working directory
mkdir repo
cd repo
git init
# Bare way to initialize without working directory.
mkdir repo
cd repo
git init --bare

Bare repositories are usually central repositories where everyone moves their work to. Bare repositories store git revision history in the root folder of your repository instead of .git subfolder.

Working with commits

# When you need to checkout to an old commit
git checkout HEAD~3 //go back to 3 commits from current
git checkout commit-id //go back to required commit id
# Revert the last commit
git revert <commit id>

Fetch vs Pull

Fetch: Only downloads new data from a remote repository. But it doesn’t integrate any of this new data into your working files. This is useful when you need to compare the new commits added to the remote branch with your local repo.

Pull: Download the repo changes integrate with local repo. So it update your current HEAD branch with the latest changes from the remote server.

# Fetch
git fetch origin
git fetch --all
git diff ..origin # Compare the fetch with local
git diff ..origin/master # Compare the fetch (specific branch)
# Pull
git pull origin master

Merge Vs Rebase

Merge: Incorporate new commits into your feature branch. This will create an extra merge commit every time you need to incorporate changes. So this will pollute the branch history.

# When you work in the dev branch and need to get the commits from new-feature branch
git merge new-features
# To handle those changes, you can either create new branchgit checkout -b new-branch-name
git add .
git commit -m "<your commit message>"
# or stash them
git stash # add them to the stash
git merge new-features # do your merge
git stash pop # get changes back into your working tree

Rebase: Move the entirety of a branch to another point in the tree by re-writing the project history by creating brand new commits for each commit in the original branch. The simplest example is moving a branch further up in the tree.

# Before rebase
/O-----O---O-O-----O--------- branch
--o-o--A--o---o---o---o----o--o-o-o--- master
git checkout branch
git rebase master
# After rebase
/o-----o---o--o-----o------ branch
--o-o--A--o---o----o--o-o master

Save the changes temporarily

git stash                     //save existing file updates
git stash --all //save all including untracked files
git stash list
git stash pop //recover the stashed changes
git stash pop stash@{0} //recover the specific stashed changes

Working with git tags

# When tags created from console
git fetch --all //pull remote branches with tags
git checkout tags/tag-1 //checkout to required tag
git checkout master //go back to latest commit
# Create tags locally and push to remote
git tag //list git tags in local
git tag tag-2 //create new tag
git push origin tag-2 //push the tag
git push origin --tags //push all tags
git tag tag-3 commit-id //create tag from commit id
git tag -a tag-4 commit-id-m "message" //create an annotated tag for a commit id
# Delete tags
git tag -d tag-1 //delete local tag
git push origin :refs/tags/tag-1 //delete remote tag

Other Useful Commands

# List the files affected by a particular commit
git diff-tree -r <commit-id>



Danuka Praneeth

Senior Software Engineer | BSc (Hons) Engineering | CIMA | Autodidact | Knowledge-Seeker