git checkout

The checkout command (git checkout) is one of the most commonly used commands in git, and it is also a very dangerous command because it rewrites the workspace:

  1. Usage 1: git checkout [-q] [<commit>] [--] <paths>...
  2. Usage 2: git checkout [<branch>]
  3. 用法三:git checkout [-m] [[-b|--orphan] <new_branch>] [<start_point>]
  • The difference between the first usage listed above and the second usage is that the first usage includes the path <paths> in the command. To avoid a conflict between paths and references (or commit ids) with the same name, you can use them in <paths> Use two consecutive dashes in front (minus sign as a split)
  • The first usage of <commit> is optional, if omitted, it is equivalent to checkout from the staging area (the default value of the checkout is the staging area).
  • The first usage (including the usage of the path <paths>) does not change the HEAD header pointer, and is mainly used to specify the version of the file to overwrite the corresponding file in the workspace. If <commit> is omitted, the files in the staging area will be overwritten with the files in the working area, otherwise the files in the specified commit will be overwritten with the corresponding files in the staging area and the working area.
  • The second usage will change the HEAD head pointer, which is mainly used to switch to the branch. If you omit <branch>, it is equivalent to checking the status of the workspace.
  • The third usage is mainly to create and switch to a new branch (<new_branch>), and the new branch is created from the commit specified by <start_point>. The new branch is not substantially different from the master branch, both are references under the refs/heads namespace.

 Concrete example:

  • git checkout branch Check out the branch branch. To complete the three steps in the figure above, update HEAD to point to the branch branch, and update the staging area and work area with the tree pointed to by branch
  • git checkout summarizes the differences between workspace, staging area and HEAD
  • git checkout HEAD same as above
  • git checkout -- filename Overwrite the filename file in the workspace with the filename file in the staging area. Equivalent to the undo of git add filename, this command is very dangerous, because local changes will be silently overwritten
  • git checkout -- . Or write git checkout . Note that there is a dot (.)  after the command. This command is the most dangerous and will cancel all local modifications (relative to the staging area), which is equivalent to directly overwriting all files in the staging area Local files, do not give users any chance to confirm!
  • git checkout HEAD . Or git checkout HEAD <file> , note that there is a dot (.) after the command, it will replace all or part of the files in the master branch pointed to by HEAD to the files in the staging area and the work area, which is also very dangerous.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324388179&siteId=291194637