Git cheat sheet

git cheat sheet

In this article, I am providing a Git Cheat Sheet which I use for my reference. Git is a powerful and simple tool with a small learning curve. The commands are not confusing and less in number. Go through the below reference tables.

Git cheat sheet

The commands are distributed under several headings according to its use case scenario. The first column denotes its use and the second column denotes its git command. Start reading from here.

CONFIGURE GIT

Name you want to be attached to your commit transactions$ git config –global user.name “[name]”
An email address you want to be attached to your commit transactions$ git config –global user.email “[email address]”
Colorization of command line output$ git config –global color.ui auto

 

CREATE OR CLONE A REPOSITORY

Clone an existing repository$ git clone [ url ]
Create a new local repository$ git init [project-name]

 

LOCAL REPOSITORY CHANGES

Changed files in your working directory$ git status
Snapshots the file in preparation for versioning$ git add [file]
Add all current changes to the next commit$ git add .
Add some changes in <file> to the next commit$ git add -p <file>
Commit all local changes in tracked files$ git commit -a
Commit previously staged changes$ git commit
$ git commit -m “[descriptive message]”
Change the last commit$ git commit –amend

 

REVIEW CHANGE HISTORY

Show all commits, starting with newest$ git log
Show changes over time for a specific file$ git log -p <file>
Lists history for a file, incl. renames$ git log –follow [file]
Changes to tracked files#$ git diff
Shows file differences between staging and the last file version$ git diff –staged
Shows differences between two branches$ git diff [first-branch]…[second-branch]
Outputs metadata and content changes of the specified commit$ git show [commit]
Who changed what and when in <file>$ git blame <file>

 

BRANCHES & TAGS

List all existing branches$ git branch -av
Switch HEAD branch$ git checkout <branch>
Create a new branch based

on your current HEAD

$ git branch <new-branch>
Create a new tracking branch based on

a remote branch

$ git checkout –track <remote/branch>
Delete a local branch$ git branch -d <branch>
Mark the current commit with a tag$ git tag <tag-name>

 

UPDATES & PUBLISH CHANGES

List all currently configured remotes$ git remote -v
Show information about a remote$ git remote show <remote>
Add new remote repository, named <remote>git remote add <shortname> <url>
Download all changes from <remote>, but

don‘t integrate into HEAD

$ git fetch <remote>
Download changes and directly

merge/integrate into HEAD

$ git pull <remote> <branch>
Publish local changes on a remote$ git push <remote> <branch>
Delete a branch on the remote$ git branch -dr <remote/branch>
Publish your tags$ git push –tags
Changes the file name and prepares it for commit$ git mv [file-original] [file-renamed]
Deletes the file from the working directory and stages the deletion$ git rm [file]
Removes the file from version control but preserves the file locally$ git rm –cached [file]

 

MERGE REPOSITORIES & REBASE

Merge <branch> into your current HEAD$ git merge <branch>
Rebase your current HEAD onto <branch>$ git rebase <branch>
Abort a rebase$ git rebase –abort
Continue a rebase after resolving conflicts$ git rebase –continue
Use your configured merge tool to solve

conflicts

$ git mergetool
Use your editor to manually solve conflicts

and (after resolving) mark file as resolved

$ git add <resolved-file>
$ git rm <resolved-file>

 

UNDO COMMITS TO A REPOSITORY

Discard all local changes in your working

directory

$ git reset –hard HEAD
Discard local changes in a specific file$ git checkout HEAD <file>
Revert a commit (by producing a new commit with contrary changes)$ git revert <commit>
Reset your HEAD pointer to a previous commit and discard all changes since then$ git reset –hard <commit>
…and preserve all changes as unstaged

changes

$ git reset <commit>
…and preserve uncommitted local changes$ git reset –keep <commit>

 

EXCLUDE FILES AND PATHS FROM TRACKING

Add a text file named .gitignore to suppresses accidental versioning of files and paths matching the specified patterns*.log
build/
temp-*
Lists all ignored files in this project$ git ls-files –other –ignored –exclude-standard

 

SAVE FRAGMENTS

Temporarily stores all modified tracked files$ git stash
Restores the most recently stashed files$ git stash pop
Lists all stashed changesets$ git stash list
Discards the most recently stashed changeset$ git stash drop

All the above Git commands in this git cheat sheet are generic commands. The different git hosters and provider distribution will have their own commands in addition to it. You can find more details about the Git project here and detailed documentation is available in the documentation section available here.

Another article about Git I have in this blog is “Tips and Best Practices for version control using GIT”. Please click here to read it if it interests you.

This is all about the Git Cheat Sheet. Hope you like it. Don’t forget to share and post any comments.

Leave a Reply

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