Work with branches in git
This is my first article in a serie of useful tips on working with git. git is a very popular tool for collaborating on projects these days. git is a quite simple tool to use but there is still a lot of topics difficult to understand for non-experts.
I would personally recommend the book Git Pocket Guide: A Working Introduction. This book have lit a light for me on some nontrivial aspects on git like branches.
Let say you just started working on empty repo and you are working on your local branch. In my case initial-branch.
Let’s list local branches in the repo:
git branch --list
Here my output:
* initial-branch
We can also list remote branches (remote-tracking branches are local references to branches in remote repository)
git branch -r
Your collegue has created new branch and he wants you to review his code. Let’s say he created a branch full-pipeline. We need to update our local branches to get information about branches in remote repo. Use command
git fetch
Output will be like this
From gitlab.com:ximilar/customers/idtotal* [new branch] full-pipeline -> origin/full-pipeline* [new branch] master -> origin/master
We see newly published branch full-pipeline. Switch to this new branch
git checkout full-pipeline
Check the active branch
git branch
output is
* full-pipelineinitial-branch
And list remote branches
git branch -r
Make some changes and initiate a commit
git commit -s -m 'Add fixes'
Then push changes to new branch:
git push origin initial-branch
Tracking upstream branch
There is one more thing. Tracking upstream branch ( https://mincong-h.github.io/2018/05/02/git-upstream-tracking/).
https://gist.github.com/hackjutsu/33b970dd117889485491c018543e5118
When using GitLab and create own branch we can
git pull
and get message
There is no tracking information for the current branch.Please specify which branch you want to merge with.See git-pull(1) for details.git pull <remote> <branch>If you wish to set tracking information for this branch you can do so with:git branch --set-upstream-to=origin/<branch> rotation
So let’s set upstream branch
git branch --set-upstream-to=origin/rotation rotation
This sets the default remote branch for the current local branch
Now git pull returns
Updating 2c0060b..6ab57f1Fast-forwardximilar/idtotal/text_reader.py | 4 ----1 file changed, 4 deletions(-)
Keep git in actual state
You are working on own branch. You have done before some time a merge with master branch. Colleagues have made some edits in your files created in your new branch. In this case you need to actualize your own branch to “master” so you have got all the changes made in master branch
git fetch origin master:master
and merge the master branch with your branch
git merge master
Then simply git commit and git push your changes.
That’s it all on branches. Enjoy the learning git and good luck!