Some userful GIT commands — 02.03.2019

Set your details

git config --global "John Doe"
git config --global ""

Make git ignore file modes

cd project/
git config core.filemode false

See your settings

git config --list

Initialize a git repository for existing code

cd existing-project/
git init

Clone a remote repository

git clone

Clone a remote repository in the current directory

git clone .

Get help for a specific git command

git help clone

Update and merge your current branch with a remote

cd repository/
git pull origin master

View remote urls

git remote -v

Change origin url

git remote set-url origin http//

Add remote

git remote add remote-name

See non-staged (non-added) changes to existing files

git diff

See staged, non-commited changes

git diff --cached

See differences between local changes and master

git diff origin/master

See differences between two commits


See the files that changed between two commits

git diff --name-only COMMIT1_ID COMMIT2_ID

See the files changed in a specific commit

git diff-tree --no-commit-id --name-only -r COMMIT_ID
git show --pretty="format:" --name-only COMMIT_ID

See diff before push

git diff --cached origin/master

See diff with only the changed lines (no context)

git diff --unified=0

See details (log message, text diff) of a commit

git show COMMIT_ID

Count the number of commits

git rev-list HEAD --countgit rev-list COMMIT_ID --count

Check the status of the working tree (current branch, changed files…)

git status

Make some changes, commit them

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 master

Push commits to all remotes, in a single command

Git does not do that, but see

See recent commit history

git log

See commit history for the last two commits

git log -2

See commit history for the last two commits, with diff

git log -p -2

See commit history printed in single lines

git log --pretty=oneline

Revert one commit, push it

git revert dd61ab21
git push origin master

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

Undo last commit, preserving local changes

git reset --soft HEAD~1

Undo last commit, without preserving local changes

git reset --hard HEAD~1

Undo last commit, preserving local changes in index

git reset --mixed HEAD~1

Undo non-pushed commits

git reset origin/master

Reset to remote state

git fetch origin
git reset --hard origin/master

See local branches

git branch

See all branches

git branch -a

Make some changes, create a patch

git diff > patch-issue-1.patch

Add a file and create a patch

git add newfile
git diff --staged > patch-issue-2.patch

Add a file, make some changes, and create a patch

git add newfile
git diff HEAD > patch-issue-2.patch

Make a patch for a commit

git format-patch COMMIT_ID

Make patches for the last two commits

git format-patch HEAD~2

Make patches for all non-pushed commits

git format-patch origin/master

Create patches that contain binary content

git format-patch --binary --full-index origin/master

Apply a patch

git apply -v patch-name.patch

Apply a patch created using format-patch

git am patch1.patch

Break up multiple changes into separate commits (or commit only part of a changed file)

git add --patch file.txt
(press 'y' for the chunks to add)
git commit -m 'first part of the file'
(repeat if desired)

Create a tag

git tag 7.x-1.3

Push a tag

git push origin 7.x-1.3

Create a branch

git checkout master
git branch new-branch-name

Create a branch from a previous commit

git branch branchname sha1-of-commit
git branch branchname HEAD~1
git checkout -b branchname sha1-of-commit

Checkout a branch

git checkout new-branch-name

See commit history for just the current branch

git cherry -v master

Merge branch commits

git checkout master
git merge branch-name

Merge a branch without committing

git merge branch-name --no-commit --no-ff

See differences between the current state and a branch

git diff branch-name

See differences in a file, between the current state and a branch

git diff branch-name path/to/file

Delete a branch

git branch -d new-branch-name

Push the new branch

git push origin new-branch-name

Get all branches

git fetch origin

Get the git root directory

git rev-parse --show-toplevel

Remove from repository all locally deleted files

git rm $(git ls-files --deleted)

Delete all untracked files

git clean -f
git clean -f -d
git clean -n -f -d

Delete all files from a git repository that have already been deleted from disk:

git ls-files --deleted -z | xargs -0 git rm

Show total file size difference between two commits

Short answer: Git does not do that.
Long answer: See

Unstage (undo add) files:

git reset HEAD file.txt

See closest tag

git describe --tags `git rev-list --tags --max-count=1`

Debug SSH connection issues

GIT_SSH_COMMAND="ssh -vvv" git clone <your_repository>

Have git pull running every X seconds, with GNU Screen

for((i=1;i<=10000;i+=1)); do sleep 30 && git pull; done

See previous git commands executed

history | grep git
grep '^git'  /root/.bash_history

See recently used branches (i.e. branches ordered by most recent commit)

git for-each-ref --sort=-committerdate refs/heads/ | head

Tar project files, excluding .git directory

cd ..
tar cJf project.tar.xz project/ --exclude-vcs

Tar all locally modified files

git diff --name-only | xargs tar -cf project.tar -T -

Look for conflicts in your current files

grep -H -r "<<<" *
grep -H -r ">>>" *
grep -H -r '^=======$' *

Apply a patch not using git:

patch -p1 < file.patch



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

Amir Mustafa

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on