Branching in Git

Creating Branches

# create a new local branch then switch to it (based on current branch)
$ git branch Trav-New-Branch$ git checkout Trav-New-Branch

# or do both operations at once (based on current branch)
$ git checkout -b Trav-New-Branch

If you want to create a branch using another branch as a starting point, then do the following:

# create Trav-Cherry-Pick from the local Trav-New-Branch
$ git checkout -b Trav-New-Branch Trav-Cherry-Pick

# Create Trav-Cherry-Pick from the remote Master branch
$ git checkout -b Trav-Cherry-Pick origin/Master

Delete a Branch

Keep in mind that local and remote branches are completely separate entities in Git. If there are two related branches, one local and one remote, deleting one will not delete the other.

# Delete a branch, but leave files on disk
$ git branch -d Trav-Cherry-Pick

# Delete a branch, and also delete local files on disk
$ git branch -D Trav-Cherry-Pick

# Delete a remote branch
$ git push origin --delete Trav-Cherry-Pick

List Branches

$ git branch -a

Local branches are normal colour, remote branches are red.

To Set the Upstream Branch in Git

$ git branch --set-upstream-to=origin/Trav-Test-To-Master Trav-Test-To-Master

or you can do it in one step when you checkout the branch

$ git checkout --track origin/Trav-GlobalRA

which will create a local branch named Trav-GlobalRA, and set it to track the remote branch origin/Trav-GlobalRA.



Categories: Development, Git

Tags: ,

Leave a comment