Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge single file without rebasing

I have three branches (let's call them master, testing, and feature). All three are shared, so I cannot rebase any of them without causing problems for others. Currently, all three branches have diverged (none is a fast-forward), so eventually some merging will need to done, since rebasing is not an option.

For the moment, though, I would like to pull in the Makefile from testing into feature, since feature was split off from master, and the Makefile was added in testing. I do not want to merge in any other changes between the two branches, however.

My understanding is that if I just git-add Makefile to feature, this will cause merge conflicts when I merge feature back into testing (and then master), especially if I make any further additions to the Makefile in my feature branch.

I could do git-cherry-pick; however, there were multiple commits to the Makefile in testing, and I assume there's a better way than trying to cherry-pick all of those commits into feature.

like image 989
chimeracoder Avatar asked Mar 22 '12 16:03

chimeracoder


People also ask

Can you merge just one file in git?

In Conclusion. We can use git checkout for far more than simply changing branches. If we supply it with a branch name and a file, we can replace a corrupted or broken file. Instead, if we want to pass some of the changed content we can use the --patch flag to manually merge an individual file.

What is an alternative to merging in git rebasing?

While merging is definitely the easiest and most common way to integrate changes, it's not the only one: "Rebase" is an alternative means of integration.

When should you avoid rebase?

If you use pull requests as part of your code review process, you need to avoid using git rebase after creating the pull request. As soon as you make the pull request, other developers will be looking at your commits, which means that it's a public branch.


1 Answers

You can just do a

git checkout branch_name <path(s)>

This can load a specific file but you can also use wildcards and directories

Note that:

  • Paths are relative
  • The path makes that git does not switch branches, so you can just commit after gettihg the file
like image 189
Dirk Avatar answered Sep 28 '22 08:09

Dirk