The impact of git checkout on the working directory

Reference link: https://blog.csdn.net/longintchar/article/details/84146935

What this article will discuss is: when we switch branches with the git checkout <branch_name> command, what happens if there are uncommitted changes?
If the status of the current workspace is "clean", it is easy to switch to another branch. However, if there are uncommitted changes, you may not be able to switch smoothly, and Git will give an error message. For example:
 $ git branch
      bug/pr-1
      bug/pr-2
      dev
    * master
    
$ git checkout dev
    error: Your local changes to the following files would be overwritten by checkout:
        NewStuff
    Please, commit your changes or stash them before you can switch branches.
    Aborting
Git reminds us: Either submit all the changes, or use the stash name to store these changes.
What is puzzling is that Git does not prevent us from switching branches every time. For example, when we create a new branch based on the last commit of the active branch, we can switch to the new branch regardless of whether there are uncommitted changes locally. Why?
A preliminary exploration of the principle
Suppose you are currently in branch branch1, and you want to switch to branch branch2, so you enter the command
git checkout branch2
1
What does this command mean for your workspace?

For every file on branch 1 but not on branch 2, Git will delete them;
for every file on branch 2 but not on branch 1, Git will create them (with appropriate content);
for every file that is already on branch 1. If the version of the file in branch 2 is different, then Git will update the file in the workspace to match the version of branch 2.

Each of the above steps may destroy your current work area (work area and temporary storage area are shared for each branch).
For 1, delete a file. If the version of the file in the workspace is the same as its version in branch 1, then it is safe to delete it; if you modify it and haven't submitted it yet, then it is not safe to delete it;
For 2, create a file, if the file does not exist in the workspace, it is safe; if the file already exists in the workspace, but the content is "wrong", then it is not safe;
for 3, if the file has been submitted , Then it is safe; if it is modified without committing, then it is not safe;
Note: It is always “safe” to create and switch to a new branch with the command git checkout -b <newbranch>: no files are created, No files have been deleted, no files have been updated, and no changes have been made to the index.
But using git checkout -b <newbranch> <start_point> is different. Git will apply the aforementioned security check rules.

 

 

Note : At this time, if you modify /delete/add the files in the newbranch branch

1. There is no commit, and then git checkout oldbranch, the switch will be successful at this time, and the relevant files in the oldbranch file will be changed to the file content in newbranch.

2. After commit, then git checkout oldbranch, the switch will be successful at this time, and the relevant files in the oldbranch file will be changed to the file content in the oldbranch (the performance is the same as '1'). However, if you change the file later, switch to b1 without commit/stash, it will prompt error (security check will be performed at this time) .

Guess you like

Origin blog.csdn.net/kh815/article/details/102544584