This cheat sheet features the most important and commonly used Git commands for easy reference.
- SETUP
- UNSET
- SETUP & INIT
- STAGE & SNAPSHOT
- BRANCH & MERGE
- INSPECT & COMPARE
- SHARE & UPDATE
- TRACKING PATH CHANGES
- REWRITE HISTORY
- TEMPORARY COMMITS
- IGNORING PATTERNS
Configuring user information used across all local repositories
-
Set a name that is identifiable for credit when review version history
git config --global user.name "[UserName]"
-
Set an email address that will be associated with each history marker
git config --global user.email "[valid-email]"
-
Set automatic command line coloring for Git for easy reviewing
git config --global color.ui auto
Removes user information configuration across all local repositories
-
UnSet a name
git config --global --unset user.name
-
UnSet an email address
git config --global --unset user.email
Configuring user information, initializing and cloning repositories
-
Initialize an existing directory as a Git repository
git init
-
Retrieve an entire repository from a hosted location via URL
git clone [url]
Working with snapshots and the Git staging area
Shows modified files in working directory, staged for your next commit
git status
It adds changes to staging area. This command stages the changes made to files and prepares them for the next commit.
-
To add a specific file
git add [fileName]
-
To add all the files that are new or changed in the current directory
git add .
-
To add all changes in the entire working tree, from the root of the repository, regardless of where you are in the directory structure
git add -A
It resets changes in the working directory.
git reset [file]
- When used with –hard HEAD, this command discards all changes made to the working directory and staging area and resets the repository to the last commit (HEAD).
git reset --hard HEAD
It shows changes between commits, commit and working tree, etc. It also compares the branches.
-
Shows the difference the working directory and the last commit
git diff
-
Shows the difference between the last and second last commits
git diff HEAD-1 HEAD
-
Compares the specified branches
git diff [branch-1] [branch-2]
-
Shows the difference of what is staged but not yet committed
git diff --staged
Isolating work in branches, changing context, and integrating changes
It lists, creates or deleyes branches in a repository. To delete the branch using git checkout and then run command to delete the branch locally
-
To list branches
git branch
-
To create a new branch
git branch [branch-name]
-
To delete a branch
git branch -d [branch-name]
Switch to another branch and check it out into your working directory
git checkout [branch-name]
Merge the specified branch's history into the current one
git merge [branch]
Examining logs, diffs and object information
-
Show all commits in the current branch's history
git log
-
Show the commits on branchA that are not on branchB
git log [branchB]..[branchA]
-
Show the commits that changed file, even across renames
git log --follow [file]
-
Show the diff of what is in branchA that is not in branchB
git diff [branchB]...[branchA]
-
Show any object in Git in human-readable format
git show [SHA]
Retrieving updates from another repository and updating local repos
-
Add a git URL as an alias
git remote add [alias] [url]
-
Fetch down all the branches from that Git remote
git fetch [alias]
-
Merge a remote branch into your current branch to bring it up to date
git merge [alias]/[branch]
-
Transmit local branch commits to the remote repository branch
git push [alias] [branch]
-
Fetch and merge any commits from the tracking remote branch
git pull
Versioning file removes and path changes
-
Delete the file from project and stage the removal for commit
git rm [file]
-
Change an existing file path and stage the move
git mv [existing-path] [new-path]
-
Show all commit logs with indication of any paths that moved
git log --stat -M
Rewriting branches, updating commits and clearing history
-
Apply any commits of current branch ahead of specified one
git rebase [branch]
-
Clear staging area, rewrite working tree from specified commit
git reset --hard [commit]
Temporarily store modified, tracked files in order to change branches
-
Save modified and staged changes
git stash
-
List stack-order of stashed file changes
git stash list
-
Write working from top of stash stack
git stash pop
-
Discard the changes from top of stash stack
git stash drop
Preventing unintentional staging or commiting of files
-
Save a file with desired patterns as .gitignore with either direct string matches or wildcard globs.
logs/ *.notes pattern*/
-
System wide ignore pattern for all local repositories
git config --global core.excludesfile [file]