Pull a pull request from remote to local and put it under a new branch

# Assume the 'remote' is named 'origin'
git fetch origin pull/ID/head:NEW_BRANCH_NAME
 
# Can use 'git checkout NEW_BRANCH_NAME' switch to the new branch

To push the current branch (committed) to a new branch at the remote

# Create a new branch at the remote, then push to it.
git push origin NEW_BRANCH_NAME

The remote refs/pull/… namespace is read-only. You can’t push any commits to it.

Branch and Remote Management

List local git branches

git branch
 
# -vv will show more the remote tracking branch of all the local branches
git branch -vv

List remote git branches

git branch -r
# `-r` is also used when deleting a remote branch.

List all git branches, all remotes + local

git branch -a

Delete a local git branch

git branch -d BRANCH_NAME

Delete a local branch which have already been merged into master

(reference: http://stevenharman.net/git-clean-delete-already-merged-branches)

$ git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d

Delete a specific remote branch (note, we need the name of the ‘remote’)

git branch -r -d origin/BRANCH_NAME

Remove a branch from a remote named foo

# Prune all the local branches tracking deleted remote branches on the remote 'foo'
# I.e.:
#   local branch: Exist
#   remote branch: Delete
#   remote name: foo
git fetch --prune foo 
 
# If the branch has already been removed in the `remote`, like github, you can:
# I.e. prune local branches for all remotes.
git fetch --all --prune

Assign/set a remote branch for a local branch

git branch -u NEW_REMOTE_BRANCH_NAME
# Another way
git push -set-upstream <remote name> NEW_REMOTE_BRANCH_NAME

$show-remotes Check the remotes registered on your local repo

# This will show the URLs of your remotes
git remote -v
 
# This does not show the URLs
git remote

Change the URL of a remote (assume origin is the name of the remote)

git remote set-url origin https://github.com/<user name>/<repo>.git
 
# From https to git (i.e. ssh)
git remote set-url origin git@github.com:<user name>/<repo>.git

Add a remote

git remote add <remote_name> <URL>
# Example:
# git remote add foo-shaderc https://github.com/foo/shaderc.git

Checkout a branch from a specific remote $checkout-remote-branch-and-make-a-local-branch-with-a-branch-name

git checkout -b <branch_name> <remote_name>/<remote_branch_name>
# Example:
# git checkout -b foo-master foo-shaderc/master

Attach a existing local branch with a remote branch

  • First, checkout the local branch first.
git branch -u <remote_name>/<remote_branch_name>
# Example
# git branch -u foo-shaderc/master

Note:

If the remote_branch_name does not match with the local_branch_name:

git push will not work automatically. It will complain there is no simple match between local branch and remote branch. In this case:

git push <remote_name> <remote_branch_name>
 
# But non-master -> 'master' case, is an exception.
# For 'master', 'HEAD' is required
git push <remote_name> HEAD:master

After using git init on a local directory, there is no ‘default’ remote added for that git directory (of course, since the git directory is not cloned from a remote). You need to manually add the ‘remotes’ for it.