However, if you have local modifications that would conflict with the switched-to branch, Git would abort the switch. As a general rule, your working copy does NOT have to be clean before you can use "switch". Switch to the specified branch and discard any local changes to obtain a clean working copy. You can also specify a starting point (either another branch or a concrete revision) if you don't provide any specific starting point, the new branch will be based on the current HEAD branch. Using the "-c" flag, you can specify a name for a new branch that should be created. The name of a new local branch you want to create. If you specify the name of an existing local branch, you will switch to this branch and make it the current "HEAD" branch.īut you can also specify a remote branch: in that case, Git will create a new local branch based on that remote branch and set up a tracking relationship. The name of a local or remote branch that you want to switch to. It has a very clear and limited purpose: switching and creating branches! Important Options The "switch" command provides a simple alternative to "checkout". The problem with "checkout", however, is that it's a very versatile command: you can not only use it to switch branches, but also to discard changes, restore files, and much more. It's relatively new (added in Git v2.23) and provides a simpler alternative to the classic "checkout" command.īefore "switch" was available, changing branches had to be done with the "checkout" command. There may be changes, but usually not by much, so you can use them now, they are easier to understand and less confusing.The "switch" command allows you to switch your current HEAD branch. ![]() They provide more explicit semantics, in line with the UNIX philosophy.īoth commands were proposed in 2019, and as of now, they are experimental. The features of git checkout are clearly separated: git switch is used to switch branches, while git restore is used to restore working tree files. ![]() # Restore all working tree files with top pathspec magic $ git restore :/ Conclusion main.cįor batch recovery: # Restore all C source files to match the version in the index $ git restore '*.c' # Restore all files in the current directory $ git restore. main.c # Quickly restore main.c from index $ git restore. E.g: # Incorrectly deleted files $ rm -f. main.cĪnother useful git restore case might be restoring a mishandled file. main.c # Equivalent to $ git restore -source=HEAD^. E.g: # Restore `./main.c` in the working tree with the last commit $ git restore -s HEAD^. The above-mentioned is the default, if we want to restore from a different commit, we can use the -source option. Note that when restoring index content from HEAD, we can only use git reset, and git checkout has no corresponding option. main.c # Restoring the working tree and index from HEAD $ git checkout HEAD. main.c # Restoring index content from HEAD $ git reset. Simple examples: # Restoring the working tree from the index $ git checkout. In the past, we could use git checkout - main.c to restore the working tree file from the index, the syntax was git checkout -, where treeish could be specified as the source. This part of the feature is taken over by git restore. Git checkout can checkout a branch or create a new branch and checkout into it: # Switched to branch 'test' $ git checkout test # Switched to a new branch 'test' $ git checkout -b test # Switch back to the previous branch $ git checkout - # Switched to a commit $ git checkout master~1Īnd git switch is used to take over branch related, so it can also do: # Switched to branch 'test' $ git switch test # Switched to a new branch 'test' $ git switch -c test # Switch back to the previous branch $ git switch - # Switched to a commit $ git switch -d master~1Īnd as we said at the beginning, git checkout can also restore working tree files. ![]() HEAD: It refers to the “current” or “active” branch, when we need to check out a branch (referring to your attempt to match the branch with what is in the working copy), only one can be checked out at a time.Index (staging area or cache): it refers to you have git add-ed, or, what would be committed if you were to run git commit.Working copy (working tree file): It refers to the file in the repository that appears on the hard disk. ![]() Before introducing how it works, we need to briefly understand its associated Git concepts.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |