cancel
Showing results for 
Search instead for 
Did you mean: 
Moderator
Moderator
  • 364 Views

Quickly cherry picking commits between branches

I wanted to share a neat tip that I discovered this morning.

I am currently working with two branches, one that a couple of developers maintain, and one that I maintain, which contains some build artifacts. I wanted to maintain some of the files in my branch current to the developers branch, but I did not want to merge everything.

This is where the checkout feature comes handy.

Say that there is a image1.png file in the dev branch that I need to merge in my build branch.

I simply run the following commands

$ git checkout build
$ git checkout dev image1.png
$ git add . 

This adds the image that is in the dev branch to my build branch.

Notice that I can also use a wildcard with that feature:

$ git checkout dev images/*

Pretty cool eh Smiley Happy

Labels (1)
4 Replies
Highlighted
joelbirchler Mission Specialist
Mission Specialist
  • 337 Views

Re: Quickly cherry picking commits between branches

Be careful not to confuse this with the Git cherry-pick command. The checkout process that you describe above restores a file from a specified branch into your current branch. This is slightly different than cherry-picking a commit.

You can use git cherry-pick <commit> to apply a specific commit on to your branch. This can be useful when manually grabbing whole commits from a branch without integrate other changes.

Reply
Loading...
Moderator
Moderator
  • 335 Views

Re: Quickly cherry picking commits between branches

Thanks Joel. Yes I did not mean to imply that this was the Git cherry-pick feature.

 

0 Kudos
Reply
Loading...
Flight Engineer sbulav Flight Engineer
Flight Engineer
  • 261 Views

Re: Quickly cherry picking commits between branches

I use this trick all the time to quickly revert file to it's remote state in master:

git checkout origin/master -- path/to/file

 

0 Kudos
Reply
Loading...
Moderator
Moderator
  • 235 Views

Re: Quickly cherry picking commits between branches

Good stuff Smiley Happy

0 Kudos
Reply
Loading...
Join the discussion
You must log in to join this conversation.