Git Cheat Sheet (Git Commands)

Git is a version control system that is widely used for software development and other version control tasks. It is a distributed revision control system with an emphasis on speed, data integrity, and support for distributed, non-linear workflows. Git was initially designed and developed in 2005 by Linux kernel developers (including Linus Torvalds) for Linux kernel development.

Git GUI client for all major platform (Windows, Linux, OS-X etc) are available on the official Git SCM web site.

Getting Started

Configure user information for all local repositories
git config --global user.name "[name]" Sets the name you want atached to your commit transactions
git config --global user.email "[email address]" Sets the email you want atached to your commit transactions
git config --global color.ui auto Enables helpful colorization of command line output

Start a new repository or obtain one from an existing URL
git init [project-name] Creates a new local repository with the specified name
git clone [url] Downloads a project and its entire version history

Working with Local Branch

Review edits and craf a commit transaction
git log See all commits
git log --no-merges [branch-name].. See only changes made on particular branch
git log --pretty=format:"%h %s" --graph Pretty commit view
git status -s Lists all new or modified files to be commited
git status Short view of status
git diff Shows file differences not yet staged
git add [file] Snapshots the file in preparation for versioning
git add . Add all modified files to be commited
git add '*.txt' Add only text files
git diff --staged Shows file differences between staging and the last file version
git reset [file] Unstages the file, but preserve its contents
git commit -m "[descriptive message]" Records file snapshots permanently in version history

Name a series of commits and combine completed efforts (Branching)
git branch Lists all local branches in the current repository
git branch [branch-name] Creates a new branch
git checkout [branch-name] Switches to the specified branch and updates the working directory
git checkout -b [new-branch-name] Checkout current branch into a new branch
git merge [branch] Combines the specified branch’s history into the current branch
git branch -d [branch-name] Soft deletes the specified branch
git branch -D [branch-name] Hard deletes the specified branch

Comparing changes
git diff See current changes, that have not been staged yet.
git diff HEAD See current changes, that have not been commited yet (including staged changes)
git diff branch-name Compare current branch to some other branch
git diff [first-branch]...[second-branch] Compare two different branches
git show [commit] Outputs metadata and content changes of the specified commit

Erase mistakes and craf replacement history
git reset [commit] Undoes all commits afer [commit], preserving changes locally
git reset --hard [commit] Discards all history and changes back to the specified commit

Shelve and restore incomplete changes
git stash Temporarily stores all modified tracked files
git stash pop Restores the most recently stashed files
git stash list Lists all stashed changesets
git stash drop Discards the most recently stashed changeset

Working with Remote Branch


Manage Remote Branch
git remote See list of remote repos available.
git remote -v Detailed view of remote repos, with their git urls
git remote add origin [https://some-git-remote-url] Add a new remote. I.e. origin if it is not set

Synchronize Changes
git push Push current branch to remote branch
git push [alias] [branch] Uploads all specific local branch commits to remote
git fetch Downloads all history from the remote repository
git branch -a See the list of all remote branches
git pull Just like pushing, you can get the latest updates from remote.
git pull [alias] [branch] Pull a specific branch

Reference


You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.