zsmith.co

GIT Quick Reference

Revision 4
© 2015-2018 by Zack Smith. All rights reserved.

GIT is a complicated tool and few people have perfect knowledge of it. Many aspects of its design seem arcane but because of technical debt and inertia, it's unlikely to be revised to be more understandable.

Common commands

To add a file git add fileName
To commit some changes locally git commit
To push changes to the master branch git push origin master
To push changes to a different remote branch git push origin branchName
To merge changes on master from some other person git pull origin master
To get the current status of local files git status
To get the change log for the branch git log
To get the change log for one file git log -- fileName
To add a tag git tag tagName
To push tags to the remote server git push --tags
To save local changes git stash
To recover and merge local changes git stash pop
To change branches git checkout branchName
To create a new branch git checkout -b newBranchName

Places

Files can be in 4 different places.

  1. Working directory.
  2. Staging.
  3. History.
  4. On a remote server.

The first three locations are on your machine. Git does not have to be used with a remote server.

Use git add to move a file from working directory to staging.

Use git commit to move a file from staging into history.

Use git push to move a file from history to the remote server.

States

Files can be in 5 different states.

  1. Untracked.
  2. Unmodified tracked.
  3. Modified tracked.
  4. Staged tracked. (Committed)
  5. Ignored.

The typical daily work process is:

  1. Use git pull to fetch others people's changes.
  2. Edit files.
  3. Use git add to move your changes from working directory to staging.
  4. Use git commit to move your changes from staging into history.
  5. Use git pull to fetch and merge others' changes into your copy.
  6. Use git push to send the merged changes from history to the remote server.

Check out/in

  git clone = does your initial checkout of a preexisting branch.
  git fetch = fetches remote changes but does not do a merge.
  git pull = fetches remote changes and then does merges those with your committed changes.
  git push = move changes from history to the remote server.

Diffing

  git diff = compares working to staged.
  git diff --staged = compares staged to history.
  git diff HEAD = compares working to both staged and history.
  git diff -w = ignores whitespace differences.
  git diff branch1..branch2 = prints differences between two branches.

Logs

  git log -- file = prints changes to one file.
  git log -S string = searches the log for a string.
  git log --decorate --graph = pretty prints the log.
  git log -G regex =

Branches

  git branch = shows all branches.
  git branch -r = shows remote branches.
  git branch name = creates a new branch.
  git branch name hashcode = creates a new branch at a specific checkin.
  git branch -m oldBranchName newBranchName = renames a branch.
  git checkout branch = switches to a branch.
  git checkout -b branch = creates and switches to specified branch.
  git branch -d branch = deletes a branch.
  git branch -D branch = forces the deletion of a branch.
  git remote add branchName URL = connects your branch to the remote server URL.
  git push -u origin branchName = sends your branch to the remove server.

File operations

  git mv file1 file2 = moves/renames a file within git.
  git rm file = removes both working and staged copies of a file.
  git ignore file = tells git to never check in a file.

Merging

  git add = marks a manually merged file as fixed.

Automatic merges sometimes introduce errors.

Rebasing

Just don't do it. It causes too many problems. If your work process requires rebasing on a frequent basis, you are doing something very wrong.

Submodules

  git submodule add URL = brings in a git repo.
  git submodule update --init --recursive = loads associated submodules recursively.
  git clone --recurse submodules URL = does what you think.

Stash stack

  git stash = saves current changes
  git stash pop = restores and merges stashed changes

Stashing is useful for doing manual merges.

Resetting

There are three levels of resets:

  git reset --soft
  git reset --mixed = (default)
  git reset --hard

git reset HEAD file undoes staged changes to a file.

git reset --hard HEAD~1; git push -f may undo the last push to the server.

Useful shell functions

  • function S { git status }
  • function C { git commit -m $1 }
  • function A { git add $1 }
  • function U { git pull }
  • function P { git push }