Home > Net >  How to change branch and pull from another branch?
How to change branch and pull from another branch?

Time:06-09

I'm kind of new to Git and I don't feel safe running commands. I'm working on Branch A and I made a lot of changes. I need to work on another branch (Branch B) that doesn't have the files of Branch A. I'm not ready to make a merge of the branches.

Is it the right way if I commit and push to Branch A then fetch --all and checkout to Branch B. Then I pull from Branch B do the required work with the files of Branch B and commit and push in Branch B to later return to Branch A and pull the pushed files I made before?

I read about stash but I'm not sure if in this case it would be useful.

CodePudding user response:

It's sort of a matter of opinion, but I don't like stashing if I don't have to; if I'm in the middle of unfinished work on Branch A and I'm told to work on Branch B, I do an add-and-commit with a wip commit message (to indicate that this is "work in progress"), and then git fetch; git switch branchB.

No need to say fetch --all. No need to push Branch A (and it's probably a bad idea to do so, since it isn't baked yet). No need to pull because the fetch updated everything.

But if you already had a local Branch B, then after you switch to Branch B you might need to say git merge to update the local branch.

CodePudding user response:

If you want to preserve the work done on branch A and switch to branch B, then later return to your unfinished work on branch A you could:

  1. git stash push (save your uncommited work to stash);
  2. git checkout B (switch branch to B);
  3. git pull (integrate the latest remote state into B);
  4. do some work;
  5. git add <pathspec> && git commit (commit your work on Branch B);

When you are ready to continue on branch A you would do:

  1. git checkout A(switch branch to A);
  2. git stash pop (apply the state previously saved to stash).
  • Related