Git tips and tricks
 gitVarious git workflows I follow and lesser known features.
Merging a feature branch
The workflow I always follow is:
$ git checkout master
$ git pull
$ git checkout feature-branch
$ git rebase master # fix any conflicts
$ git checkout master
$ git merge feature-branch --no-ff
First I pull the most recent changes to the branch I want to merge the feature branch into (in this case master
). I’ll
then rebase the feature branch with master
to apply any new commits from master
and highlight any conflicts, so they
can be fixed - this avoids running into merge conflicts when merging into master
and therefore having to fix
on the master
branch and push a “Fix merge conflicts” commit.
I’ll then proceed with the merge, --no-ff
ensures a merge commit is always created which makes the scenario where the
changes need reverting a lot easier (you can just revert the merge commit which will revert all the commits added when
the merge was made)
Interactive rebase
Command: git rebase -i|--interactive
Rebase all the commits up to, but not including, the given <commit-hash>
$ git rebase -i <commit-hash>
Rebase the last n
commits
$ git rebase -i HEAD~n
It will then open a list of the selected commits in your text editor where you can apply commands.
The most common commands I find using during the rebase are: fixup
, reword
& drop
Note: Rebasing rewrites the commit history for any commits selected, so you’ll likely only want to apply to commits that haven’t been pushed or are working alone on a feature branch where you can
git push --force
Viewing changes in the console
Command: git show
When committing small fixes straight to the master branch I typically like to sanity check the changes I have just staged and committed as pushing some unwelcome file to master is a bit embarrassing.
$ git show # Shows the diff of the most recent commit
$ git show HEAD~1 # """ commit prior to the first...