简体   繁体   中英

Simple Git Branch Switching Workflow

I am working on a project using Git. Now, I am working on a feature in a branch and ran across something that I need to see some code from someone else's branch to remind myself how a function he wrote works (these two branches will get merged when I am done).

I do not really want to commit my changes right now and git checkout his branch...then switch back over.

What is a better way to deal with this problem?

The tool for the job is git-stash . Running git stash in a Git directory with changed files will stash away your changes, bringing the working directory back to the state of HEAD (a clean state). This allows you to checkout a different branch, as well as many other operations. When you're done and back in this branch, run git stash pop to pop the changes in the stash back onto the working directory, bringing yourself back the state you were at before stashing the changes away.

$ git branch
* develop-back
  develop-other
$ git stash
$ git checkout develop-other
$ ...
$ git co develop-back
$ git stash pop

Yes, this works perfectly well and is very convenient. But remember that you shouldn't be afraid of committing changes in Git. In fact, git-stash does exactly that: commit your changes somewhere. Committing in Git means saving your work , and in this case that's exactly what you want to do. You can always reset-mixed:

$ git commit -am 'stashing away for a moment'
$ git co develop-other
$ ...
$ git co develop-back
$ git reset HEAD~

This workflow is equivalent to stashing, and you can see that it takes no more steps. git stash is just a "wrapper" around this procedure, with a somewhat more humane interface.

Sounds like you want to use git stash .

First execute git stash . Then checkout the other branch. Once you've finished taking a look, go back to your own branch, and use git stash pop .

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