Branches in git is divided into 2 kind. Remote and local. I will demonstrate a basic scenario in this post regarding the local branch.
local branch
Scenario
You are working on your own branch while there is an issue shown up on production branch which need you to do a hotfix. Here is what you need to do.
switch to production branch create a issue branch and switch to it which allow you to start working on it and do some commits.
$ git checkout -b issue
if you want to do some other fix on the production branch now, you will need to commit all your current work on the issue branch and switch to production branch . At this point, your working directory is exactly the same before you start working on branch hotfix. When you switch branches, Git resets your working directory to look like it did the last time you committed on that branch.
then you can create a branch called hotfix and do your work there. Here is what it looks like now.
- you can run tests on the hotfix branch to see if it works fine together. Then switch back to master branch and merge the work that you have done on hotfix branch. You will notice the fast-forward notation. Because the commit c3 pointed to by the branch hotfix you merged in was was directly ahead of the commit c1 that you are on. So git will simply let master point to the same commit that hotfix point to. This is the so called fast-forward.
$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
index.html | 2 ++
1 file changed, 2 insertions(+)
after that, you do will not need the hotfix branch since the master branch already pointed to it. You will not need to branch pointing to the same commit.
$ git branch -d hotfix
Deleted branch hotfix (3a0874c).
- now you can switch back to issue branch and continue the previous work.
merge
Scenario
After you switch back to issue branch, you might want to integrate the work that you have done on the hotfix. So it comes to the merge.
- So we decided to issue work is done and ready to be merged into the master branch. Checkout master branch and merge issue branch.
- In this case, the master branch is not the direct ancestor branch you are working on which is issue branch. So git will create a new commit to be pointed by the master branch. Instead of just moving the branch pointer forward, Git creates a new snapshot that results from this three-way merge and automatically creates a new commit that points to it.
Now that we have your work done on issue branch merged with the master branch, we can delete the issue branch.
merge conflict
It is very common that different 2 branches change the same file differently. In this case, git will pause to let us handle the conflicts. Run git status
to see the files unmerged. Below is how the conflicts look like called standard conflict-resolution markers
.
<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53:index.html
The section between <<<<<<<
and =======
is the version that you change on the HEAD(the one you checkout) branch which is master in this case. The section between =======
and >>>>>>>
is the version belongs to the branch you are going to merge. We can solve the conflict manually by deciding which part to save. Then add the changed file to stage it. We can also use Intellij
or mergetool
to resolve it using graphical tool.
Once we add it, we can commit the changes using git commit
. It is always good idea to use git status
to check the conflicts.
Remote branches
a pointer to your remote repositories comparing to the local branches.
origin
git ls-remote [remote-name]
: displays references available in a remote repository along with the associated commit IDs.
git remote show [remote-name]
: displays the detail info of remote repository.
[remote-name]
in the above is equal to the name or alias that you gave to your remote repo, origin by default. We will discuss later. Remote branch take the form of (remote-name)/(branch-name)
. To see which remote branch you are currently tracking, use git branch -vv
to see also the last commit info.
When using git clone
git will automatically set up the remote-name as origin. To change it, use git clone -o [new-name]
.
When you merge a remote branch to the local one, will the remote branch disappear?
No, you will need to delete it manually by git push origin --delete [remote-branch-name]
.
Imagine we have a local branch master pointing to the commit of c3, however the remote branch master has been modified by others and is pointing to c4.
To synchronize your work, run git fetch origin
. This command looks up which server “origin” is and fetch data to update the local repo.
How git fetch updates your remote references: