Skip to content

Instantly share code, notes, and snippets.

@bencooling
Last active December 25, 2015 03:29
Show Gist options
  • Save bencooling/6910048 to your computer and use it in GitHub Desktop.
Save bencooling/6910048 to your computer and use it in GitHub Desktop.
Useful Git commands

Branching

General

  • Master branch is automatically created by git init
  • When a branch is checked out, the HEAD is pointing to the branches latest commit
  • When multiple branches are merged back into a single branch, say the master, GIT will merge made by the 'recursive' strategy (GIT creates a common commit with multiple parents).

List branches

$ git branch # Current active branch has a star on it
$ git branch -a # all branch

Create and switch to new branch

$ git checkout -b [branch_name]

This is shorthand for

$ git branch [branch_name] # Create new branch
$ git checkout [branch_name] # Change to branch

Merge branch back to master

$ git merge [branch]
$ git merge --squash master

** Delete branch**

$ git branch -D [branch_name]

Run --merged option on the master/production branch to see what branches have been merged into the current branch. It's normally safe to delete these branch

$ git branch --merged

Branches requiring merging

$ git branch --no-merged

Clone specific branch

$ git clone -b simple ~/git/galleryneue/

Useful Git commands

Recover deleted file in Git repo

In this example, I am recovering sftp-config.json

$ git rev-list -n 1 HEAD sftp-config.json // c8a503af26d659bd182313677b35cdf8e5244afa
$ git checkout c8a503af26d659bd182313677b35cdf8e5244afa sftp-config.json

List files by commit

$ git diff-tree --no-commit-id --name-only -r a202accc5ffdcf50b81423d225a73edd8ac8293f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment