![]() If your changes would be overwritten by the attempt to switch branches, then git won't just allow you to switch branches because you would lose data. Well, sometimes you can - such as in the case you describe. Hi Anish, unfortunately this is not an answer to my question because git checkout would change existing files in the working directory as well. This may rarely be the option you'd want, but it's easy enough to do if it is what you want. git checkout can take paths as an argument, which, if given, leaves HEAD alone, and just checks out those paths into your working directory and index, so you can just use: git checkout branchname. (Recall that only having one worktree and index is what necessitates git's default behavior so if you don't want that, you can make more worktrees.) ĥ - You can discard your changes. git commit -m "wip"Ĥ - You can check out the other branch to a new worktree and index. ![]() As long as you don't push them to a shared repository, it's easy to undo the commit anyway. If you do have deletions, you can still make it work, but rather than work around that it probably just makes more sense to use one of the other methods I've listed at least in those cases.ģ - You can go ahead and commit your changes. unless your changes include deletion of files) you can use the form of checkout to pull the changes back later. git stashĢ - You can create a temporary branch for your changes. ![]() Then after you're done looking at the other branch, you can come back to the branch where the changes are and unstash the changes. if you only want to create a new branch, git checkout -b BRANCHNAME will do that without touching. I only need to tell Git its new branch already without allowing Git to write files in workdir. Please note, that you need option -m to see an entry in git. If you want to switch branches without bringing your local changes with you, you have several options.ġ - You can stash your changes. Then you can use it like git switch existingbranch. Your changes are in the worktree and maybe the index at this point, but by default you only have one of those (not one per branch) so switching branches without bringing along your changes would mean discarding the changes. Utility aside, it's really the only sensible default behavior for git. This may not be what you expected, but it's often very useful. ![]() You can and if you do, the uncommitted changes come with you. On my production server using CLI, when I switch to the new branch with git checkout mynewbranch, the files are not updated. Locally I use tortoise GIT and when I switch to the new branch, the files are updated. I can't switch branches before committing? I have created a new branch and added some files. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |