Git cheat sheet

Russell Bateman
October 2022
last update:

Table of contents

Simple cheats
Advanced cheats
Useful links

Quick, simple and frequently used cheats around branching...

  1. List branches in a repository
    $ git branch
    
  2. Create new branch
    $ git checkout -b branch-name
    
  3. Switch to a branch
    $ git checkout branch-name
    
  4. Create new branch and switch to it
    $ git checkout -b branch-name
    
  5.  

    If you do work in a (new) branch, create new files or make changes in files, but fail to add/commit those files, then when you switch (or checkout) another branch, git will automatically merge those files to that branch.

    This is not often what you really want git to do. However, ...

    ...while this appears frustrating, any other strategy would result in the loss of your work on that (new) branch. You must at very least, add, then commit to that branch's local git repository (else how can you think git could keep it?)

     
     
  6. Once good changes are in that new branch, merge them into master
    $ git checkout master
    $ git merge branch-name
    
  7. Delete branch from repository once you've finished with it (if you want to)
    $ git branch -d branch-name
    

For example...

$ git branch
* master
$ git checkout -b generic-patient-ids
Switched to branch 'generic-patient-ids'
$ git branch
* generic-patient-ids
  master

  —do some work in the new branch, be certain to add and commit it

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch
* master
  generic-patient-ids
$ git merge generic-patient-ids
Updating ee385b2..7d87195
Fast-forward1
 pom.xml                                                      |  3 ++-
 src/main/java/com/acme/OrganizationUtilities.java            |  2 +-
 src/main/java/com/acme/Author.java                           | 10 +++++-----
 .
 .
 .
 src/test/resources/allergies.xml                             | 16 ++++++++--------
 src/test/resources/immunizations.xml                         |  4 ++--
 46 files changed, 359 insertions(+), 152 deletions(-)
 create mode 100644 src/main/java/com/acme/pojos/Id.java
 create mode 100644 src/test/java/com/acme/PatientRoleTest.java
$ git status
On branch master
Your branch is ahead of 'origin/master' by 11 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
$ git branch -d generic-patient-ids
Deleted branch generic-patient-ids (was 5b24e08).

1 Fast-forward means it's a blissful day because there are no conflicts you must resolve between your new code and something that changed in master while you were off working on that new code.

More advanced, esoteric, high-powered cheats...

  1. Don't track a file: add it to its own line in .gitignore
    $ vim .gitignore
    
  2. Stop tracking a file you accidentally committed
    $ git rm -r --cached filename
    
  3. Rewrite the last commit message
    $ git commit --amend -m "(Fix screwed-up commit message)"
    
  4. Add a file to the last commit as an afterthought
    $ git add filename
    $ git commit --amend --no-edit
    
  5. Zero out all changes make to a file
    $ git checkout filename
    
  6. Zero out all changes in an abortive working project (to the most recent commit in the repository)
    $ git restore .
    
  7. Restore a file to an old version back in time
    $ git log --name-status (record the commit hash)
    $ git restore --source commit-hash filename
    
  8. Restore a file you accidentally deleted
    $ git restore filename
    
  9. Discard all changes in your local repository and return to what's on the remote
    $ git reset --hard origin/master
    

Useful links