My Git Cheat Sheet

I prefer to use git from the command line, rather than using a GUI-based tool.  Here are my favorite or most-used git commands.

Tools

  • Command line git – posh-git
  • Comes with Git for Windows – https://git-scm.com/download/win
  • After install, you have shortcut to “Git Shell”
  • NOTE: Tab for auto-completion in Git Shell

 

Setting up Kdiff3 as diff/merge tool

  • Download/install Kdiff3
  • Add to PATH: c:\Program Files\Kdiff3 (or equiv)
  • git config –global merge.tool kdiff3
  • git config –global diff.tool kdiff3

 

Cloning – get local of repo

 

List branches (local and remote)

  • git branch -a

 

Create new local branch

  • git branch 170502
  • git checkout -b 170502 master    // create new branch off specified branch (master)

 

Switch to new branch

  • git checkout 170502

 

Switch back to master (or another branch)

  • git checkout master

 

Push new branch (simple git push will suggest this)

  • git push –set-upstream origin 170502

 

Revert local changes (single file or all files)

  • git checkout HEAD <filename>
  • git checkout HEAD .

 

Doing diffs of local changes

  • git difftool <filename>              // diff local file to remote (same branch, if not staged)
  • git difftool master <branchname> — <filename>   // diff file across branches, remote copies
  • git difftool master — <filename>   // diff local file to different branch (remote)

 

Sequence for commit/push changes

  • git add .    — add file to list of things to be committed
  • git commit -m “some message” .    — commit to local repo
  • git push     — push to remote

To unstage a file (reverse of git add)

  • git reset <filename>

To merge changes back to master, from branch

  • On github.com site, find branch, do New Pull Request
    • This just shows all diffs, allows comments, but doesn’t yet create the pull request
  • Add title and comments
  • Review commits and changes
  • Create Pull Request
  • Verify no conflicts
  • (Can leave Pull Request sitting out there at this point)
  • Merge Pull Request
  • Can delete branch after merge (Trash icon in branch list)

 

Update local, after merge

  • git checkout master
    • Back to branch that you merged into
  • git pull
    • Update from remote (you get the just-merged changes)
  • git remote update origin –prune
    • Update local list of remote branches
  • git branch -d 170502
    • Delete local branch that you just merged

 

Switching to new branch that already exists on remote

  • git remote update origin –prune
    • Update local list of remotes
  • git checkout newbranch
    • Automatically set to track remote

 

Abandon a branch

  • git checkout master
    • Get off the branch
  • git branch -d mybranch
    • Delete local
  • Delete branch on github web site

 

Merging

  • Assume you’re working on a feature branch and want to periodically merge from master
  • git checkout master
  • git pull
  • git checkout mybranch
  • git merge master

Merge remote into local changes

  • E.g. if you’re on branch mybranch, have local changes not pushed, and there are other changes already pushed onto mybranch from elsewhere
  • git checkout mybranch
  • git fetch
    • Update remote tracking branches locally, e.g. origin/mybranch
  • git merge origin/mybranch
    • Merge from remote (tracking branch) into your branch
  • Resolve any merge conflicts

If you have conflict

  • git mergetool filewithconflict
  • Resolve in mergetool (e.g. KDiff3) and save
  • Note that merged file is now included in list of files to be committed and that .orig file exists as untracked file
  • Remove untracked orig files
    • Option 1
      • git clean -n
      • git clean -f
    • Option 2 – just delete files

 

See also: Git Cheat Sheat by Tower

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s