Use of Git

This article is reproduced from: https://www.liaoxuefeng.com/
If there is any infringement, please contact the blogger to delete the article.



Enter at the command line:

$ git config --global user.name "Your Name"
$ ​​git config --global user.email "[email protected]"

Because Git is a distributed version control system, each machine must Self-reported home: your name and email address. You might be worried, what if someone deliberately impersonates someone else? There is no need to worry about this. First of all, we believe that everyone is a kind and ignorant masses. Second, there are ways to check if there are people who are pretending to be.


First, choose a suitable place and create an empty directory:


$ mkdir learngit
$ cd learngit
$ pwd

/Users/michael/learngit The
pwd command is used to display the current directory. On my Mac, this repository is located at /Users/michael/learngit.


If you use a Windows system, in order to avoid encountering various inexplicable problems, please make sure that the directory name (including the parent directory) does not contain Chinese.


The second step is to turn this directory into a repository that Git can manage through the git init command:


$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/ The first step is to
initialize the git repository




with the command

git add

Tell Git to add the file to the repository:



$ git add readme.txt
executes the above command, nothing is displayed, that's right, the Unix philosophy is "no news is good news", indicating that the addition is successful.


The second step, use the command
git commit
to tell Git to submit the file to the repository:


$ git commit -m "wrote a readme file"
[master (root-commit) cb926e7] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt
briefly explain the
git commit command, the input after -m is the description of this commit, you can enter any content, of course, it is best to make sense, so that you can easily find the changes from the history Record.


The git status command allows us to keep abreast of the current status of the warehouse


Use the command git diff <filaname> to see what has been modified

There must be some command in the version control system that can tell us the history. In Git, we use the git log command to view:


If you think that the output information is too much and you are dazzled, you can try adding the --pretty=oneline parameter:

$ git log --pretty=oneline

Use the git reset command

$ git reset --hard HEAD^

The previous version is HEAD^, and the previous version is HEAD^^. Of course, it is easier to write 100 ^ for the previous 100 versions, so it is written as HEAD~100.


The latest version append GPL can no longer be seen! For example, you took a time shuttle from the 21st century to the 19th century. If you want to go back, you can't go back. What should you do?
In fact, there are still ways. As long as the above command line window has not been closed, you can follow it up and find it, and find that the commit id of the append GPL is 3628164..., so you can specify back to the future A version of:
$ git reset --hard 3628164

HEAD is now at 3628164 append GPL


Now, you roll back to a certain version, turn off your computer, regret it the next morning, and want to revert to a new version? What should I do if the commit id of the new version cannot be found?
In Git, there is always regret medicine. When you go back to the add distributed version with $ git reset --hard HEAD^, and then want to revert to the append GPL, you must find the commit id of the append GPL. Git provides a command git reflog to log every command you make:

$ git reflog


summary:

The version pointed to by HEAD is the current version, so Git allows us to traverse the history of versions using the command git reset --hard commit_id.

Before the shuttle, use git log to view the commit history to determine which version to roll back to.
To go back to the future, use git reflog to view the command history to determine which version of the future to go back to.

git checkout -- file can discard the modification of the workspace:
$ git checkout -- readme.txt
The command git checkout -- readme.txt means that all the modifications of the readme.txt file in the workspace are undone. There are two situations:
One is that readme.txt has not been placed in the temporary storage area since it was modified. Now, if you revoke the modification, you will return to the same state as the version library; the other
is that readme.txt has been added to the temporary storage area and then modified. , now, undoing the modification returns to the state after it was added to the staging area.

In short, it is to return this file to the state of the last git commit or git add.


Use the command git reset HEAD file to undo the changes in the temporary storage area (unstage) and put it back into the workspace:

$ git reset HEAD readme.txt



Under normal circumstances, you usually delete useless files directly in the file manager, or delete them with the rm command:

$ rm test.txt


If you really want to delete the file from the repository, use the command git rm to delete it, and git commit

$ git rm test.txt

rm 'test.txt'

$ git commit -m "remove test.txt"

Another case is that it is deleted by mistake, because there are still files in the repository, so you can easily restore the mistakenly deleted files to the latest version:
$ git checkout -- test.txt




Guess you like

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