简体   繁体   中英

How to change branch and pull from another branch?

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.

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 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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM