Various git commands at one place that we use regularly in our working life.
git status # new updates in repo
git add <file-name> # add new files to commit
git commit -m 'your-message'
git push # push to remote
git pull # pull from remote
git log # commits log
git reflog # tracks local Git refs along with remote
git clone <url>
git checkout -b <branch>
git branch -d <branch_name> # delete local branch
git branch -D <branch_name> # forced delete local branch
git reset --hard HEAD~1 # removes your top commit if your head points there
git reset --soft HEAD~1 # in case if you want to get your work back
git remote remove origin # remove remote origin
Clone submodules and a branch at a given depth (depth=1, clone's a large branch quickly with latest code):
git clone <url> --recurse-submodules --depth=<depth> -b <branch>
Pull a large repo quickly if cloned with depth=1. If new commits are merge-commits try pull with a larger depth may be 10 :
git pull --depth=10
To retrieve all:
git pull --unshallow
Know your repo's remote info:
git remote -v # repo's remote url
git branch -va # repo's available remote branches
Update repo's remote url. In case your repo is moved to some other url:
git remote set-url origin <remote-repo-url-here>
Store git credentials permanently:
git config --global credential.helper store
Cache git credentials:
git config --global credential.helper "cache --timeout=<time in seconds>"
Basic submodule commands
git submodule add <url> # add a submodule in repo
git submodule init # initialize submodules
git submodule update # updates submodules
git submodule update --init # initialize and update submdoules
git submodule update --init --recursive # to intialize, fetch and checkout nested submodules
git submodule update --remote --merge # merge remote changes locally
git submodule update --remote --rebase # reabase local with remote changes
git submodule foreach git pull origin <branch> # pull updates from all submodules
Push submodule changes
git submodule foreach git push -u origin <branch>
# Or add submodule path manually
git add <submodule-changes>
git commit -m ''
git push
Update submodule's remote url. In case moved to some other url:
# new url local submodule is pointing to:
git config --file=.gitmodules submodule.<submodule-path-in-file-here>.url <url>
# new branch local submodule is pointing to:
git config --file=.gitmodules submodule.<submodule-path-in-file-here>.branch <branch>
# After updating, run following to update submodules:
git submodule sync
git submodule update --init --recursive --remote
Clone your fork and do the following as required:
git remote -v # shows your fork remote
To synchronize your fork with main repo:
git add upstream <url> # your main repo url, now above command will show both remote urls
git fetch upstream # fetches all the branches
git fetch upstream <branch> --depth=<depth> # to fetch specific branch at the given depth
# In case if u fail to fetch run this and then fetch again:
git config --local --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git branch -va # to see remote and local branches
# Checkout a fork-branch and run merge commands mentioned below in this section to sync
# your branch
git checkout <branch>
To create a new local branch from upstream dev because fork and original repo could have same names, if not done this way git will create a temporary branch itself with a detached head. Dev branch name is an example:
git checkout -b upstream-dev upstream/dev # creates a local branch upstream-dev
# And to push from a local branch to upstream, add commit and push by:
git push -u upstream <branch>
In order to merge an upstream branch with fork branch:
git fetch upstream master # upstream branch
git checkout dev # fork branch
git merge master # will merge master in dev
PRs are welcome from the community