1. New files

1. Set the user signature

The function of the signature is to distinguish between different people, so that when viewing the version, you can know who is operating. You must set up your signature after installing Git for the first time, or you won’t be able to commit your code. In addition, the signature set here has nothing to do with the account you logged into the remote warehouse.

Git config --global user.email Git config --global user.emailCopy the code

2. Initialize the local library

If you want to use Git to manage files and directories, you must first get git to manage them, so you need to initialize them.

git init
Copy the code



Initialization succeeded, indicating that an empty Git repository was initialized.gitDon’t change the file here, you can open it.

3. Check the status of the local library

After initializing the local library, you can view the status of the local library.

git status
Copy the code



Take a look at what the three returned lines say:

  1. The local library is in the Master branch, which is generated by default.
  2. Nothing has been submitted yet.
  3. Except that you haven’t, and there’s nothing for you to submit right now.

Add a new file at this timehello.txtAnd use it togit statusIt’s different here.



A new line appears:Untracked filesFiles that have not been traced, marked in red. This file is only in the workspace, but is not tracked by Git.

The description on the last line was also changed to say that no files have been submitted yet, but there are files that have not been traced.

4. Add temporary storage area

Git prompts do or very friendly, basically will give you operation tips. Now, how to keep files traceable? Use Git add.

git add
Copy the code



There is awarningBecause I created the file directly in the git-bash window with the Linux command, I automatically converted the CRLF used in Win.

Now let’s review itgit status.



Green, green, just not Louis Koo (this is a reference to a legendary pager AD), but just nowhello.txtFile.

5. Delete the temporary storage area

Now this file is only in the staging area, and files in the staging area can be deleted. If I don’t want a historical version of the file now, delete it in the staging area before committing it to the local repository. Git also gives a hint.

git rm --cached <file>
Copy the code



Successfully deleted, but remember that this is the staging area, your local workspace files are not touched, usellCheck it out.

6. Commit the local library

Resubmit the file to the staging area, where it can then be committed to the local library to form a historical version.

Git commit -m "Log info" file nameCopy the code



Submit successfully, see the prompt message:

  • [master (root-commit) a70616da70616dThat’s the version number. This is the short version.
  • 1 file changed, 19 insertions(+), 1 file changed, 19 lines of information inserted (19 lines of text)

7. View the version information

The submission of the local library produces a version information that can be viewed using this command.

git reflog
Copy the code



The version information that was just committed is displayed.

  • a70616dThis is the version number.
  • (HEAD -> masterThe delegate pointer points to this first version.

    You can also use this command to view more trusted version information:
git log
Copy the code



You can see that the version number here is very longa70616d3fc1c69f948a7b0d4ed2b640bedb1e747This is the version number of the full version.

2. After the document is modified

The above is a new file, but most of the scene is the same file is repeatedly modified, now I go to modify hello. TXT file, add write content, and then git status.



A file has been modified, red means it has not been traced, then repeat the above action, submit to the staging area, usegit add. Submit successfully check again.

Finally commit the local library.

Here I see the last prompt, a new line, a deleted line, but I’m just adding something after the first line.

Because git maintains files on a line basis, I changed the first line. In fact, git always deletes the first line before adding the modified first line.

Git reflog



You can see:

  • There are two versions of information.
  • The pointer at this point is to the second version.

Version shuttle

For example, now I want to go back to an earlier version of history.

First look at the historical version,git reflogSo far I have 3 versions.



I’m now going to go back to the second submission and copy the publication number using the following command:

git reset --hard 94ca3de
Copy the code

When I look at the version, I see that the pointer has moved to the second version submitted.



The first line tells you that you did a reset operation and what the target version number is.

Git switching works by moving the HEAD pointer.

Git HEAD = “master”; git HEAD = “master”

Git /refs/heads/ git/refs/heads/ git/refs/heads/

When switching to a70616D, the version number in the master file will change to the corresponding version.

When we first submitted the third version, it would look something like this:

When we switch versions, we actually change the pointer, such as when we switch to version 2:

Next, branch correlation.