DM
Technical reference

Git Cheatsheet

Version control for code

Getting Started

Create a Repository

Create a new local repository

$ git init [project name]

Clone a repository

$ git clone git_url

Clone into a specific directory

$ git clone git_url my_directory
Make a change

Show file status

Show modified files in working directory, staged for your next commit

$ git status

Stage a file

Stages the file, ready for commit

$ git add [file]

Stage all changes

Stage all changed files, ready for commit

$ git add .

Commit staged files

Commit all staged files to versioned history

$ git commit -m "commit message"

Commit tracked files

Commit all your tracked files to versioned history

$ git commit -am "commit message"

Discard unstaged changes

Discard changes in working directory which is not staged

$ git restore [file]

Unstage a file

Unstage a staged file or file which is staged

$ git restore --staged [file]
Configuration

Set global username

Set the name that will be attached to your commits and tags

$ git config --global user.name "name"

Set global email

Set an email address that will be attached to your commits and tags

$ git config --global user.email "email"

Enable Git color

Enable some colorization of Git output

$ git config --global color.ui auto

Edit global config

Edit the global configuration file in a text editor

$ git config --global --edit
Working with Branches

List local branches

$ git branch

List all branches

$ git branch -av

Switch to branch

Switch to my_branch, and update working directory

$ git switch my_branch
Observe your Repository

Show commit log

Show the commit history for the currently active branch

$ git log

Compare branches

Show the commits on branchA that are not on branchB

$ git log branchB..branchA

Remote & Sync

Synchronize

Fetch remote branches

$ git fetch [alias]

Merge remote branch (no fast-forward)

$ git merge --no-ff [alias]/[branch]

Merge remote branch (fast-forward only)

$ git merge --ff-only [alias]/[branch]

Push local commits

$ git push [alias] [branch]

Pull changes from remote

$ git pull

Cherry-pick a commit

Merge a specific commit into your current branch

$ git cherry-pick [commit_id]
Remote

Add remote alias

$ git remote add [alias] [url]

List remotes

$ git remote

List remotes with URLs

$ git remote -v

Remove remote

$ git remote rm [remote repo name]

Change remote URL

$ git remote set-url origin [git_url]
Temporary Commits

Stash changes

$ git stash

List stash stack

$ git stash list

Apply top stash

$ git stash pop

Drop top stash

$ git stash drop
Tracking path Changes

Remove file from project

$ git rm [file]
Ignoring Files

Example .gitignore file

/logs/*
!logs/.gitkeep

# Ignore Mac system files
.DS_Store

Git Tricks

Rename branch

Rename local branch

$ git branch -m <new_name>

Push renamed branch

$ git push origin -u <new_name>

Delete remote branch

$ git push origin --delete <old>
Rewriting history

Rewrite last commit message

$ git commit --amend -m "new message"
Log

Search log by term

$ git log -S'<a term in the source>'

Log changes to file

$ git log -p <file_name>

Graph commit history

$ git log --pretty=oneline --graph --decorate --all
Branch

List branches with upstreams

$ git branch -vv

Switch to previous branch

$ git checkout -

List remote branches

$ git branch -r

Checkout file from branch

$ git checkout <branch> -- <file>
Git Aliases

Git Aliases

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status