![]() Note that if daves_branch doesn't exist locally you'll need to git fetch first before using switch. It will then also automatically set up remote branch tracking. Since you do not have the branch locally, this will automatically make switch look on the remote repo. If daves_branch exists on the remote repository, but not on your local branch, you can simply type: git switch daves_branch Note: this difference was tested using Git version 2.37.All of the information written below was accurate, but a new command, git switch has been added that simplifies the effort. git switch -f does not work at all if you are currently merging (even without conflicts), as you get this error message:įatal: cannot switch branch while merging However, when attempting to switch branches while you are currently sitting in the middle of a merge, git checkout -f will succeed, even if you have unresolved conflicts at that moment. It seems like the last sentence applies to the other meaning of checkout, i.e. When checking out paths from the index, do not fail upon unmerged entries instead, unmerged entries are ignored. This is used to throw away local changes and any untracked files or directories that are in the way. When switching branches, proceed even if the index or the working tree differs from HEAD, and even if there are untracked files in the way. This is used to throw away local changes.Īnd similarly, git checkout -f is documented as such (emphasis on the last sentence): If -recurse-submodules is specified, submodule content is also restored to match the switching target. Both the index and working tree are restored to match the switching target. Proceed even if the index or the working tree differs from HEAD. (For example you can use checkout with a commit or a remote tracking branch such as origin/main directly, but with switch you must also explicitly specify the -detach option.) However, I have found at least one significant functionality difference too. The other answers have already covered the motivation behind splitting checkout into switch and restore, as well as the fact that syntactic usage differences exist. ![]() Tl dr: When using checkout with -force, you can switch branches while in the middle of a merge. The source tree for restoring is now given by the -s option, rather than being an inline argument.-detach is now always required when switching to a detached head, where it was previously optional for commits but required for branches.The -b/ -B options for creating a new branch before switching are renamed to -c/ -C.N/A (use git switch then git switch -orphan )Īs shown in this comparison, some prior usages can be converted to the new commands by replacing the old command name ( checkout) to the new one ( switch, restore), whereas others require additional adjustment. From reading through the documentation, I think this should be a fairly complete comparison: GIT SWITCH VS CHECKOUT FULLI have not found a full comparison of the commands anywhere. Note that as of December 2021, the new commands are still listed as experimental ( switch, restore): Old "git checkout" command is still here and will be until all (or mostįrom this, it's clear that the new commands were introduced to reduce confusion by having two focused commands, rather than one multi-purpose command. To remedy that, theĬommand will be split into two new ones: switch and restore. ![]() Users (and it even bites old timers sometimes). "git checkout" doing too many things is a source of confusion for many The commit that added the switch command explains the rationale for the new commands in its commit message: Git-checkout - Switch branches or restore working tree files This dual purpose of checkout can be seen in its summary description in the documentation: In other words, checkout does two different things, and this release split each of those different things into its own focused command.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |