Git create new branch without checkout10/7/2023 For every file that is in both branches, if the version in branch2 is different, update the working tree version.Įach of these steps could clobber something in your work-tree:.For every file that is in branch2 and not in branch1, create that file (with appropriate contents).For every file that is in branch1 and not in branch2, 1 remove that file. A git checkout branch2 would have to do this: That is-and please note that this is still simplified there are some extra-difficult corner cases with staged git adds, git rms and such-suppose you are on branch1. The rule here is simple in one way, and complicated/hard-to-explain in another: You may switch branches with uncommitted changes in the work-tree if and only if said switching does not require clobbering those changes. Why does it work sometimes, and not other times? This answer is all about when Git will let you git checkout branch2 even though you started making some changes. If Git won't let you switch, you already have a remedy: use git stash or git commit or, if your changes are trivial to re-create, use git checkout -f to force it. Both do the same thing, when used in the basic ways. Sidebar: git stash save is the old syntax git stash push was introduced in Git version 2.13, to fix up some problems with the arguments to git stash and allow for new options. You can then git stash apply them after switching. Note that git stash save or git stash push actually means "Commit all the changes, but on no branch at all, then remove them from where I am now." That makes it possible to switch: you now have no in-progress changes. You may want to use git stash to save them this is one of the things it's designed for. If Git won't let you do it, you have to commit your changes, to save them somewhere permanent. Sometimes Git says "OK, you're on branch2 now!" Sometimes, Git says "I can't do that, I'd lose some of your changes." The observation here is that, after you start working in branch1 (forgetting or not realizing that it would be good to switch to a different branch branch2 first), you run: git checkout branch2 (My own preference is to just commit anyway, avoiding git stash and not trying to be too tricky, but others like other methods.) It is not a recommendation to engage in any particular workflows. This answer is an attempt to explain why Git behaves the way it does.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |