8

We have a development branch which is our master and a separate maintenance branch. I frequently have to cherry-pick commits from the master to the maintenance or vice-versa when I fix bugs. Normally I accomplish this by performing the following procedure...

  1. Commit on the master branch
  2. Push commit to remote
  3. Switch/Checkout to maintenance branch
  4. Cherry-pick the commit from step 1
  5. Build and make sure everything is still working as intended
  6. Push maintenance commit to remote

The problem that I have is because the branches have become significantly divergent I have to rebuild the entire project each time I switch which takes up to 10 minutes. This is expected, but I'd like to not have to do this since I'm frequently switching between branches. So to avoid this I created a second working directory so that I have a directory for each branch. The problem with this is that I can't cherry-pick the original master commit into the maintenance directory until I've pulled that commit into the master branch of the maintenance directory from the remote. When I do this of course, I have to completely rebuild.

Is there a way to pull the commits into the master branch of my maintenance directory without switching? Or, is there a better way of doing this entirely? We recently switched to Git from CVS, so I'm not that familiar with it.

Brian
  • 14,610
  • 7
  • 35
  • 43
John Brooks
  • 73
  • 1
  • 4
  • 3
    Possible duplicate of [How to 'git pull' into a branch that is not the current one?](http://stackoverflow.com/questions/18994609/how-to-git-pull-into-a-branch-that-is-not-the-current-one) – Victor Sergienko Jan 27 '17 at 19:51
  • Since you already have multiple directories/clones on your machine, you can have them pull from each other instead of from remote. http://stackoverflow.com/questions/10603671 – approxiblue Jan 28 '17 at 16:13
  • Possible duplicate of [Merge, update, and pull Git branches without using checkouts](https://stackoverflow.com/questions/3216360/merge-update-and-pull-git-branches-without-using-checkouts) – Victor Sergienko Oct 29 '17 at 23:29

2 Answers2

1

You can download the new commits into the other repository simply by running fetch (in the other repository):

git fetch origin

However, fetch will only update the remote tracking branches, not the local branches, so master will not move, but origin/master will. So you can cherry-pick the newest commit with:

git cherry-pick origin/master

(To my knowledge, there is no elegant way to create a new commit atop another branch than the one that is currently checked out, so I think you need to stick to the two repositories if you don't want to switch branches. Sounds like you'll need that anyway in order to test the changes you make on the other branch?)

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
0

I guess multiple working dirs each dedicated for a given branch with its own build directory is what you want. In this case you would get a single repository with multiple workdirs attached to it, so if you perform a change (a commit, or a fetch, whatever) in one directory, that change is instantly visible to all other workdirs.

If your git is fresh enough (>= 2.5) you could go with git worktree, see this tutorial for more info.

Otherwise (git < 2.5) you can pick git-new-workdir script from git contribs and use it.

user3159253
  • 16,836
  • 3
  • 30
  • 56
  • I updated git to the latest today and tried the worktree out. It works great. The only problem is that TortoiseGit doesn't yet support a git worktree, but I'm just going to learn to use the BASH terminal. Thank you. – John Brooks Dec 23 '15 at 21:49