Skip to content

Index

Amend

Amend the last commit with new changes. This helped me when I had some basic whitespace changes that didn't warrant a new commit. Giving the --no-edit option means we won't edit the commit message.

git commit --amend --no-edit

Fixup

Combine the last commit with the previous commit.

git commit --fixup <commit>

Reword

Change the commit message of the last commit. Use git rebase --continue when done.

git rebase -i HEAD~2 # operate on the last two commits
pick 1a2b3c4 Last commit
reword 5d6e7f8 Previous commit # change the commit message
reword 9g0h1i2 Commit 1 # change the commit message

# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message

Squash & Fixup

Squash and fixup allow us to combine multiple commits into one. We can mark commits as fixup or squash in an interactive rebase, and squash will prompt for a new commit message whereas fixup will use the previous commit message. One of the conventions at a job I had was to only have a single commit per pull request, so I would use this to combine all my commits into one before merging.

When I first used this with I had to use a git push --force on Bitbucket to update the remote repository.

git rebase -i HEAD~2 # operate on the last two commits

With Fixup

pick 1a2b3c4 Last commit
fixup 5d6e7f8 Previous commit # gets squashed into 'Last commit'
fixup 9g0h1i2 Commit 1 # gets squashed into 'Last commit'

# Commands:
# p, pick = use commit
# f, fixup = like "squash", but discard this commit's log message

With Squash

pick 1a2b3c4 Last commit
squash 5d6e7f8 Previous commit # gets squashed into 'Last commit'
squash 9g0h1i2 Commit 1 # gets squashed into 'Last commit'

# Commands:
# p, pick = use commit
# s, squash = use commit, but meld into previous commit

Resetting to fix my booboos

Sometimes I need to reverse through my commit history to fix a mistake I've made.

Soft

Undoes all of the changes between HEAD and the commit, but leaves the changes staged.

git reset --soft HEAD~1

Mixed

Undoes all of the changes between HEAD and the commit, but leaves the changes unstaged.

git reset --mixed HEAD~1

Hard

Undoes all of the changes between HEAD and the commit, and discards the changes.

git reset --hard HEAD~1

Finding Commits

We can find all of the commits for a given file by using the --follow option.

git log --follow --oneline -- filename

Find changes to a file at a line over commits.

git log -L 1,1:filename
git log -S"<<string to search for>>" --since="2 years ago" --until="now" -- /path/to/the/file

--since and --until can be specified as a date, or a relative date like "2 years ago".

Merging

Often run into divergent branch issues and need to resolve with rebasing or fast forwarding. What do each of these options mean?

Fast Forward

Rebase

Change the base of the development branch to the branch we are merging into.

Merge

Pulls in the latest changes from the branch you are merging into and creates a merge commit.