Skip to content

Instantly share code, notes, and snippets.

@michailb
Forked from juristr/git-cheat-sheet.md
Created September 2, 2013 19:53
Show Gist options
  • Save michailb/6416693 to your computer and use it in GitHub Desktop.
Save michailb/6416693 to your computer and use it in GitHub Desktop.

Git Cheat Sheet

Committing and Undoing

Adding file for committing

$ git add <filename>

or

$ git add .

Executing Commit

$ git commit -m "This is my comment"

or adding + committing in one

$ git commit -a -m "My comment"

Undoing all changes

Reverts all changes to the last commit

$ git reset --hard

Removing a tracked file

To remove a tracked file from git but not from the local file system use

git rm --cached <filename>

Merging

Merge single file(s)

$ git checkout <local-branch-name> <file-path>

Update from original repo

$ git remote add upstream <org_git_url>

Then you can fetch it normally through

$ git pull upstream <branch>

Branches

Docs: http://gitref.org/branching/

List all branches

$ git branch -v

Create Branch

Checks out a branch and automatically switches to it.

$ git checkout -b <new-branch-name>

Download a remote branch

$ git checkout -b local-branch-name origin/remote-branch-name

Publish a local branch

$ git push -u origin <local-branch-name>

Delete local branch

$ git branch -D bugfix

Delete remote branch

$ git push origin :<remote-branch-name>

Delete tags

Local tags

git tag -l | xargs git tag -d

Remote tags

$ git ls-remote --tags origin | awk '/^(.*)(\s+)(.*[0-9])$/ {print ":" $2}' | xargs git push origin

Sync with a remote fork

First add a "remote" to the original fork

git remote add upstream https://github.com/otheruser/repo.git

...then you can update just normally using

git pull upstream master

Submodules

Initialize

To initialize and update existing submodules simply use

git submodule init
git submodule update

Update

To update all submodules in the sense of performing a git pull on all of them you can use

git submodule foreach git pull

which will iterate through each of the submodule.

Git Configuration

Pretty logs

Add an alias to your ~/.gitconfig like

[alias]
  lg = log --graph --full-history --all --color --pretty=tformat:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s%x20%x1b[33m(%an)%x1b[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment