Skip to content

Instantly share code, notes, and snippets.

@acanimal
Last active February 21, 2017 14:18
Show Gist options
  • Save acanimal/3c9e2568a93e1e6a01a7 to your computer and use it in GitHub Desktop.
Save acanimal/3c9e2568a93e1e6a01a7 to your computer and use it in GitHub Desktop.
Git Tips

Download a given pull request:

  • Download the PR specified by the ID into a new branch BRANCHNAME: $ git fetch origin pull/ID/head:BRANCHNAME
  • Switch to the branch: $ git checkout BRANCHNAME

Checkout a file from another branch (overwrite file):

You are on a branch branch1 and want to overwrite file file with the contents of the same file in branch branch2:

$ git checkout branch1          # Go to branch1
$ git checkout branch2 file     # Checkout the file from branch2

Recover a deleted file

  • Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it: git rev-list -n 1 HEAD -- <file_path>.
  • Then checkout the version at the commit before, using the caret (^) symbol: git checkout <deleting_commit>^ -- <file_path>.

Compare files from different commits/branches

$ git diff branch1 branch2 -- file

Upload local branch to a remote but into a new branch

$ git push origin local_name:remote_name

Show files changed on each commit

$ git log --pretty=oneline --name-status

How to know which tracks each branch

$ git branch -vv # doubly verbose!

How to change the remote branch tracking a local branch

Suppose you are working the local branch A you want the tracking remote to be B:

$ git branch local_branch --set-upstream-to remote/remote_branch

Delete a branch both on local and remote

To remove a local branch from your machine:

$ git branch -D my-local-branch

To remove a remote branch from the server:

$ git push origin :{the_remote_branch}

Delete a tag both on local and remote

$ git tag -d release01
$ git push origin :refs/tags/release01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment