Git cheat sheet and aliases


This post features the most important and commonly used Git commands for easy reference, plus some useful Git aliases.

Configuration

Set your user name globally.

git config --global user.name "[first_name last_name]"

Set your email address globally.

git config --global user.email "[email_address]"

Set automatic command line coloring for Git for easy reviewing.

git config --global color.ui auto

List all your Git aliases.

git config --get-regexp ^alias\.

Setup

Initialize an existing directory as a Git repository.

git init

Retrieve an entire repository from a hosted location via URL.

git clone [url]

Stage and Snapshot

Show modified files in working directory.

git status

Add a file as it looks now to stage for your next commit.

git add [file]

Add all files as they look now to stage for your next commit.

git add .

Remove a file from the working directory and the repository, staging the deletion.

git rm [file]

Moves or renames a file or directory in your repository.

git mv [file_or_directory]

Put a file out of stage while retaining the changes in working directory.

git reset [file]

Put all files out of stage.

git reset .

Discard changes made to a file.

git checkout [file_name]

Discard changes made to all files.

git checkout .

Difference of what is changed but not staged.

git diff

Difference of what is staged but not yet in commit.

git diff --staged

Commit your staged content as a new commit snapshot.

git commit -m "[meaningful_message]"

Store

Store modified and staged changes (stack)

git stash

List stashed file changes in stack order.

git stash list

Get file changes from top of the stack.

git stash pop

Discard changes from top of the stack.

git stash drop

Branch and Merge

List branches. An asterisk will appear next to the current branch.

git branch

Create a new branch at the current commit.

git branch [branch_name]

Switch to another branch and checkout the working directory.

git checkout [branch_name]

Create a new branch and switch to it.

git checkout -b [branch_name]

Merge the specified branch history into the current one.

git merge [branch]

Inspect and Compare

Show the commit history for the currently active branch.

git log

Show the commits in branch_a that are not in branch_b.

git log branch_b..branch_a

Show the commits that changed file, even across renames.

git log --follow [file]

Show the difference of what is in branch_a but not in branch_b.

git diff branch_b..branch_a

Show the details of the current commit, including its changes.

git show

Show the details of the specified commit, including its changes.

git show [commit_sha]

Share and Update

Add a Git URL as an alias. The most common alias is origin.

git remote add [alias] [url]

Fetch down all the branches from the remote repository.

git fetch

Fetch and merge any commits from the tracking remote branch.

git pull

Merge a remote branch into your current branch to bring it up to date.

git merge [alias]/[branch]

Push local branch commits to the remote repository branch.

git push

Push local branch commits to a new branch in the remote repository.

git push -u [alias] [branch]

Rewrite History

Apply any commits of current branch ahead of the specified one.

git rebase [branch]

Clear staging area, rewrite working tree from specified commit (rollback)

git reset --hard [commit]

Include staged changes to the last commit.

git commit --amend

Making changes to a past commit (not recommended when there are more people working on the branch the commit came from)

  1. Perform an interactive rebase of the commit to be edited.
git rebase -i [commit_sha]~
  1. Change the commit you want to edit from pick to edit.
edit 8a62da1 commit_to_edit
pick 9ee3178 next_commit
pick 9cca210 next_commit
  1. Once you have made your changes, add them to the stage and perform an amend.
git add .
git commit --amend
  1. Finish the rebase.
git rebase --continue
  1. If the remote repository has been pushed previously, a new push will need to be forced.
git push --force

Useful Git Aliases

List aliases shortcut.

git config --global alias.aliases "config --get-regexp '^alias\.'"

List all global configurations shortcut.

git config --global alias.g "config --global -l"

Status shortcut.

git config --global alias.s "status"

Commit shortcut.

git config --global alias.c "commit -m"

Diff shortcut.

git config --global alias.d "diff"

Push shortcut.

git config --global alias.p "push"

Logs with pretty format graph shortcut.

git config --global alias.l "log --graph --pretty=format:'%C(red)%h%C(reset) -%C(yellow)%d%C(reset) %s %C(green)(%cr) %C(magenta)<%an>%C(reset)' --abbrev-commit --date=relative"
2024-10-04
Written by Samuel de Vega.
Tags