cancel
Showing results for 
Search instead for 
Did you mean: 
Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 2,580 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 :)

Labels (1)
4 Replies
joelbirchler
Moderator
Moderator
  • 2,553 Views

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.

Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 2,551 Views

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

 

0 Kudos
sbulav
Flight Engineer Flight Engineer
Flight Engineer
  • 2,477 Views

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
Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 2,451 Views

Good stuff :)

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