This is the third in a series about Git
Last we left off you had committed a number of times to a branch. Now it is time to merge that branch back into main.
As noted in the previous post, we’ll do this in three stages:
- Merge main into feature1
- Interactive Rebase feature1
- Merge feature1 into main
The first step is done only to surface merge conflicts on your machine rather than on the server because it may be easier to fix on your computer. It is entirely up to you. I’ll cover merge conflicts in a future post, but the short answer is that if two programmers change the same line(s) in a file, git won’t know what to do and so will throw up a merge conflict. It is then up to you to resolve the conflict by taking one or the other or both versions.
If, like me, you have forgotten what you’ve committed so far, enter the command
clear git log origin..HEAD
The first line will clear your terminal, the second will produce a list of all the commits that have not yet been pushed to the server. That will display the SHA (unique ID) for each commit, who committed it and when along with the commit name. If that is more than you want to know, tack on –oneline which will give you a simple list of the commit names and their abbreviated SHA:
git log origin..HEAD --oneline c4e2048 (HEAD -> feature1) Update calculator 3513f21 Add calculator class 5703b9d First commit
In either case, you can see that we have three commits. The most recent one has a terrible message (Update calculator) that tells us nothing. We need a couple more commits for demonstration purposes. Add a multiply method to calculator and check it in, and then add a divide method and check that in.
You now have 5 commits and you’d like to get them onto your main branch. Let’s start by merging main into feature1. If that goes smoothly we’ll be ready to merge feature1 into main. Remember, we’re only doing this first step to handle merge conflicts and we won’t have any. Make sure you are on the Feature1 branch by using
You ought to see
On branch feature1 nothing to commit, working tree clean
If you’re on the wrong branch enter
git checkout feature1
To merge main into feature enter
git merge main
Since no one has added to main or changed it in any way, you’ll be told that you are already up to date. Often though, others will have merged their feature branches into main, and then this will merge the changed main into your branch. Assuming there are no problems, you are ready to merge your branch into main. But wait! Before you do, it is time to make sure your branches are going to be helpful to anyone reviewing your merge.
The traits we are looking for are
- Messages have descriptive names
- You have squashed together any commits that are really part of the same action
It is annoying to have to read commits like this:
c436e10 (HEAD -> feature1) Add Divide method 832252e Add Multiply method c4e2048 Update calculator 3513f21 Add calculator class 5703b9d First commit
First of all, we don’t know what happened in “Update Calculator” and second, do we really have to have add, update, add multiply and add divide? Can’t we combine these? Of course we can. We will squash all the calculator commits together and fix up the message with Interactive Rebase.
First and foremost you may have heard of rebase or rebasing. This is not the same thing. Rebasing isn’t all that hard, but it does take a bit of time to get your head around it so we’ll cover it in a future post. Interactive Rebasing is a different animal.
Make sure you are on the Feature1 branch and enter
git rebase -i HEAD~5
Between HEAD and 5 is a tilde (~)
This says to do an interactive rebase of all the commits from the current one (HEAD) and going back 5. You’ll get a message that git is waiting for your editor. On my machine that editor defaults to Visual Studio Code (your mileage may vary).
At the top of the file I see my commits and at the bottom of the file I see all the ways I can mess with them. The two we care about are s for squash and pick (which just means don’t drop this commit). You can also change the order of the commits if that helps.
I start out with
pick 5703b9d First commit pick 3513f21 Add calculator class pick c4e2048 Update calculator pick 832252e Add Multiply method pick c436e10 Add Divide method
I want to combine the second one with all the ones below it, so I change the pick to s for all the ones I want to squash
pick 5703b9d First commit pick 3513f21 Add calculator class s c4e2048 Update calculator s 832252e Add Multiply method s c436e10 Add Divide method
I then save the file and close VSCode. VSCode will immediately reopen and prompt for the commit message(s). I’ll add just one:: Create Calculator Class. Git will come back and tell me that I have successfully rebased.
Now, rather than making four commits, we make 1. One way we can see this is by entering
git log -1 --pretty=fuller
which will tell you who created the commit, who actually made the commit and the message from the commit.
Things look good, and it is time to merge back into main. Check out main and enter
get merge Feature1
You should get a very satisfying message that tells you what files were merged and how many changes were made in each
Updating 9a42b5e..46e6d7a Fast-forward Test10/Test10.sln | 25 +++++++++++++++++++++++++ Test10/Test10/Calculator.cs | 31 +++++++++++++++++++++++++++++++ Test10/Test10/Program.cs | 14 ++++++++++++++ Test10/Test10/Test10.csproj | 8 ++++++++ 4 files changed, 78 insertions(+) create mode 100644 Test10/Test10.sln create mode 100644 Test10/Test10/Calculator.cs create mode 100644 Test10/Test10/Program.cs create mode 100644 Test10/Test10/Test10.csproj
You’ve done it! You made a feature branch, made some changes, committed the changes, used interactive rebase to squash some of the commits together and then merged the entire set of commits into main!
Now remember, all that happened in your local repo. Time to push these changes up to the server
Once git settles down, open your favorite browser and navigate to your repo on GitHub. If you drill down into the code and select calculator.cs you will see the commit message Create Calculator Class and all the changes that were made in the commits we squashed together. As far as the server is concerned they all came in a single commit.
Back up one level and click on History. You’ll see only two commits: the first one and the one you just made. Anyone looking at your commits will instantly get what that commit was (rather than four commits each of which does a small part)
Clicking on Create Calculator Class takes you to the code that goes with all the squashed commits.
In coming posts we’ll look at
- Merge conflicts
- Fancy git commands