Commonly Used Git Commands – A Cheat Sheet

Note: This post is part 2 of two-parts Learning to Work with Git & GitHub post series. This learning post is still in active development and updated regularly.

In the previous 1st part of this learning series introduction to Git and its key features, common Git terms, basic Git commands, and Git workflow from a perspective a front-end web developer were discussed.

Learning series
Part 1: Introduction to Git – The Basics
Part 2: Commonly Used Git Commands – A Cheat Sheet (this post)

The objective this part 2 of Git learning series is assemble a cheat sheet of most commonly used Git commands, a go to resource.

Commonly Used Git Commands

Before getting started with the Git & GitHub, it is important to get familiar with some commonly used terms. A google search with “Git Cheat Sheet”, returns thousands of ready to use results at our finger tips, why other cheat sheet? That is a good question! This Git command cheat sheet is intended to be my personalized go-to source for my day to day Git & GitHub uses. Hence this cheat sheet is work-in-progress.

There are various ways of organizing the Git commands. In this posts, the commands are organized primarily after Git Training manual.

Git Configuration

Configuration (get and set) of user information for all local repositories or global options. It can be query/set/replace/unset options with this command. The name is actually the section and the key separated by a dot, and the value will be escaped.

#! Set the username
git config –global user.name
#! Set the user email
git config –global user.email
#! Create a Git command shortcut
git config –global alias.
#! Set the preferred text editor
git config –system core.editor
#! Open and edit the global config file in the text editor
git config –global –edit
#! Enable command line highlighting
git config –global color.ui auto

Additional Information: Git Setup & Config

Starting a Project

When starting out with a new repository, you only need to do it once; either locally, then push to GitHub, or by cloning an existing repository.

#! Create an empty repository in the project folder
git init
#! Clone a repository from GitHub and add it to the project folder
git clone (repo URL)
#! Clone a repository to a specific folder
git clone (repo URL) (folder)
#! Display a list of remote repositories with URLs
git remote -v
#! Remove a remote repository
git remote rm (remote repo name)
#! Retrieve the most recent changes from origin but don’t merge
git fetch
#! Retrieve the most recent changes from origin and merge
git pull

Additional Information: Git Init | Git Documentation

Ignoring Files

Files can be excluded from being tracked with Git which is typically done in a special file named .gitignore .

cat .gitignore
/logs/*
!logs/.gitkeep
/tmp
*.swp

Additional Information: Collection of .gitigore templates at GitHub

Managing File Changes

Files changes in staging area can be managed by add, rm (remove), or viewing previous versions of a file. Every changes made in a file has entry in the log. Here a file can be browse and inspect the evolution of project files.

#! Add file changes to staging
git add (file name)
#! Add all directory changes to staging
git add .
#! Add new and modified files to staging
git add -A
#! Remove a file and stop tracking it
git rm (file_name)
#! Untrack the current file
git rm –cached (file_name)
#! Recover a deleted file and prepare it for commit
git checkout <deleted file name>
#! Display the status of modified files
git status
#! Display a list of ignored files
git ls-files –other –ignored –exclude-standard
#! Display all unstaged changes in the index and the current directory
git diff
#! Display diff between files in staging and the most recent versions
git diff –staged
#! Display changes in a file compared to the most recent commit
git diff (file_name)
Git Branches

Quoting directly from the Git documentationA branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As you initially make commits, you’re given a master branch that points to the last commit you made. Every time you commit, it moves forward automatically.”

#! List all local branches
git branch
#! List remote and local branches
git branch -a
#! Create a local branch and switch to it
git checkout -b branch_name
#! Switch to an existing branch
git checkout branch_name
#! Push branch to remote
git push origin branch_name
#! Rename current branch
git branch -m new_name
#! Delete a local branch
git branch -d branch_name
#! Delete a remote branch
git push origin :branch_name

Additional Information: Git Branching | Git Documentation

Git Logs

Git Log shows commit logs. The Git documentation describes the git log “command takes options applicable to the git rev-list command to control what is shown and how, and options applicable to the git diff-* commands to control how the changes each commit introduces are shown.”

#! Show commit history in single lines
git log --oneline
#! Show commit history for last N commits
git log -2
#! Show commit history for last N commits with diff
git log -p -2
#! Show all local file changes in the working tree
git diff
#! Show changes made to a file
git diff myfile
#! Show who changed what & when in a file
git blame myfile
#! Show remote branches and their mapping to local.
git remote show origin

Additional Information: Git Log | Git Documentation

Git Commit

It is the process adding the code to the project (local repository). Before committing the code, it has to be in the staging area. It is important to add a commit message, for future reference, explaining what changes were made in a commit.

#! Commit changes along with a custom message
git commit -m "(message)"
#! Commit and add all changes to staging
git commit -am "(message)"
#! Switch to a commit in the current branch
git checkout <commit>
#! Show metadata and content changes of a commit
git show <commit>
#! Discard all changes to a commit
git reset –hard <commit>
#! Discard all local changes in the directory
git reset –hard Head
#! Show the history of changes
git log
#! Stash all modified files
git stash
#! Retrieve stashed files
git stash pop
#! Empty stash
git stash drop
#! Define a tag
git tag (tag_name)
#! Push changes to origin
git push

Additional Information: Git Commit | Git Documentation

Git Cleanup

The git-clean – Remove un-tracked files from the working tree. As described in the Git documentation, it “cleans the working tree by recursively removing files that are not under version control, starting from the current directory.Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.”

#! Delete all untracked files
git clean -f
#! Delete all untracked files and directories
git clean -df
#! Undo local modifications to all files
git checkout -- .
#! Unstage a file
git reset HEAD myfile

Additional Information: Git clean | Git Documentation

Git Tags

Git documentation describes “Git has the ability to tag specific points in a repository’s history as being important. Typically, people use this functionality to mark release points (v1.0, v2.0 and so on)”.

#! Get remote tags
git pull --tags
#! Switch to an existing tag
git checkout tag_name
#! List all tags
git tag
#! Create a new tag
git tag -a tag_name -m 'tag message'
#! Push all tags to remote repo
git push --tags

Additional Information: Tagging | Git Documentation

Git Stashes

Git documentation describes Git Stashing as “takings the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time”.

#! Save changes to a stash
git stash save 'stash_name' && git stash
#! List all stashes
git stash list
#! Apply a stash and delete it from stash list
git stash pop

Additional Information: Git Stashing | Git Documentation

Reverting Changes vs Resetting

To undo or roll back a commit, the difference between the revert vs reset should be understood.

git revert <commit>: Revert some existing commits. It creates a new commit undoing the changes but preserves the history.

git reset <commit>: Reset current HEAD to the specified state. Any changes position of HEAD giving a cleaner history (without previous commit history).

Additional Information: Git revert & Git Reset | Git Documentation

Wrapping Up

In this learning-note post, some common Git commands cheat sheet was assembled as a go to resource. This page is work-in-progress is updated constantly while doing the Git projects.

Next Post: Getting Started with a local Git Project & deploy to GitHub server

This post was inspired by Cody Arsenault’s Git Cheat Sheet – a Collection of the Most Useful Commands and some sections are directly adopted from his post.

Useful resources

While preparing this post, I have referred the following references extensively. Please refer to original posts for more detailed information.