Moment For Technology

Git from abandonment to entry (vi): Checkout

Posted on Sept. 23, 2022, 2:40 a.m. by 羅雅萍
Category: The front end Tag: git

? This is the 17th day of my participation in the Gwen Challenge. Check out the details: Gwen Challenge

0 x00 ? preface

? Git getting started is linked below. Read the articles in that order at ?.

Git goes from quitter to starter column

This series of articles will help you quickly learn the basics of Git through examples and practices.

In this article, you'll learn how the checkout command functions and works.

Project initialization

Next, with the knowledge of the branches in the previous chapter, through the graphical way to illustrate what happens after the command is called!

Start by initializing a project for subsequent sample demonstrations. Create two branches: master and dev. There are two files file. TXT and readme.md in the project directory.

The project submission record is at ?.

0x01 Checkout

The git checkout command is used to switch branches or checkout content to the working directory.

Check out the content

The command checks out contents from the historical commit (or staging area) to the working directory without changing the HEAD pointer, meaning that the current branch does not change.

Unspecified commit

Use git checkout -- to checkout the content to the working directory. Git checkout is the same command structure as git checkout .

The command does not specify commit. By default, files in the current staging area are checked out overwriting files in the working directory.

In the initial project, change the file.txt content v5 to v6 by checkout. Then use git checkout -- file.txt to overwrite the file contents of the staging area.

The command flow is as follows: ?.

If the project file exists in the staging state, the command is overwritten with the file contents of the staging area. However, the file in the current staging area has not been committed and does not appear in the commit snapshot (next committed snapshot).

Next, verify with an example. In the initial project, change the contents of file.txt file V5 to v6 by staged; Then use git add file.txt to temporarily store your changes. Next change the file.txt content to todo.

After using git checkout -- file.txt command, the local file contents reset staged V6 by passively instead of last committed V5.

The command flow is as follows: ?.

Specify the submission

Use the git checkout command to overwrite the corresponding files in the staging area and working directory from the specified history commit check-out file.

Git checkout HEAD~2 file. TXT will update the file in HEAD~2 (the parent of the current commit node, node 8e47817) in the staging area and overwrite the corresponding files in the working directory. The current branch does not change, the HEAD pointer does not change.

HEAD Current submission node HEAD~ parent node of the current submission node HEAD~2 Parent node of the current submission node HEAD~N Parent node of the current submission node...... The parent node

After you run git checkout HEAD~2 file. TXT, the file is updated to v4, and the temporary file is updated to file. TXT. The HEAD pointer is still pointing to the master branch.

The command flow is as follows: ?.

Switch branch

Run git checkout to move the HEAD flag to the branch.

The contents of the staging area and working directory are the same as those of the commit node corresponding to HEAD. If you switch to an older branch, the working directory reverts to what it looked like when the branch was last committed. If Git can't do this cleanly, it will forbid branch switching.

First check the commit record and file status of the project. At this point, HEAD points to the master branch and the directory is clean. There are no modified or temporary files.

After switching branches, run git checkout dev and check the commit record and file status again. At this point, HEAD points to the dev branch and the directory is clean.

All files in the new commit node (7632A63) are copied to the staging area and working directory; Only files existing on the old commit node (7416512) are deleted, so the file readme.md is deleted, and the current directory only has the file file.txt (the file content is v3).

View staging area (index)

Use git ls-files to view the contents of the staging area (index). A staging area (index) is not technically a tree structure, but is actually implemented as a flat list.

$ git ls-files -s
Copy the code

At this point, the staging area (index) only has the file file.txt, and the file readme.md has been removed in the update.

Simple merge

Checkout is safe for the working directory and checks to make sure it doesn't lose files that have changed. It will first try a simple merge in the working directory so that all files that have not been modified will be updated. If the merge is not possible, switching branches is prohibited.

In the initial project, the content of file.txt file was modified first, and then the branch switch was tried. It was shown that there was a conflict in the overwrite operation, and the switch command was terminated.

Ignore modified or staged files

As you can see above, files that only exist in the old commit node will be deleted. If there are modified or temporary files will be ignored, will not be affected, the state is preserved.

In the initial project, add the file lib\simplegit.py.

print('hi,git! ') 
Copy the code

Add it to the staging area with git add lib/simplegit.py, and continue to modify the lib\simplegit.py content.

print('hello,git! ') 
Copy the code

The status of the working directory is ?.

At this point, the staging area (index) file is as follows ?.

After you run git checkout dev to switch branches, the lib/simplegit.py file remains.

Check the directory status and file contents. The modified or temporary contents of the lib/simplegit.py file are retained.

View the index contents, and the temporary state of lib/simplegit.py is kept up to date.

The command flow is as follows: ?.

0 x02 ? reference

"Reset Reveal ",ebook "git checkout",ebook "git checkout",docs

0x03 Attention column

This article has been included in the column ?, you can directly follow.

Read more | The series continues to be updated

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.