Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git: Cheat Sheet

Git Cheat Sheet

NOTE: Anything in angle brackets like <this> should be replaced with your own value

Configuration

Global ignore file
git config --global core.excludesfile ~/.gitignore_global
More about ignoring files and sensible defaults for ~/.gitignore

Set the default editor
git config --global core.editor "nano"

The Basics

git status See what the current situation is

git add . Add all files in the current directory to the index

git add <filename> Add a single file to the index

git rm <filename> Remove a deleted file from the index

git commit -m "<your message>" Commit your changes

git checkout -b <new branch> <existing branch> Create a new branch

git diff <filename> See what has changed in a file since the last commit

git merge --no-ff <branch name> Merge another branch into the current branch

git push <remote> <branch> Push your changes up to the server

git pull <remote> <branch> Pull changes down from the server

Damn useful commands

  • Selectively add parts of a file to the index

      git add -p <filename>
    
  • Remove a file from the index while keeping changes

      git rm -r --cached <filename>
    
  • When you realise you have been working on the wrong branch

      git stash
      git checkout other-branch
      git stash pop
    

    You can take this further and apply your stash to a new branch:

      git stash branch <branchname> [<stash>] 
    

    This creates a new branch starting at the commit at which the stash was created, checks out the branch, and applies the stash.

  • Undo last commit (keep changed files)

      git reset --soft HEAD^
    

    (Add further ^ to step back more than 1 commit)

  • Undo last commit (discard changed files)

      git reset --hard HEAD^
    

    (Add further ^ to step back more than 1 commit)

  • Undo a specific commit

    • Identify the hash of the commit

        git log --graph
      
    • Revert the commit

        git revert <commit hash>
      

      This produces another commit. To exit Vim after adding the commit message,

        [Ctrl] + [C]
        :wq [Enter]
      

      If you just want the modified files but not the auto-commit

        git revert -n <commit hash>
      
  • Ignore files that already exist in the repository

      git update-index --assume-unchanged <filename>
    

    You won’t be able to update that file until you do:

      git update-index --no-assume-unchanged <filename>
    
  • Delete a local branch

              git branch -d <branch>
    
  • Track a remote branch

      git push -u <remote> <branch>
    
  • Track a remote branch (including tags)

      git push -u --follow-tags <remote> <branch>
    
  • Delete a remote branch

      git push <remote> --delete <branchName>
    
  • Recover a deleted branch

      git checkout -b <branch> <sha>
    

    Where <sha> is the tip of your deleted branch (use gitref to find the hash)

  • Generate a changelog

      git log --oneline --no-merges <last tag>..HEAD
    

Safe Branching Model

Two main branches:

master
develop

Create a feature branch:

git checkout -b myfeature develop

Incorporate a finished feature into develop:

git checkout develop
git merge --no-ff myfeature
git branch -d myfeature
git push origin develop

Create a release branch:

git checkout -b release-1.2.0 develop

Modify a file in some way (ie change version number)

git commit -a -m "Bumped version number to 1.2.0"

This is a good time to update the changelog with an overview of features and bugfixes in this release.

Finishing a release branch:

git checkout master
git merge --no-ff release-1.2.0
git tag -a v1.2.0 -m "Version 1.2.0"

If your project has a package.json, a better way to bump the version number and create a tag is to use:

npm version <major|minor|patch>

Merge changes back into develop:

git checkout develop
git merge --no-ff release-1.2.0
git branch -d release-1.2.0

Creating a hotfix branch

A hotfix is a bugfix that must be urgently pushed to production. We want to avoid introducing further bugs from untested code, so we only work on the fix from the master branch:

git checkout -b hotfix-1.2.1 master

Modify a file in some way

git commit -a -m "Fixed important thing that broke. Derp."

Finishing a hotfix branch:

git checkout master
git merge --no-ff hotfix-1.2.1
git tag -a v1.2.1 -m "Version 1.2.1"
# Or preferably:
npm version patch

Merge changes back into develop:

git checkout develop
git merge --no-ff hotfix-1.2.1

The one exception to the rule here is that, when a release branch currently exists, the hotfix changes need to be merged into that release branch, instead of develop.

git branch -d hotfix-1.2.1

Deployment

Set up the folder structure on the server:

mkdir myproject
cd myproject
mkdir repo
mkdir live

Initialise the repo:

cd repo
git init --bare

Add a post receive hook to make the changes live:

cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE="../live" git checkout -f

Make the hook executable:

chmod +x hooks/post-receive

Add the remote to your local repo:

git remote add web "ssh://user@example.com/~/myproject/repo/"

You are now ready to push. The first time, you will need to add the branch:

git push web +master:refs/heads/master

After that, you can just:

git push web master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.