Skip to content

Instantly share code, notes, and snippets.

@NimaAra
Last active June 6, 2019 23:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NimaAra/859e4ebc53d8b64364fa470d6270aa1c to your computer and use it in GitHub Desktop.
Save NimaAra/859e4ebc53d8b64364fa470d6270aa1c to your computer and use it in GitHub Desktop.
Git commands cheat-sheet
Essentials

Initialize a Working Area:

git init

Clone an existing Repository:

git clone https://github.com/libgit2/libgit2
git clone https://github.com/libgit2/libgit2 mylibgit

git clone does the following:

  • Clones a Repository to a given directory
  • Creates remote tracking branches for each branch (visible using git branch -r)
  • Creates and checks out an initial branch that is forked from the cloned Repository's currently active branch

After the clone, a plain git fetch without arguments will update all the remote tracking branches, and a git pull without arguments will in addition merge the remote master branch into the current master branch, if any.

This default configuration is achieved by creating references to the remote branch heads under refs/remotes/origin and by initializing remote.origin.url and remote.origin.fetch configuration variables.

Checkout branch (foo) and point HEAD to it:

git checkout foo

Displays references available in a local repository along with the associated commit IDs:

git show-ref
git show-ref --tags
git show-ref --heads

Check status:

git status
git status -s
git status --short

When using the -s or --short flags the following convention is used:

  • !! Ignored files and folders
  • ?? New files that are not tracked
  • A New files that have been added to the Staging Area
  • M File that have been modified
  • MM File that have been modified, staged then modified again
  • D File that have been deleted
  • R File that have been renamed
  • C File that have been copied
  • U File that have been updated but unmerged

Check status (including untracked files):

git status -su

Compare the Working Area with the Staging Area (Changes not yet staged):

git diff

That command compares what is in your working directory with what is in your staging area. The result tells you the changes you’ve made that you haven’t yet staged.

Compare but produce a more compact result:

git diff --stat
git diff --shortstat
git diff --dirstat

Compare the Staging Area with the Repository (Changes that are staged which will go into next commit):

git diff --staged
git diff --cached

Show commits not yet pushed:

git log @{u}..
git cherry -v

Show commits not yet pushed against a branch (foo):

git cherry -v origin/foo

Show what has changed since the last commit:

git diff HEAD

Compare a branch (foo) with the Master:

git diff foo master

Check logs:

git log

Move a file or files from the Working Area to the Staging Area:

git add foo.txt
git add *.cs

Move all files in the current and subdirectories from the Working Area to the Staging Area:

git add .

Move the changes from the Staging Area to the Repository:

git commit -m "initial commit"

Commit everything against the last commit:

git commit --amend -a

Check out data from a Branch (foo) from the Repository to both the Working Area and the Staging Area:

git checkout foo

Rename a file and move it from Working Area over to the Staging Area:

git mv bar.txt bart.jpeg

Fetching all the data from the Remote (foo):

git fetch foo

This goes out to the remote project foo and pulls down all the data that you do not have yet. After doing this you should have references to all the branches from that remote which you can then merge or inspect at any time.

Fetching all the data from all your Remotes:

git fetch --all

Refresh deleted branches from the Remote:

git fetch -p

Pushing to Remote

git push foo master

This pushes any commits you have done back up to the server. The command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to fetch their work first and incorporate it into yours before you’ll be allowed to push.

Revert given files to the previuos version of a given commit:

git checkout c5f567~1 -- file1/to/restore file2/to/restore

Revert given files to a given commit:

git checkout c5f567 -- file1/to/restore file2/to/restore

Revert a given commit:

git revert --no-commit <commit-hash>
git revert -n <commit-hash>

The commands above are equal. Note this only reverts the changes to the local repository, don't forget to push the changes back to the server.

Unstaging changes

Remove a file from both the Working Area and the Staging Area:

git rm foo.txt -f

Remove & Replace file from both the Working Area and the Staging Area:

git checkout HEAD foo.txt

Note this command overwrites the foo.txt file in both Working Area and Staging Area by copying it from the Repository.

Remove a file from the Staging Area and not the Working Area:

git rm foo.txt --cached
git reset HEAD foo.txt

Remove every change from both the Working Area and the Staging Area:

git reset --hard HEAD

Note this command copies all the files from the Repository over to the Staging Area as well as the Working Area.

Remove untracked files as well as all files/folders specified in .gitignore:

git clean -d -x -f
  • -d Untracked files including directories
  • -x Files ignored by git (.gitignore)
  • -f Force

Remove every change from the Staging Area and not the Working Area:

git reset HEAD

Note this command copies all the files from the Repository over to the Staging Area but does not touch the Working Area.

Make current branch (typically master) to point to a given commit:

git reset e153d3

Make current branch (typically master) to point to a given commit, then make the files in the Working Area and the Staging Area the same as the versions committed in a given commit:

git reset --hard e153d3

Branching (Essentials)

Create Local Branch

git branch feature/FOO-123

Push Branch to Remote

git push -u origin feature/FOO-123

Push changes to Branch

git push

Delete Local Branch

gb -d feature/FOO-123

Delete Remote Branch

git push origin --delete feature/FOO-123

Checkout remote branch

git checkout --track origin/feature/FOO-123
or
git checkout -b FOO-123 origin/feature/FOO-123

Rebasing (Essentials)

Rewriting history in the feature branch for the last 3 commits

git checkout feature
git rebase -i HEAD~3

Rebasing feature branch to origin/master

git fetch --all
git rebase origin/master feature/FOO-123 // Incorporate changes from origin/master and include it in local feature branch
git push origin feature/FOO-123:master   // Make the origin/master point to the feature branch

Which is the same as:

git fetch --all    
git checkout feature/FOO-123
git rebase origin/master
git checkout master
git pull
git rebase feature/FOO-123
git push origin master   

Incorporating changes from origin/master

git fetch --all
git rebase origin/master feature/FOO-123
git push -f // to push the updated feature branch to remote

Which is the same as:

git fetch --all
git checkout feature/FOO-123
git rebase origin/master
git push -f

Exploring (Essentials)

Commits on the current branch

git log master..
git log origin/master..

Commits on the origin/feature/FOO-123 that are yet to be pulled in to the tracking local branch.

git log ..@{u}
git log ..origin/feature/FOO-123
git log feature/FOO-123..origin/feature/FOO-123

Commits on the current branch that are yet to be pushed to it's tracking remote e.g. origin/feature/FOO-123.

git log @{u}..
git log origin/feature/FOO-123..
git log origin/feature/FOO-123..feature/FOO-123

Commits on the current branch that are yet to be pushed to origin/master.

git log origin/master..

Commits on the current tracking remote branch that are yet to be pushed to origin/master.

git log origin/master..@{u}
git log origin/master..origin/feature/FOO-123

Commits on the origin/master that are yet to be pulled in to current branch.

git log ..origin/master

Commits on the origin/master that are yet to be pulled in to current tracking remote branch.

git log @{u}..origin/master
git log origin/feature/FOO-123..origin/master

Branching

List Local branches:

git branch

List Remote branches:

git branch -r

Note remember to run git fetch --all for getting the latest changes.

List both Local and Remote branches:

git branch -a

List branches including their last commits:

git branch -v
git branch -rv
git branch -av

List branches you have already merged:

git branch --merged

Note entries in the list produced by above which are missing the * in front of them are generally fine to delete as you've already incorporated their work into another branch so you are not goint to loose anything.

List branches containing work you have not merged:

git branch --no-merged

List Tracking branches:

git branch -vv

Checkout a Remote branch (foo):

git fetch --all
git checkout foo

Create a new empty local branch (EMPTY) then push it to Remote:

git checkout --orphan EMPTY
git reset --hard
git commit --allow-empty -m "Created an empty branch"
git push origin EMPTY

Create a new Local branch (foo):

git branch foo

Note if you are on master and run the above command it merely creates the foo branch and the HEAD will still point to master.

Switch to a branch (foo):

git checkout foo

This command moves HEAD to point to the foo branch. Note this also reverts the files in the Working Area back to the snapshot that foo points to.

Create and Switch to a new Local branch (foo):

git checkout -b foo
// the above is the same as:
git branch foo
git checkout foo

Create a Remote branch (feature/foo) from the current branch:

git checkout -b feature/foo
// then push it to remote
git push -u origin feature/foo
// which is the same as ... git push <remote-name> <local-branch-name>:<remote:branch:name>
git push origin feature/foo:feature/foo

Mege branch (foo) with master:

git checkout master
git merge foo

Delete a Local branch (foo):

git branch -d foo

Delete a Remote branch (foo):

git push origin --delete foo
git push origin -d foo

Push a Local branch (foo) to Remote:

git push origin foo

// you can also change the remote name to bar
git push origin foo:bar

Push changes overwriting any changes on the branch (foo):

git push --force origin foo
git push -f origin foo

Working with Remote

List the short names of remote servers configured:

git remote

List the short names and urls of remote servers configured:

git remote -v

List remote references (branches, tags etc):

git ls-remote

Add Remote Repository as foo:

git remote add foo https://github.com/someone/foo

Remove Remote Repository (foo)

git remote remove foo
git remote rm foo

Renaming a Remote (foo to bar)

git remote rename foo bar

Changing a Remote to a Repository with user/pass:

git remote set-url origin https://<USER>:<PASS>@github.com/<USER>/<REPOSITORY>.git

Inspecting a Remote

git remote show origin

Exploring

Browse the Repository:

gitk --all

The above command shows all the refs(branches, tags etc.).

Show changes in a given file:

gitk v2.5.0.. EasyMessageHub\

The above shows the changes since v2.5.0 that changed any file in the EasyMessageHub\ directory.

Show all the acitivity for the current branch:

git reflog

Show the logs since a given time:

git log --since=2.weeks
git log --since="2017-02-21"

Show the logs from a given time:

git log --after=2.weeks
git log --after="2017-02-21"

Show the logs from a given committer/author:

git log --comitter=nima
git log --author=nima

Show the history in a nice format:

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

Show the history with date and committer name:

git log --graph --pretty=format:"%C(yellow)%h%Creset%C(cyan)%C(bold)%d%Creset %C(green)[%cn]%Creset %s %C(cyan)[%ci (%cr)]%Creset"

Show the history in more detail:

git log --stat

Show the history of a file (foo.txt):

git log -p foo.txt
gitk foo.txt

Note the above command does not show changes if a file is renamed.

Show the entire history of a file (foo.txt) including history beyond renames and with diffs for each change:

git log --follow -p -- foo.txt
gitk --follow -p -- foo.txt

In other words, if the file named bar was once named foo, then git log -p bar (without the --follow option) will only show the file's history up to the point where it was renamed -- it won't show the file's history when it was known as foo. Using git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as foo. The -p option ensures that diffs are included for each change.

Showing who has changed a given file:

git blame foo.txt

The command shows the latest commit which changed a given line in the file foo.txt. A ^ signifies the first time the file was added to the project.

Show detaild diff for each committed log:

git log --patch
git log -p

Filter the logs:

git log --grep apples --oneline

This filters to only show the logs with the word apples in the commit message.

git log -S apples --patch
git log -G apples --patch

-S accepts a string and -G accepts a REGEX; The above shows the logs where the word apples was added or removed from any file.

Show the last n commits:

git log -3 --oneline

Show the details between given commits:

git log HEAD~5..HEAD^ --oneline

This commands shows the log between the 5 commits before the HEAD and the parent of the HEAD commit.

Show the history between branches:

git log foo..master --oneline

Shows the commits that are in the master branch but not in the foo branch. E.g. go to from foo to master and show me all the new commits that you see. So if we merge master right now we would get all the displayed commits.

Show the commits between tags:

git log Easy.Common-release-v2.9.9..Easy.Common-release-v3.0.0 --oneline

Shows the commits between the given tags.

Show the details of a given commit:

git show 2cc5405
git show someBranchName
git show HEAD
git show HEAD^
git show HEAD~2

The ^ refers to the parent and it can be used multiple times e.g. HEAD^^ referring to the parent commit of the parent commit of the commit the HEAD is pointing to. The same can be achieve by using ~ for example HEAD~2.

Show where HEAD was a month ago:

git show HEAD@{"1 month ago"}

Show the difference between commits:

git diff HEAD HEAD~2

The command shows the difference between the current commit and 2 commits earlier.

Show the difference between branches:

git diff master someBranch

Show the difference between the given files; The files do not need to be part of a git repository:

git diff --color-words 1.txt 2.txt

Fixing Mistakes (DO NOT USE TO CHANGE SHARED HISTORY ONLY LOCAL COMMITS)

Fixing the last commit message:

git commit --amend -m "ABC-123 fixed: Should work fine"

Fixing latest commits:

git commit --amend

This commands ammends the last commit.

Working with Tags

Listing all the Tags:

git tag

Searching for Tags:

git tag -l "v1.2.3"

Creating a Tag (annotated)

git tag -a v1.4 -m "Stable release"

Creating a Tag (annotated) from a commit

git tag -a v1.6 38ea264 -m "Stable release"

Showing a Tag along with the commit that was tagged:

git show v1.4

Pushing a Tag to the server:

git push origin v1.4

Pushing all Tags to the server:

git push origin --tags

Checkout a Tag:

git checkout -b version1.4Branch v1.4

Note if you run this command and do a commit, your version1.4 branch will be slightly different than your v1.4 tag since it will move forward with your new changes, so do be careful.

Deleting a remote Tag:

git fetch --all
git tag -d v1.4
git push origin :refs/tags/v1.4

Setting proxy

git config --global http.proxy http://user:pass@somewhere.com:1234

SVN to Git Migration

Working with both SVN and Git:

1. git svn init -s https://svn.repo/app ./MyApp This assumes that under the SVN repo there is the standard folder structure of trunk, branches and tags.

2. Do a git svn fetch until no more commits are fetched.

3. You can then checkout a specific SVN branch or trunk via git checkout -b trunk remotes/trunk.

  • To pull in new commits from SVN use: git svn rebase
  • To push local commits to SVN use: git svn dcommit

Conversion:

1. Convert the SVN repository to Git:

git svn clone --stdlayout --no-metadata https://svn-repo.com/Something/ ./Awesome

2. Create the repository on the server:

  • Add README.md
  • Add .gitignore

3. CD into the directory and add remote repository:

git remote add origin https://github.com/Someone/Awesome.git

4. Get the latest changes (README.md, .gitignore etc.):

git pull origin master --allow-unrelated-histories

5. Push everything to the server:

git push origin master --force

Git Bundling

This makes all remote branches also local branches

git clone --mirror git@example.org:path/repo.git
git bundle create repo.bundle --all

THIS and THAT

Working with Config

Edit local git config

git config -e

Edit global git config

git config -e --global

Set Notepad++ as editor

git config --global core.editor "'c:/program files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Working on a feature branch

  1. Create a feature branch:
git checkout -b my-feature
  1. Push branch to remote:
git push -u origin my-feature

Setting up Bare Repositories

  1. Create an empty repository:
cd C:\My-Git\
git init --bare My-Repo.git

2.a Clone the empty repository to a working directory

git clone C:\My-Git\My-Repo.git My-Repo

2.b Create a working folder from existing files (non git repo)

cd C:\Somewhere\My-Repo2
git init
git add .
git commit -m "some stuff"

git remote add origin C:\My-Git\My-Repo.git
git push -u origin master

Setting up SSH

As per THIS

Create the key

ssh-keygen -t rsa -b 4096 -C "Nima.Ara@Outlook.com"

Add the key to SSH (git-bash.exe)

eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa

Go to Github and add the public key for your machine.

Create a config file inside: ~\.ssh with the following content:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Host github-PRIVATE
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_PV

Add the repo using the SSH format

git@github-PRIVATE:USERNAME/Foo.git


pull vs fetch

  • When you use pull, git tries to automatically do your work for you. It is context sensitive, so git will merge any pulled commits into the branch you are currently working in. pull automatically merges the commits without letting you review them first. If you don’t closely manage your branches, you may run into frequent conflicts.
  • When you fetch, git gathers any commits from the target branch that do not exist in your current branch and stores them in your local Repository. However, it does not merge them with your current branch. This is particularly useful if you need to keep your Repository up to date, but are working on something that might break if you update your files. To integrate the commits into your master branch, you use merge.

clone vs checkout

  • clone is for fetching repositories you don't have.
  • checkout is for switching between branches in a Repository you already have; It can also be used to overwrite a file in your working copy with a version of that file from another revision.

What did I just commit?

git show
or
git log -1 -p

Show the content of a file at a specific commit

git show a4c8def:README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment