Time to time we have to work on source code which are downloaded from a git repository, modify the code, implement the functionality, test it, get the code reviewed and then push it up to the repository. Now, how do we achieve this with the following in mind:
1. I do not want to do a git pull on my local source code base frequently simply because it is cumbersome.
2. I however, want to catch up with the mainline code (let’s call it master branch) so that when I am ready to push the code up I do not have enormous amount of merge conflicts, or conflicting other features which somehow difficult to accommodate at the last minute. So to avoid any surprise I would like to catch up with the mainline (be it master branch) time to time (I do it one a week at least).
3. I do not want to use merge command to catch up with the mainline code as it messes up with the git log.
All these points in mind I would recommend the below working principle:
1. git clone <git repository>
2. git checkout <branch name> (or by default you maybe already in the master branch. This would depend from source code to source code or repository to repository, so better check where you want to base your implementation.)
3. Now, after the above step 2, we are sitting at the branch where we want to start our implementation/bug fixing etc. Let’s call this “master” branch.
4. I would now like to create my private branch called “my_private”. To create a private branch do the below:
git checkout -b my_private
5. Now carry on with your implementations.
Multiple commits in private branch
While developing a new feature you might like to do it in a incremental way, i.e. when a little milestone is achieved you may like to commit it before carrying further. To do this do the below:
git add <file names>
git commit -m “commit message”
Now, if you carry on like this you would end up with multiple commits on your private branch. However, for doing review and pushing it up, it would be nice if all your changes/new implementations comes under one single commit. To achieve this you can use git interactive rebase command as below:
git rebase -i HEAD~n
where n stands for the number of commits (from the latest commits) to be included in the single commit.
This will bring up a screen which will look like as below:
pick 794969c79 Revert all commits that comprise 10x feature pick 808bc82dd Added database version and product code into DiagnosticsPresenter. pick 0b4d6973c Implementing Recent Tab pick 0c284d078 Implementing Recent Tab # Rebase 75ff6ea39..1f778b29d onto 75ff6ea39 (4 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
Leave the first pick but convert the second and all other picks to squash as below:
pick 794969c79 Revert all commits that comprise 10x feature squash 808bc82dd Added database version and product code into DiagnosticsPresenter. squash 0b4d6973c Implementing Recent Tab squash 0c284d078 Implementing Recent Tab # Rebase 75ff6ea39..1f778b29d onto 75ff6ea39 (4 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
Write and save, and you will be presented with the screen where you can decide what you would like to have as the new single commit message. Save and exit and you would have all your local commits squished into one single commit as below:
107b084c0 vbhadra Mon Sep 9 10:36:26 2019 +0100 Rework Recents presenter to communicate with STEDs facade instead of other presenter. 1aef95ec0 Andrei Tucma Tue Sep 10 08:30:19 2019 +0100 ITC5 calibration: calibrate popover option only available for itc5 8f761ce9c Robert Fry Mon Sep 9 15:57:09 2019 +0100 Possible fix for internal GPS to show signal strength 475af1405 dlangford Mon Sep 9 15:43:23 2019 +0100 STEDS Message popup design change eb1eaf342 Algirdas Balciunas Mon Sep 9 14:24:20 2019 +0100 GPS source switching fix 460b05fee Algirdas Balciunas Mon Sep 9 14:11:25 2019 +0100 Fixed ship/cargo values in AIS data dialog d7a47f77b Algirdas Balciunas Mon Sep 9 10:29:52 2019 +0100 Fix for app slowdown on Atom devices
Now, let’s say you want to catch up with the master branch. Go back to your master branch as below:
git checkout master (remember your base code maybe from a branch which not called master. It doesn’t matter, just go to the branch where you started)
git status (this will tell you how many commits your master is behind the latest commit)
git pull (this will bring all the latest changes delivered by others into your local master branch)
Now, go back to your private branch:
git checkout my_private (move to my_private branch)
git rebase master (this will rebase your my_private branch with the master)
107b084c0 vbhadra Mon Sep 9 10:36:26 2019 +0100 Rework Recents presenter to communicate with STEDs facade instead of other presenter. 1aef95ec0 Andrei Tucma Tue Sep 10 08:30:19 2019 +0100 ITC5 calibration: calibrate popover option only available for itc5 8f761ce9c Robert Fry Mon Sep 9 15:57:09 2019 +0100 Possible fix for internal GPS to show signal strength
Time to push up your changes
1. Go back to the master branch
2. Merge your private branch with master
3. git push
<strong>git checkout master</strong> <strong>git merge my_private</strong> <strong>git push</strong>
Leave a Reply