How To: Work with (local) branches
related commands:
branch checkout merge rebase
A single Git repository can maintain a lots of development branches. You want to try an idea, make some experimental changes, fix a bug out of master, or just try a different way without sharing it? Create a new branch!
In a branch, you can work exactly as if you were in the ‘master’: add, commit, rm, mv, etc… but until you explicitly say it, a local branch won’t be tracked by remote repositories.
Create
How to create a branch and go in it:
$ git branch <branch_name>
$ git checkout <branch_name>
or the short way:
$ git checkout -b <branch_name>
If you want to know tracked branches, just do:
$ git branch # local
$ git branch -r # remote
$ git branch --all # all
Merge
This is a small introduction about merging branches.
Your developments in a branch is fine, so you want to add it in the ‘main’ branch or in a remote branch. It’s called a merge and you must be very carefull when you do it.
As example, let’s say that your local branch is named “experimental” and the remote branch is “master”, and we want to merge “experimental” in “master”.
First, you must answer this question: “Is experimental a usefull branch?” or with other words, “Do I want to keep this branch in logs?”.
If “yes”, you just have to do:
$ git checkout master
$ git merge --no-ff experimental
# or
$ git merge --no-ff experimental -m "merge commit message"
The --no-ff
option ensures that your branch will remain in logs, even if ‘master’ doesn’t have new commits.
If “no”, and ‘master’ has not new commits while your devs in ‘experimental’, just do a simple merge
:
$ git checkout master
$ git merge experimental
If “no” and ‘master’ has new commits, you will have to rebase
your branch before merging:
$ git checkout experimental
$ git rebase master experimental
$ git checkout master
$ git merge experimental
I’ll explain it in details in a future post, but just a little word about rebase
: with this command, you will “move” your branch start, by adding all missing commits from ‘master’.
So, for those who say that
merge
andrebase
do the same, it’s wrong!! at least if you want to keep a clean log…
Delete
Your idea was wrong and/or the branch is not needed anymore: delete it.
$ git branch -D <name>
Reminder
branch
Create a branch
$ git branch <name>
List branches
$ git branch # local branches $ git branch -r # remotes branches $ git branch --all # all tracked branches
Delete a merged branch
$ git branch -d <name>
Delete a useless branch
$ git branch -D <name>
checkout
Go to parent commit of a previous commit
$ git checkout <hash>^
Go to parent commit of a tag
$ git checkout <tag>^
Go to “grand parent” of a tag/commit
$ git checkout <object>^^
Create a branch and navigate to it
$ git checkout -b <new_branch>
Create a tracked branch and navigate to it
$ git checkout --track <remote>/<branch>
Create a branch, track it with a different name and navigate to it
$ git checkout -b <local_branch> <remote>/<remote_branch>
merge
Merge <branch> in current branch
$ git merge <branch>
Merge <branch> in current branch with ensure that it will remain in logs
$ git merge --no-ff <branch>
Merge <branch> in current branch with ensure that it will remain in logs with a message for the merge commit
$ git merge --no-ff <branch> -m "message"
rebase
Apply commits from <branch> to current branch
$ git rebase <branch>
Modify commits history from the commit <hash>
$ git rebase -i <hash>^