Git (2)-repository warehouse

Git (2)-repository warehouse

Git init will initialize an empty Git repository. At the same time, after we execute git init, a .git directory will be automatically created in the current directory. This directory is used by Git to track and manage the repository.

$ mkdir gitDemo
$ cd gitDemo
$ git init
Working Directory
$ touch test.txt
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test.txt

nothing added to commit but untracked files present (use "git add" to track)
Staging area (Stage or Index)
$ git add test.txt
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file: test.txt
Submit the contents of the staging area to the local warehouse
git commit -m"init test"
[master (root-commit) c147eb0] init test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

git checkout

Modify the file test.txt and add the file content 123456. If you want to undo the added file content, execute git checkout, but the prerequisite is that the file status is in the work area without performing the git add operation.

git checkout - file (file name)

Among them - (two horizontal lines) is very important, without --, it becomes a command for git to switch branches

$ vim test.txt
$ cat test.txt
123456
$ git checkout test.txt
$ cat test.txt

git reset HEAD

The content of the modified file is qwerty, and the git add operation is performed at the same time. The file is temporarily in the temporary storage area, and the modification of the temporary storage area is cancelled. The git reset HEAD command

$ vim test.txt
$ cat test.txt
qwerty
$ git add test.txt
$ git reset HEAD test.txt # Cancel the temporary storage area
Unstaged changes after reset:
M test.txt
$ git checkout test.txt # Undo the modification
$ cat test.txt

Cancel commit modification

git reset --soft #Canceled the commit  
git reset --mixed (default) # canceled commit, canceled add
git reset --hard #Cancel commit, cancel add, cancel workspace modification

HEAD represents the commitid of the latest submission of the current version, the current latest submission 972cc6e5fb6d6edd53fecd88f343cab46ad62cbe, the previous version is HEAD, the previous version is HEAD, of course, we write 10 of the previous 10 versions. Of course, we don’t write this way, we can use HEAD ~10, the tilde + number represents how many times the previous commit should be rolled back.

$ git log
commit 972cc6e5fb6d6edd53fecd88f343cab46ad62cbe (HEAD -> master)
Author: baxiang <baxiang@roobo.com>
Date: Sun Dec 23 01:04:30 2018 +0800

    rollback two

commit 7c75daeda34530e698c208a5cedfacf1f1629b11
Author: baxiang <baxiang@roobo.com>
Date: Sun Dec 23 01:03:29 2018 +0800

    rollback first
$ git reset --hard HEAD^
HEAD is now at 7c75dae rollback first
$git log 
commit 7c75daeda34530e698c208a5cedfacf1f1629b11 (HEAD -> master)
Author: baxiang <baxiang@roobo.com>
Date: Sun Dec 23 01:03:29 2018 +0800

    rollback first

Code submission to restore git reflog View git operation records

