Category: Tutorials
Share:

The following is a list of some useful GIT commands that I often turn to during my work process. It is organized in blocks of similar logical use – shortcuts here:

Remote
Cloning
Branching
Deleting
Comparing Commits
Merging
Stashing
Pushing

I will assume that you already have some familiarity with GIT, what it does and why one would use it. If that is not the case, check out Atlassian’s What Is GIT?.

The list below is a comprehensive cheatsheet intended for command line use, which I prefer. Tools such as SourceTree are super handy, in particular when dealing with conflicts. Personally, I find it more efficient when operating primarily from the command line.

First, do yourself a favor (if you haven’t done it yet), and enable git syntax highlighting – it is a beautiful thing:

1
git config --global color.ui auto

Now, to the actual git commands…

Remote

Syncs branch to remote

1
git fetch

Lists all remotes

1
git remote

Lists remote information (e.g. URL for fetching and push)

1
git remote -v

Add remote

1
git remote add origin https://github.com/someuser/repo.git

Removes origin repo

1
git remote rm origin

Shows where repository info is stored

1
cat .git/config

Cloning

Clone

1
git clone https://github.com/someuser/repo.git

Clone to a assigned directory

1
git clone https://github.com/someuser/repo.git assigneddirectory

Branching

Lists branches

1
git branch

Create a new branch (named newbranch)

1
git branch newbranch

Switch to a branch (named newbranch)

1
git checkout newbranch

Creates and switches to a new branch (named anotherbranch)

1
git checkout -b anotherbranch

Discard local changes of a file

1
git checkout -- filename.html

Creates new branch tracking a remote branch

1
git branch -b newbranch origin/newbranch

Creates new branch tracking a remote branch and switches to it

1
git checkout -b newbranch origin/newbranch

Discard local changes of a file, replace by the version in another branch

1
git checkout somebranch -- filename.html

Show all branches contained in the current branch

1
git branch --merged

Renames branch

1
git branch -m oldname newname

Shows remote branches

1
git branch -r

Shows all branches (local and remote)

1
git branch -a

Show path / reference to branch HEAD

1
cat .git/HEAD

Lists directories / branches

1
ls -la .git/refs/heads

Deleting

Deletes branch

1
git branch -d branchtodelete

Deletes branch regardless of unmerged content

1
git branch -D branchtodelete

Comparing Commits

Lists commits and SHA* in compact view

1
git log --oneline

Lists commits and SHA* in compact view, showing all branches (‐‐all) in color

1
git log --graph --oneline --decorate --all

Lists a certain number of remote’s commits in compact view

1
git log --oneline origin/master -3

Show changes between a specific point in time to current working directory

1
git diff 871d36b

Compares a file from a specific point in time to now

1
git diff 871d36b myfilename.php

Compares a specific point in time to another point (range – from to)

1
git diff 871d36b..e7b1871

Compares a file from specific point in time to another point (range – from to)

1
git diff 871d36b..e7b1871 myfilename.php

Compares specific point in time to head

1
git diff 871d36b..HEAD

Compares specific point in time to head, listing number of changes(‐‐stat) and files changed (‐‐summary)

1
git diff --stat --summary 871d36b..HEAD

Compares specific point in time to head, ignoring whitespaces(-b) and all spaces(-w)

1
git diff -b -w 871d36b..HEAD

Compares changes between branches

1
git diff master..anotherbranch

Compares changes between branches showing inline color coded changes

1
git diff --color-words master..anotherbranch

Compares changes between a branch and second to last commit in another branch

1
git diff master..somebranch^

Compares remote and local branches

1
git diff origin/master..master

Shows changes in a commit

1
git show 436eb47

Shows only a certain number of commits (3 in this case)

1
git log --oneline -3

Merging

Get changes from another branch

1
git merge anotherbranch

Merge, but do not fast-forward*

1
git merge --no--ff somebranch

Only merge if fast-forward* is possible

1
git merge --ff-only abranch

Cancel merge

1
git merge --abort

Stashing

Stash local changes

1
git stash

Add a message to a stash

1
git stash save "my stash message here"

List stashes

1
git stash list

Lists files and inserts of the most recent stash

1
git stash show stash@{0}

Lists files and inserts of a certain stash as diff

1
git stash show -p stash@{1}

Applies latest stash and delete from stashes

1
git stash pop

Applies a stash and delete from stashes

1
git stash pop stash@{1}

Applies stash but does not delete it from stashes

1
git stash apply

Deletes a stash entry

1
git stash drop stash@{2}

Removes all the stashes

1
git stash clear

Pushing

Push changes to remote

1
git push origin master

Set tracking in relation to remote

1
git push -u origin master

Deletes branch on remote, keeping local

1
git push origin :branchtodelete

Deletes branch

1
git push origin --delete branchtodelete

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.