Some userful GIT commands — 02.03.2019

Here there are some examples of git commands that I use often.

Not all commands written here are git commands, but all of them are related to git. Please refer to the documentation for more details.

Set your details

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.

Make git ignore file modes

This option is useful if the file permissions are not important to us, for example when we are on Windows.

See your settings

Initialize a git repository for existing code

Clone a remote repository

This creates a new directory with the name of the repository.

Clone a remote repository in the current directory

Get help for a specific git command

Update and merge your current branch with a remote

Where origin is the remote repository, and master the remote branch.
If you don’t want to merge your changes, use git fetch

View remote urls

Change origin url

Add remote

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

Note that this does not track new files.

See staged, non-commited changes

See differences between local changes and master

Note 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

See the files that changed between two commits

See the files changed in a specific commit



See diff before push

See diff with only the changed lines (no context)

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

Count the number of commits

git rev-list COMMIT_ID --count

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

Make some changes, commit them

Rename/move and remove files

Change message of last commit

Push local commits to remote branch

Push commits to all remotes, in a single command

Git does not do that, but see

See recent commit history

See commit history for the last two commits

See commit history for the last two commits, with diff

See commit history printed in single lines

Revert one commit, push it

Revert to the moment before one commit

# 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

Undo last commit, without preserving local changes

Undo last commit, preserving local changes in index

Or git reset HEAD~1
See also

Undo non-pushed commits

Reset to remote state

See local branches

See all branches

Make some changes, create a patch

Add a file and create a patch

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

Make a patch for a commit

Make patches for the last two commits

Make patches for all non-pushed commits

Create patches that contain binary content

Apply a patch

Apply a patch created using format-patch

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


Create a tag

Push a tag

Create a branch

Here master is the starting point for the new branch. Note that with these 2 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-name

Create a branch from a previous commit

or using a symbolic reference (e.g. last commit):

You can also use


Checkout a branch

See commit history for just the current branch

(master is the branch you want to compare)

Merge branch commits

Here we are merging all commits of branch-name to master.

Merge a branch without committing

See differences between the current state and a branch

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

Delete a branch

Push the new branch

Get all branches

Get the git root directory


Remove from repository all locally deleted files


Delete all untracked files

Including directories:

Preventing sudden cardiac arrest:


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

Source (and alternatives):

Show total file size difference between two commits

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

Unstage (undo add) files:

See closest tag

Source: See also git-describe.

Debug SSH connection issues

Have git pull running every X seconds, with GNU Screen

Use Ctrl+a Ctrl+d to detach the screen.

See previous git commands executed


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


Tar project files, excluding .git directory

Tar all locally modified files

Look for conflicts in your current files

There’s also git-grep.

Apply a patch not using git:

Web Artisan. Human — Engineering.