Most useful git commands
Lifecycle of git
The following figure shows the distinct areas of git. Knowing this concept is essential in order to understand git.

Figure 1: The lifecycle of the status of your files
This is an overview of the most common git commands. I strongly recommend knowing its basic and using it to anybody writing code, whether alone or particularly within a team. To get a quick theoretical introduction into the topics please have a look here: Getting Started - Git Basics
Setup
Set your details
git config --global user.name "John Doe"
git config --global user.email "john@example.com"Use --global to set the configuration for all projects. If git config is used without --global and run inside a project directory, the settings are set for the specific project.
One can configure Git to automatically remove references to deleted remote branches when fetching:
git config --global fetch.prune trueMake git ignore file modes
cd project/
git config core.filemode falseThis option is useful if the file permissions are not important to us, for example when we are on Windows.
See your settings
git config --listGet help for a specific git command
git help cloneStarting a repo
Initialize a git repository for existing code
cd existing-project/
git initClone a remote repository
git clone https://github.com/user/repository.gitThis creates a new directory with the name of the repository.
Clone a remote repository in the current directory
git clone https://github.com/user/repository.git .Remotes
Update and merge your current branch with a remote
cd repository/
git pull origin masterWhere origin is the remote repository, and master the remote branch.
If you don’t want to merge your changes, use git fetch
Update a forked repo
git fetch upstream
git checkout master
git merge upstream/mastersource: https://help.github.com/en/articles/syncing-a-fork
# 1. Clone your fork:
git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git
# 2. Add remote from original repository in your forked repository:
cd into/cloned/fork-repo
git remote add upstream git://github.com/harlecin/hugo-site.git
git fetch upstream
# 3. Updating your fork from original repo to keep up with their changes:
git pull upstream master View remote urls
git remote -vChange origin url
git remote set-url origin http//github.com/repo.gitAdd remote
git remote add remote-name https://github.com/user/repo.gitmerge master and dev**
I generally like to merge master into the development first so that if there are any conflicts, I can resolve in the development branch itself and my master remains clean.
(on branch development)$ git merge master
(resolve any merge conflicts if there are any)
git checkout master
git merge development (there won't be any conflicts now)Branching
Create a branch
git checkout master
git branch new-branch-nameHere master is the starting point for the new branch. Note that with these two commands we don’t move to the new branch, as we are still in master and we would need to run git checkout new-branch-name.
The same can be achieved using one single command:
git checkout -b new-branch-nameCreate a branch from a previous commit
git branch branchname sha1-of-commit
# or using a symbolic reference (e.g. last commit):
git branch branchname HEAD~1
# or
git checkout -b branchname sha1-of-commitSource: http://stackoverflow.com/a/2816728/1391963
Checkout a branch
git checkout new-branch-nameSee commit history for just the current branch
git cherry -v master(master is the branch you want to compare)
Merge branch commits
git checkout master
git merge branch-nameHere we are merging all commits of branch-name to master.
Merge a branch without committing
git merge branch-name --no-commit --no-ffSee differences between the current state and a branch
git diff branch-nameSee differences in a file, between the current state and a branch
git diff branch-name path/to/fileDelete a branch
git branch -d new-branch-namePush the new branch
git push origin new-branch-nameGet all branches
git fetch originGet the git root directory
git rev-parse --show-toplevelSource: http://stackoverflow.com/q/957928/1391963
Remove from repository all locally deleted files
git rm $(`git ls-files --deleted`)Source: http://stackoverflow.com/a/5147119/1391963
Delete all untracked files
git clean -fIncluding directories:
git clean -f -dPreventing sudden cardiac arrest:
git clean -n -f -dSource: http://stackoverflow.com/q/61212/1391963
Delete all files from a git repository that have already been deleted from disk:
git ls-files --deleted -z | xargs -0 git rmSource (and alternatives): https://stackoverflow.com/a/5147119/1391963
Show total file size difference between two commits
Short answer: Git does not do that. Long answer: See http://stackoverflow.com/a/10847242/1391963
Unstage (undo add) files:
git reset HEAD file.txtSee closest tag
git describe --tags `git rev-list --tags --max-count=1`Source: http://stackoverflow.com/q/1404796/1391963. See also git-describe.
Check Differences
See non-staged (non-added) changes to existing files
git diffNote that this does not track new files.
See staged, non-commited changes
git diff --cached
# or
git diff --staged--staged is a synonym for --cached
See differences between local changes and master
git diff origin/masterNote that origin/master is one local branch, a shorthand for refs/remotes/origin/master, which is the full name of the remote-tracking branch.
See differences between two commits
git diff COMMIT1_ID COMMIT2_IDSee the files that changed between two commits
git diff --name-only COMMIT1_ID COMMIT2_IDSee the files changed in a specific commit
git diff-tree --no-commit-id --name-only -r COMMIT_ID
#or
git show --pretty="format:" --name-only COMMIT_IDSource: http://stackoverflow.com/a/424142/1391963
See diff before push
git diff --cached origin/masterSee diff with only the changed lines (no context)
git diff --unified=0See details (log message, text diff) of a commit
git show COMMIT_IDCount the number of commits
git rev-list HEAD --count
git rev-list COMMIT_ID --countCheck the status of the working tree (current branch, changed files…)
git statusCommiting
git add changed_file.txt
git add folder-with-changed-files/
git commit -m "Commiting changes"Rename/move and remove files
git rm removeme.txt tmp/crap.txt
git mv file_oldname.txt file_newname.txt
git commit -m "deleting 2 files, renaming 1"Change message of last commit
git commit --amend -m "New commit message"Push local commits to remote branch
git push origin masterRevert one commit, push it
git revert dd61ab21
git push origin masterRevert to the moment before one commit
#reset the index to the desired tree
git reset 56e05fced
#move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"
#Update working copy to reflect the new commit
git reset --hardSource: http://stackoverflow.com/q/1895059/1391963
Undo last commit, preserving local changes
git reset --soft HEAD~1Undo last commit, without preserving local changes
git reset --hard HEAD~1Undo last commit, preserving local changes in index
git reset --mixed HEAD~1
#or
git reset HEAD~1See also http://stackoverflow.com/q/927358/1391963
Undo non-pushed commits
git reset origin/masterReset to remote state
git fetch origin
git reset --hard origin/masterSee local branches
git branchSee all branches
git branch -aMisc
Logs
See recent commit history
git logSee commit history for the last two commits
git log -2See commit history for the last two commits, with diff
git log -p -2short for patch, the -2 can be omitted
See commit history printed in single lines
git log --pretty=onelineDebug SSH connection issues
GIT_SSH_COMMAND="ssh -vvv" git clone <your_repository>Have git pull running every X seconds, with GNU Screen
Use Ctrl+a Ctrl+d to detach the screen. See previous git commands executed
history | grep git
# or
grep '^git' /root/.bash_historySee recently used branches (i.e. branches ordered by most recent commit)
git for-each-ref --sort=-committerdate refs/heads/ | headSource: http://stackoverflow.com/q/5188320/1391963
Look for conflicts in your current files
grep -H -r "<<<" *
grep -H -r ">>>" *
grep -H -r '^=======$' *