前几天天遇到个烦心事,想把作业提交push到github,结果弄来弄去没弄懂,作业和其他本地全被clean掉了,回收站都没找着,贼烦。根本原因都是之前学习git的使用一知半解,只求现用,现在几乎忘了。现在看着官方文档捋一遍。
What is Git?
It is a version control system. Git uses the concept of 'snapshot' to update the version. For example, when a file under a project changes, the new version project will only keep the changed file for efficiency. Version stored files. This concept is very important, it is very helpful to understand the future use!
There are three operating states for data:
- Modified, indicating that the file was modified, but it was not submitted to the database
- It has been temporarily stored. The state after performing the add operation indicates that the current version of a modified file is marked to be included in the next submitted snapshot.
- Submitted, the status after the commit operation indicates that the data has been safely stored in the local database.
Three areas:
work area, temporary storage area and Git warehouse.
- Work area: the place where we usually modify
- Temporary area: saves the information about the changes to be submitted, generally in the Git repository directory, including the index and data (can be understood as a snapshot)
- Warehouse: save the submitted project (can be understood as a permanent save snapshot)
I won't write about the Git installation process, I can find it everywhere on the Internet.
Just look at the configuration after installation:
Configuration
After the perfect installation, we will set our own username and email address in the first step like this:
$ git config --global user.name "Elizabeth"
$ git config --global user.email "[email protected]"
Then we can use
$ git config --global user.name
$ git config --global user.email
View the global user name and email address.
git config command is used to set the configuration of the variable GIt,
different users and different warehouses can be set different configurations
of these variables are stored in three different positions:
/etc/gitconfig
, ~/.config/git/config
, .git/config
respectively the global configuration of all users, the current user profile, and the current configuration warehouse , The higher the level from left to right, it will cover the former configuration.
Configure warehouse users:
$ git config user.name "Elizabai"
$ git config user.email "[email protected]"
View warehouse users
git config user.name
View global / warehouse configuration
$ git config --global --list
$ git config --list
Get Git repository
Two ways:
- Convert local directory to Git repository
- Clone remote warehouse
1. The first type:
enter the directory where version control is required and execute the command:
$ git init
The .git directory appears:
here you only need to know that this is the backbone of the warehouse
and then add the file to the warehouse:
$ git add test1.txt
$ git commit -m 'initial project version'
2. The second
$ git clone https://github.com/libgit2/libgit2 mylibgit
Create a directory named mylibgit, then initialize a .git file and clone from the remote repository.
Various states
We create a file
test2.txt with the content ABC at
this time to git status
view the current status:
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test2.txt
nothing added to commit but untracked files present (use "git add" to track)
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
It shows that we are now in the master branch, and also lists untracked files, and suggestions.
Then we git add 文件/目录
add to the staging area:
$ git add test2.txt
Look at the state again
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test2.txt
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
List files added to staging area
We can use git rm --cached 文件/目录
to remove the temporary storage area
If you use git rm 文件/目录
the file that git will work jointly staging area directory deleted.
After adding to the temporary storage area
git add
, Git will track the changes of this file. Let's modify the content of the following test2.txt and add a D to
see the status:
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test2.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test2.txt
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
Two new tips: the
first one tells us that we can use git add <file> to update the staging area, and the
second one tells us that we can use git restore <file> to go back to what we modified before
We use the first one will return to the state before our modification, that is, the file content is only ABC.
See
we can use git diff
to compare the contents of the staging area and warehouse:
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git diff
diff --git a/test2.txt b/test2.txt
index 48b83b8..a6bddc4 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1 +1 @@
-ABC
\ No newline at end of file
+ABCD
\ No newline at end of file
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
Seeing the new file, the difference between the old file is only D, and there is no line break at the end of the two files
To see additional temporary storage to be added to the contents of the next commit, you can use git diff --staged
or git diff --cached
command. These two commands will compare the difference between the temporary file and the last file submitted
Submit the update
We submit the modified test2.txt git commit
to the local warehouse:
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git commit -m "第一次提交"
[master (root-commit) f40e8b9] 第一次提交
1 file changed, 1 insertion(+)
create mode 100644 test2.txt
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
Take a look at the status:
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git status
On branch master
nothing to commit, working tree clean
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
If we suspect that the above staging area is troublesome, Git also provides shortcuts git commot -a
to submit our changed files and staging area files, so that we git add
can submit directly even if we are too lazy .
View the submission history
Then in the previous section test2.txt, I added E and F respectively and submitted
them in two submissions. Then we can use the warehouse git log
to view the submission history:
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git log
commit 3868bcdb2f716c560177d1242875e84245f94ad7 (HEAD -> master)
Author: Elizabeth <Elizabeth@example.com>
Date: Wed Apr 8 17:09:58 2020 +0800
第三次提交
commit 0f06de25f32c9f390e5b83fb2eee055bcc05891c
Author: Elizabeth <Elizabeth@example.com>
Date: Wed Apr 8 17:09:32 2020 +0800
第二次提交
commit f40e8b946f2f5c39b26629cab15508f275ab0033
Author: Elizabeth <Elizabeth@example.com>
Date: Wed Apr 8 16:51:21 2020 +0800
第一次提交
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git log -p
commit 3868bcdb2f716c560177d1242875e84245f94ad7 (HEAD -> master)
Author: Elizabeth <Elizabeth@example.com>
Date: Wed Apr 8 17:09:58 2020 +0800
第三次提交
diff --git a/test2.txt b/test2.txt
index 402476b..1c5f8ba 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1 +1 @@
-ABCDE
\ No newline at end of file
+ABCDEF
\ No newline at end of file
commit 0f06de25f32c9f390e5b83fb2eee055bcc05891c
Author: Elizabeth <Elizabeth@example.com>
Date: Wed Apr 8 17:09:32 2020 +0800
第二次提交
diff --git a/test2.txt b/test2.txt
index a6bddc4..402476b 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1 +1 @@
-ABCD
\ No newline at end of file
+ABCDE
\ No newline at end of file
commit f40e8b946f2f5c39b26629cab15508f275ab0033
Author: Elizabeth <Elizabeth@example.com>
Date: Wed Apr 8 16:51:21 2020 +0800
第一次提交
diff --git a/test2.txt b/test2.txt
new file mode 100644
index 0000000..a6bddc4
--- /dev/null
+++ b/test2.txt
@@ -0,0 +1 @@
+ABCD
\ No newline at end of file
END
(Press q to quit) lists the information submitted each time and the difference compared to the previous one, often the hex is the version number.
Undo operation
Resubmit:git commit --amend
Cancel the temporary storage of a file after canceling the temporary operation of multiple files git reset HEAD <file>...
Undo file submission:, git checkout -- <file>...
but this is a bit dangerous, you can understand this command is: it will take out the latest version of the warehouse to overwrite the file! After taking out, the latest version of the file in the warehouse is also gone.
Both of these undo operations are risky, because the work area file was modified during our period.
Remote warehouse configuration
1. Add url: git remote add url名字 url
2. Check existing urlgit remote -v
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git remote add Url1 https://github.com/xxxx/xxxx.git
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git remote -v
Url1 https://github.com/xxxx/xxxx.git (fetch)
Url1 https://github.com/xxxx/xxxx.git (push)
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
Pull to
$ git fetch <remote>
pull the data that we do not have yet, you need to manually merge and
push
$ git push <remote> <branch>
Label
Create label: label
the current version v1, -m specify label information:
git tag -a v1 -m "my version 1.4"
push the current version to the server along with the label:
$ git push Url1 v1
delete the local warehouse label
git tag -d <tagname>
delete the remote warehouse label
$ git push Url1 :refs/tags/v1.4-lw
Switch version
View version number
Back to the previous version:
git reset --hard HEAD^
the previous version:
git reset --hard HEAD^
back to the previous version:
git reset --hard HEAD~3
back to the specified version
git reset --hard 91010df
back to view all iterative versions after back
git reflog