NealWalters
NealWalters

Reputation: 18167

Git switch between independent branches

I have two sets of code in my repository. One is the old system, which from time to time I have to maintain. The other is my new system. I never want to merge the two, but I want to be able to switch between them.

Earlier I switched from the new branch to the original. Today I want to go back to the feature branch, so I did this:

git switch feature/development

Then I try

git pull

and see various message about merges.

I also tried

git pull --force 

and it says "Pulling is not possible because you have unmerged files".

Is there a simple way to maintain both branches and switch back and forth between them?

Upvotes: 0

Views: 65

Answers (2)

torek
torek

Reputation: 487725

The git pull command is a convenience command: it means run git fetch, then run a second Git command. The second command is selectable, but the default is git merge.

So you asked your Git to do a merge. That merge has not yet finished. You must finish or stop the merge before you can do anything else.

If you did not want to do a merge at all, use git merge --abort to stop the ongoing merge and put things back the way they were before you started a merge. If you do want a merge, you must finish the merge operation for Git, which was not able to do this on its own, then use git merge --continue to tell Git that you have put the correct merge result into place and Git should save that as the correct result for future merges.

Also, this is BizTalk, so some of the files really can't be merged.

Nonetheless, if you want to use git merge—which git pull will do—you must merge them, by constructing the correct result and telling Git that you have done so. (Your other option is to not merge at all.)

Is there a simple way to maintain both branches and switch back and forth between them?

Not if you use git pull, because that means fetch and then merge. When you hit a merge conflict, you must fix it (or terminate the merge as shown above) before you can switch.1

If you just want to have two separate work-trees in which you work on two separate branches, consider:

  • using git worktree if your Git is at least 2.15 (git worktree was new in Git 2.5 but there's a particularly bad bug that's not fixed until 2.15), or
  • using two separate Git repositories.

With two separate work-trees—whether backed by two separate repositories, or a single shared one—you can leave merge conflicts in place as long as you like and still do work in the other work-tree.


1There are some minor exceptions to this rule but they don't apply in your case—if they did, your switch would have worked.

Upvotes: 1

ebillis
ebillis

Reputation: 195

You can use git stash.

For example you have two branches, oldVersion and newVersion. Actually you are working on oldVersion. Once you want to go on newVersion you can commit or stash if the job is not done.

Actually there is no pending changes so you can checkout to another branche easily. When you will come back to oldVersion, you can retrieve your work from the stash with git pop.

Hope i have answer your question.

Upvotes: 0

Related Questions