$ git reflog
7c75dae (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
972cc6e HEAD@{1}: commit: rollback two
7c75dae (HEAD -> master) HEAD@{2}: commit: rollback first

Executing git reset --hard + the commit id that needs to be rolled back can be restored to the commit record of rollback two.

$ git reset --hard 972cc6e
HEAD is now at 972cc6e rollback two
$ git log
commit 972cc6e5fb6d6edd53fecd88f343cab46ad62cbe (HEAD -> master)
Author: baxiang <baxiang@roobo.com>
Date: Sun Dec 23 01:04:30 2018 +0800

    rollback two

commit 7c75daeda34530e698c208a5cedfacf1f1629b11
Author: baxiang <baxiang@roobo.com>
Date: Sun Dec 23 01:03:29 2018 +0800

    rollback first

git commit --amend

--amend will overwrite the last commit to modify the message (content)

$ vim test.txt
$ cat test.txt
123456
$ git add test.txt
$ git commit -m "update test file"
$ git log
commit ad1e86d2e6d14694d5965c9c53541d49bc451a72
Author: xxxx
Date: xxxxx

    update test file

commit c147eb0517f6399895367a97ca204bd0ca4a1736
Author: xxxxx
Date: xxxxxx

    init test
$ vim test.txt
$ cat test.txt
654321
$ git add test.txt
$ git commit --amend -m"second update"
$ git log
commit 84f20e85cd39090a8d16aecc58bcd04d8ae625a5
Author: xxxxx
Date: xxxxx

    second update

commit c147eb0517f6399895367a97ca204bd0ca4a1736
Author: xxxx
Date: xxxx

    init test

git rm

Delete the tracked files in the cache

 git rm readme
rm'readme'
baxiangdeMacBook:gitDemo baxiang$ git status
At branch master
Changes to be submitted:
  (Use "git reset HEAD <file>..." to cancel staging)

    Delete: readme

git mv

Modify file name

git mv readme readme.md

git log

View the commit history, and you can also add --oneline to view it in a concise way

 $ git log --oneline
eddccf5 (HEAD -> master) delete readme
70afaaf init readme
b5b7d12 update index
501fdac js
46995cb (test) add style.css
3dccbfc add index

View the history of all branches

git log --all
commit fe52636baa64e10e5795c26dcf0434aca48dcead (HEAD -> dev)
Author: baxiang <baxiang@roobo.com>
Date: Wed Dec 12 00:39:25 2018 +0800

    update readme

commit b5b7d12749d468627bf0afd413ba5c2dfa16beaf (master)
Author: baxiang <baxiang@roobo.com>
Date: Wed Dec 12 00:26:53 2018 +0800

    update index

-n plus number to view only the last 2 submissions

$ git log -n2
commit eddccf59f9308c2b9fdd4409f5d167749622c5ee (HEAD -> master)
Author: baxiang <baxiang@roobo.com>
Date: Fri Dec 21 19:17:57 2018 +0800

    delete readme

commit 70afaaf5cfcf5f11c768a135043f345f4b464606
Author: baxiang <baxiang@roobo.com>
Date: Fri Dec 21 13:51:18 2018 +0800

    init readme

image.png

File difference comparison

git diff compares the difference between the work area and the temporary storage area

git diff
diff --git a/README.md b/README.md
index 57edfa4..2ca7848 ​​100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-test git
+git note

git diff --cached compares the difference between the staging area and the version library

git diff --cached
diff --git a/README.md b/README.md
index 9daeafb..57edfa4 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-test
+test git

git diff HEAD can view the difference between the workspace and the version library

git diff HEAD
diff --git a/README.md b/README.md
index 9daeafb..2ca7848 ​​100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-test
+git note

git stash staging

$ rm text.txt
$ git status
At branch master
Changes not yet staged for submission:
  (Use "git add/rm <file>..." to update the content to be submitted)
  (Use "git checkout - <file>..." to discard changes in the workspace)

    Delete: text.txt

The modification has not been added to the commit (using "git add" and/or "git commit -a")
$ git stash
Saved working directory and index state WIP on master: 972cc6e rollback two
$ git status
At branch master
No documents to submit, clean work area

View current temporary records

$ git stash list
stash@{0}: WIP on master: 972cc6e rollback two

Use git stash apply to restore, but it should be noted that the stash content is not deleted, which means it can be reused, you need to use git stash drop to delete

git stash apply
Delete text.txt
At branch master
Changes not yet staged for submission:
  (Use "git add/rm <file>..." to update the content to be submitted)
  (Use "git checkout - <file>..." to discard changes in the workspace)

    Modification: README.md
    Delete: text.txt

The modification has not been added to the commit (using "git add" and/or "git commit -a")

Using git stash pop will directly delete the temporary record

$ git stash pop
Delete text.txt
At branch master
Changes not yet staged for submission:
  (Use "git add/rm <file>..." to update the content to be submitted)
  (Use "git checkout - <file>..." to discard changes in the workspace)

    Delete: text.txt

The modification has not been added to the commit (using "git add" and/or "git commit -a")
Dropped refs/stash@{0} (c894dd4530c7826a438ec784d53d8328960b0c9c)

Ignore files

Create a file named .gitignore in the workspace, and provide ignore file content in common development languages ​​in GitHub's open source project/gitignorehttps://github.com/github/gitignore

image.png

$ echo'*.cpp'> .gitignore
$ touch test.cpp
$ git status
At branch master
Untracked files:
  (Use "git add <file>..." to include the content to be submitted)

    .gitignore

The submission is empty, but there are untracked files (use "git add" to establish tracking)

Untracked files

$ git status
At branch master
Your branch is 1 commit behind'origin/master', and you can fast forward.
  (Use "git pull" to update your local branch)

Untracked files:
  (Use "git add <file>..." to include the content to be submitted)

    "Java/Linux\347\232\204Java\347\216\257\345\242\203\351\205\215\347\275\256.md"
    Linux/2019-01-17.md
    "Linux/Fedora\351\205\215\347\275\256.md"
    "Linux/Linux-\346\226\207\344\273\266\346\220\234\347\264\242.md"
    "Linux/Linux-\347\243\201\347\233\230\347\256\241\347\220\206.md"
    "Linux/Linux-\350\275\257\344\273\266\347\256\241\347\220\206.md"
    "Linux/Linux\342\200\224\342\200\224-\346\226\207\344\273\266\347\256\241\347\220\206.md"
    "Linux/Linux\342\200\224\342\200\224NFS.md"
    "Linux/Linux\342\200\224\342\200\224\346\225\210\347\216\207\345\267\245\345\205\267.md"
    "Linux/Linux\342\200\224\342\200\224\347\224\250\346\210\267\347\256\241\347\220\206.md"
    "Linux/Linux\347\275\221\347\273\234\347\256\241\347\220\206.md"
    "Linux/\347\275\221\347\273\234\344\274\240\350\276\223\345\215\217\350\256\256\342\200\224\342\200/224TCP.md"
    "Linux/\347\275\221\347\273\234\344\274\240\350\276\223\345\215\217\350\256\256\342\200\224\342\200/224UDP\345\215\217\350\256\256.md"
    "Linux/\347\275\221\347\273\234\345\215\217\350\256\256\342\200\224\342\200\224IP\345\215\217\350\256/256.md"
    "Linux/\347\275\221\347\273\234\345\237\272\347\241\200\347\237\245\350\257\206.md"
    Web/C&C--/
    Web/Go/
    Web/HTML/
    Web/Java/
    Web/Linux/
    Web/MachineLearning/
    "Web/MySQL\342\200\224\342\200\224\347\216\257\345\242\203\351\205\215\347\275\256.md"
    Web/Python/
    Web/iOS/

The submission is empty, but there are untracked files (use "git add" to establish tracking)

For untracked files, do not want to submit to the staging area. It can be added to .gitingore and set to ignore file types or file directories. But sometimes it is necessary to clean up some temporary files and automatically generated files. Manual deletion is too troublesome. At this time, you can use the git clean command to help you complete this operation

-d #Delete untracked directories and files in the directory. If the directory contains other git warehouse files, they will not be deleted (-dff can be deleted).
-f #If clean.requireForce under git cofig is true, then the clean operation needs -f(--force) to enforce it.
-i #Enter interactive mode
-n #Does not actually perform the delete operation, only displays the list of files that will be cleaned up
-X #Delete only the files marked in .gitignore. Those files that are neither under git version control nor in .gitignore will be kept.
Reference: https://cloud.tencent.com/developer/article/1438618 Git (2)-repository-cloud + community-Tencent Cloud