Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

git - How can I checkout a branch while ensuring that none of my current branch's changes are carried over?

I want switch branches and have git always set the working directory contents to reflect the state of the branch I'm switching to. I'm experiencing the behaviors stated here https://web.archive.org/web/20160331103129/http://www.gitguys.com/topics/switching-branches-without-committing - git is making a decision** on whether to carry certain files from the working directory associated with my current branch to the branch I switch to.

** This seems to conflict with what's stated here https://www.atlassian.com/git/tutorials/using-branches: "Checking out a branch updates the files in the working directory to match the version stored in that branch..."

In some cases I don't want git to make a decision - I just want Git to make the working directory match the state of the new branch. However, I don't want to lose any changes. I'm willing to commit or stash prior.

Possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Uncommitted changes remain in your worktree or index until you tell git where to commit them.

git add, git checkout and git reset all have a --patch option. The workflow is, doing what needs doing and recording it where it needs recording are separate tasks. Often enough you find yourself doing effectively drive-by bugfixing, where you find a complete mess someoneusually me left and the issue is, there's doing the work, and doing the paperwork, but you're staring right straight at the work and know exactly what needs doing.

The git way1 is, Do It. Get it all correct in your worktree (this may involve a bit of exploratory branching itself)

  • and then construct the commit series you'd have constructed in the first place if you'd known exactly what you were doing all along.

That's the important part. That's what makes professional, publishable work. What trips people up is, git's meant at least as much for the messy part that comes before that.


1hahaha he said "the git way", as if there were only onelol


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...