branch

Switch to a Branch

Master branch

switch to master

git checkout master
git checkout -f master //force to proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. or use git reset --hard

nonMaster branch

switch to another branch

git checkout feature/JIRA-12345

[Optional]Use the github GUI to create a new branch, or the following command:

# create branch 
git checkout -b [new branch name] 
eg: 
git checkout -b kaiMaster
with -b, this is shorthand for both:
git branch kaiMaster  //create new branch
git checkout kaiMaster  //switch to new branch. If just this alone, Local modifications to the files in the working tree are kept

# push to central repository and setup remote tracking
git push -u origin [new branch name]

Rename branch

assumes the current branch is the one being renamed

git branch -m [new branch name]

Delete branch

list local branches status

git branch
git branch -vv //list local branches status, still show my junk local branches. same as git branch without any option

update from remote

git pull --prune  //only delete remote-tracking branches, not local branches.
git fetch --prune //try this
git fetch --all -p // update local branches status
git branch --merged //lists branches which were merged, but not every of them was removed remotely.

delete a local branch that has already been merged to master

git branch -d [your branch name]

delete a local branch that has NOT been merged and will be abandoned

git branch -D [your branch name]

delete a remote branch

git push origin --delete [your branch name]

Clean-up Remote Branch References

If you're noticing a large amount of remote repository references that don't actually exist anymore, run the following command to prune those branches:

git fetch --prune

Clean-up Local Branches Merged To Master

The following command will delete all local branches that have merged to master.
Alias this within your terminal for easy access.

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

Merge Branches

check out the branch you'd like to merge into

git checkout [destination branch]

merge the branch into the current branch

git merge [your branch]

results matching ""

    No results matching ""