Learn how to cherry pick a commit from a different branch in Git and enhance your programming skills. Take a further step into your programming career with our comprehensive coding courses and bootcamps.
Key Insights
- To cherry pick a commit from a different branch, the first step is to navigate to the Git repo folder using the terminal.
- The second step is checking out the branch where you want to work by executing the 'git checkout master' command.
- Each commit has a unique hash, you need to find this hash from either the commit history on GitHub or Bitbucket or by running the 'git log --oneline' command in your terminal.
- Once you have the hash for the commit you want to cherry-pick, you can execute the 'git cherry-pick' command followed by the hash.
- It is possible to cherry-pick multiple commits by listing multiple hashes in the order they were originally committed.
- Beyond Git, there are a variety of coding courses and bootcamps available including web development, Python, data science, and web design courses.
Essential Git Commands
-
git status
What's changed, staged, and untracked — your most-used command.
-
git diff
See exactly what's different before staging.
-
git log
Inspect commit history with --oneline or --graph for visualization.
-
git reset
Undo staging or commits — careful with --hard.
Cherry Pick a Commit from a Different Branch
- In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder that is your Git repo.
- Check out the branch you want to work on (not the one with the commit). Enter this command (if needed, replaced master with your desired branch):
- Git checkout master
- Each commit has a unique hash (which looks something like 2f5451f). You need to find the hash for the commit you want to cherry-pick. Here are two places you can see the hash for commits:
- In the commit history on the GitHub or Bitbucket or website.
- In your terminal (Terminal, Git Bash, or Windows Command Prompt) run the command Git log—oneline
- Once you know the hash for the good commit you want to cherry-pick, run the following command (replacing 2f5451f with your commit's hash):
- Git cherry-pick 2f5451f
| Cherry Pick Multiple Commits |
|---|
|
You can cherry-pick multiple commits by listing multiple hashes in the order they were originally committed. Git cherry-pick hash1 hash5 hash7 |
Go Beyond Git
We offer the best coding courses and bootcamps for students at all levels of experience. Classes are hands-on and students receive workbooks with step-by-step instructions through exercises with real-world applications. Check out our programming courses now: