Git Handbook

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

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
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

# 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
# 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>



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Danuka Praneeth

Danuka Praneeth

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