Practical Git Cheat Sheet

Working with repositories

Create a repository from an existing directory

$ git init

Clone a repository

$ git clone [URL]

Making Changes

Show current status of the local repository

$ git status

Show changes since last commit in the local repository

$ git diff

Discard all local unstaged changes

$ git checkout .

Discard changes in an unstaged file

$ git checkout [path-to-file]

Discard all untracked files

$ git clean -f

Staging

Add all files in current folder and subfolders to staging

$ git add .

Add specific file to staging and prepare for committing

$ git add [path-to-file]

Only add tracked files to staging

$ git add -u

Remove a file from staging

$ git reset --[path-to-file]

Committing

Commit staged changes

$ git commit -m "[message]"

Add staged changes to previous commit

$ git commit --amend

Excluding files from Git

Create a .ignore file, if does not exist

$ touch .gitignore

Add the file to .ignore file

$ echo [path-to-file] >> .gitignore

Branching

List all local branches

$ git branch

List all remote branches

$ git branch -r

Create a new branch

$ git branch [branch-name]

Switch to a branch

$ git checkout [branch-name]

Move current changes to a new branch

$ git checkout -b [branch-name]

Merge a specific branch into the current branch

$ git merge [branch-name]

Delete a branch locally

$ git branch -d [branch-name]

Delete a branch on remote

$ git push --delete [remote-name] [branch-name]

Rename the current local branch

$ git branch -m [new-name]

Rename a different local branch

$ git branch -m [old-name] [new-name]

Working with remotes

Push all local branch commits to remote

$ git push [remote-name] [branch-name]

Download history from remote

$ git fetch

Update local branch with commits from remote

$ git pull

List all the remotes

$ git remote -v

Change URL of a remote

$ git remote set-url [remote-name] [new-url]

Rename a remote

$ git remote rename [current-remote-name] [new-remote-name]

Remove a remote

$ git remote rm [remote-name]

Version history

Show commit history of this branch

$ git log

Show commit history of a specific file including renames

$ git log --follow [path-to-file]

Subscribe via RSS