Git: Correcting mistakes 2 (amend & cherry-pick)

June 05, 2021

In this part, we will learn about amend and cherry-pick. I use amend almost daily. Cherry-pick is pretty useful and asked in interviews at times.

amend

Makes changes to the most recent commit.

Caution: Use on a Local commit since amend rewrites the history of commits.

When does amend come to the rescue?

  • Adding files you forgot to commit
  • Removing files you commit by mistake
  • Editing the commit message

How does it work?

Amend removes the old commit and adds a new one. HEAD then points to the new commit. amend working

Try it yourself

Let’s look at the command in action. I urge you to try these steps out on a local repo to get a better understanding. Except for git, no installations are necessary. I am using VS code, however, these commands can be run directly in the terminal.

  1. Create a folder. And a file with some initial text.
  2. Open git bash or terminal in that folder and run git init in the folder to initialize a git repository.
  3. Add and commit changes.
screenshot of steps so far
  1. We will now modify this initial commit. Add a new file and delete the current file.
screenshot of next step
  1. Stage the changes using git add .
  2. Commit changes using git commit --amend -m 'amend initial commit' Notice that the previous commit d680c29 is replaced with the new one 980dc84.
screenshot where amend is used

Note: To retain the commit message, use git commit --amend --no-edit

cherry-pick

Picks commits from other branches and adds them to the current branch. It creates duplicate commits.

Situations where cherry-pick is useful:

  • Team Collaboration - when you want to use code in a particular commit on someone’s branch without merging their branch into yours. Cherry-pick that commit onto your branch.
  • Bug hotfixes - when master branch has a bug that needs to be fixed immediately and is already fixed on develop.Cherry-pick the commit on master from develop.

How cherry-pick works:

cherry-pick working

Try it yourself

Time to look at the command in action. Again, please try these steps out on a local repo to get a better understanding. It takes hardly 10 mins.

  1. Create a folder. And a file with some initial text.
  2. Open git bash or terminal in that folder and run git init in the folder to initialize a git repository.
  3. Add and commit changes.
screenshot of steps so far
  1. Create a new branch ‘develop’ using git checkout -b 'develop'
  2. Add a new file, stage the changes and commit.
screenshot of branch creation and new commit
  1. Assume that you found the bug in the MainFile and fix it. Just update some text in the MainFile and commit the change in ‘develop’.
screenshot where bug is fixed
  1. Time to cherry-pick the last commit (bugfix) from ‘develop’ to ‘master’. Go to ‘master’ branch with git checkout master.
  2. Execute git cherry-pick <commit sha>. It’s a92d214 in this case. On ‘develop’ run git log --oneline to know the ‘commit sha’
screenshot for cherry-pick command

Note: To avoid creating a new commit, but get the changes of a particular commit, use the --no-commit option with cherry-pick. Similarly, --edit can be used to edit the commit message.

References:

Congratulations on reaching the end. 🎉 To get notified about other posts in this series,