DEV Community

loading...
Cover image for Merging Local Git Branches

Merging Local Git Branches

connoro7 profile image Connor Dillon ・2 min read

Using Merge

git checkout test
git pull
git checkout master
git pull
git merge --no-ff --no-commit test
Enter fullscreen mode Exit fullscreen mode

This method will test your merge before a full commit, and will avoid a fast-forward commit by --no-ff,

If a conflict is encountered, we can run git status to check details about the conflicts and try to solve

git status
Enter fullscreen mode Exit fullscreen mode

Once we solve the conflicts, or if there is no conflict, we commit and push them

git commit -m 'merge test branch'
git push
Enter fullscreen mode Exit fullscreen mode

But this way will lose the changes history logged in test branch, and it would make master branch difficult for other developers to understand the history of the project.

So the best method is we have to use rebase instead of merge (suppose, when in this time, we have solved the branch conflicts)...

Using Rebase

Following is one simple sample, for advanced operations, please refer to http://git-scm.com/book/en/v2/Git-Branching-Rebasing

git checkout master
git pull
git checkout test
git pull
git rebase -i master
git checkout master
git merge test
Enter fullscreen mode Exit fullscreen mode

Yep, when you have uppers done, all the Test branch's commits will be moved onto the head of Master branch. The major benefit of rebasing is that you get a linear and much cleaner project history.

❗️ CAUTION

The only thing you need to avoid is: never use rebase on public branch, like master branch.

In other words, if you're ever about to perform an operation like the following, please be absolutely sure you know what you're doing.

git checkout master
git rebase -i test
Enter fullscreen mode Exit fullscreen mode

For more details, please see this article from Atlassian on The Golden Rule of Rebasing.

Discussion

pic
Editor guide