Useful git stuff - Resolving conflicts and squashing commits

Here are some tips for resolving conflicts using the terminal. The typical conflict is between a feature branch and a development branch prior to a pull request. The below method utilises git rebase which means the commit history is slightly cleaner. Compared to pulling development into the feature branch and merging it, then fixing the conflict and committing it again.

Steps

First, switch to the development branch and pull development so it’s up to date, then switch to conflicted feature branch

git rebase development

This will rebase the current feature branch with development. Meaning the new feature commits will be stacked on top of the old development commits. It will pause the rebase process when it finds a conflict.

git status

Then open the conflict file and fix it manually, using a code editor. Then add the file using git. No need to commit as rebase is doing the committing

git add file

Then continue the rebase using the below command.

git rebase —continue

Finally push the new branch history to remote, we need to force the push so that it overwrites the history in the remote.

git push —force (note there weren’t any commits)

How to squash commits

How to squash commits, it will then open up in an editor where you can add/delete lines of code:

git rebase -i HEAD~[number of commits you want to go back]

Then change pick to either 'squash' or 'fixup'. Note fixup will delete the corresponding commit message, whereas squash may combine them.

git push --force

References:

* https://www.atlassian.com/git/tutorials/merging-vs-rebasing

Tags: