git -- 02 common operation

1, Create local warehouse - git init

# cd to working directory
$ pwd
/d/lihua/java code/my/spring-cloud-H

$ git init # Create local warehouse

2, Put the contents of the workspace into the staging area - git add filename

$ touch test.txt  # New folder

$ ls
cloud-01-consumer-hygtrix-dashboard9001/  cloud-04-eureka-server-7001/
cloud-01-provider-hygtrix-payment8001/    cloud-04-eureka-server-7002/
cloud-01-provider-payment8001/            cloud-04-eureka-server-7003/
cloud-01-provider-payment8002/            cloud-05-gateway-server-9001/
cloud-01-zk-provider-payment-8003/        cloud-06-zipkin-consumer-81/
cloud-02-consumer-feign-hystrix-order80/  cloud-06-zipkin-provider-8001/
cloud-02-consumer-feign-order80/          pom.xml
cloud-02-consumer-order80/                spring-cloud-H.iml
cloud-03-api-commons/                     test.txt

$ vim test.txt # Then write something

$ git add test.txt # Put test.txt into the staging area. Note: (git add.) you can put all files in the current directory into the staging area
warning: LF will be replaced by CRLF in test.txt.  # Ignore these
The file will have its original line endings in your working directory

3, View commit log - git log

$ git log # This command can view the record of our commit, that is, the record of using git commit
fatal: your current branch 'master' does not have any commits yet # Not used yet
be careful:
1. Untracked files: indicates that the file has never been added to the temporary storage area. This is a newly added file; add required
2. Changes not staged for commit: the file has been changed but has not yet entered the staging area, so add is required;
3. Changes to be committed: the file has entered the temporary storage area, but has not been submitted to the version Library (resource area), so commit is required;

4, View file status - git status filename

(1) All documents

$ git status # View the status of all files in the current directory.
On branch master 

No commits yet # Not submitted

Changes to be committed: 
  (use "git rm --cached <file>..." to unstage)
        new file:   test.txt # A new file was created

Untracked files: # The following files are untraceable
  (use "git add <file>..." to include in what will be committed)
        .idea/
        cloud-01-consumer-hygtrix-dashboard9001/
        cloud-01-provider-hygtrix-payment8001/
        cloud-01-provider-payment8001/
        cloud-01-provider-payment8002/
        cloud-01-zk-provider-payment-8003/
        cloud-02-consumer-feign-hystrix-order80/
        cloud-02-consumer-feign-order80/
        cloud-02-consumer-order80/
        cloud-03-api-commons/
        cloud-04-eureka-server-7001/
        cloud-04-eureka-server-7002/
        cloud-04-eureka-server-7003/
        cloud-05-gateway-server-9001/
        cloud-06-zipkin-consumer-81/
        cloud-06-zipkin-provider-8001/
        pom.xml
        spring-cloud-H.iml

(2) Specify file

$ git status test.txt # View the status of the specified file
On branch master

No commits yet

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

(3) After updating the file, add was not submitted

$ vim test.txt # Update this file

$ git status test.txt
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   test.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:   test.txt


5, Put all the contents of the staging area into the resource area - git commit -m "msg"

$ git add test.txt # Put the updated test.txt back into the staging area
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory

hs@DESKTOP-9D82N3U MINGW64 /d/lihua/java code/my/spring-cloud-H (master)
$ git commit -m "# Tip: put the contents of the buffer into the resource area"  # Put the contents of the buffer into the resource area. Note: - m is followed by the prompt of this submission
[master (root-commit) c5852f2] # Tip: put the contents of the buffer into the resource area
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
$ git status test.txt
On branch master
nothing to commit, working tree clean # Indicates that the file has been add ed and commit ted, and the latest test.txt is stored in the temporary storage area and resource area
$ git log # View the commit record
commit c5852f24b718b48f29654bfb0d6c25e4c8c3af3b (HEAD -> master)
Author: lihua <1559465552@qq.com>
Date:   Wed Nov 3 17:47:24 2021 +0800

    # Tip: put the contents of the buffer into the resource area

6, Fallback version -- git reset --hard commitId

Note: the git reset command can not only rollback the version, but also rollback the modification of the staging area to the workspace. When we use HEAD, it indicates the latest version.

We will modify test.txt, add and commit again

$ vim test.txt

hs@DESKTOP-9D82N3U MINGW64 /d/lihua/java code/my/spring-cloud-H (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory

hs@DESKTOP-9D82N3U MINGW64 /d/lihua/java code/my/spring-cloud-H (master)
$ git commit -m "Second submission"
[master 7592488] Second submission
 1 file changed, 1 insertion(+)


$ git log
commit 759248856e205aaf5d27e2cb5fcfa699f2b2783e (HEAD -> master)
Author: lihua <1559465552@qq.com>
Date:   Wed Nov 3 17:53:55 2021 +0800

    Second submission

commit c5852f24b718b48f29654bfb0d6c25e4c8c3af3b
Author: lihua <1559465552@qq.com>
Date:   Wed Nov 3 17:47:24 2021 +0800

    # Tip: put the contents of the buffer into the resource area

When we write code, we find that the code can't run after modifying the code. At this time, we can reply to the original code by revoking. When our code has been completely saved, it can't be undone. What to do? If you have managed projects with git, you can = reply to your code.
For example, we modified the above test.txt file twice. What if I undo these changes.

  1. Before modification:
  2. First modification:
  3. Second modification:
*Fallback to the previous version: git reset --hard HEAD ^, HEAD ^ means fallback 1 version, HEAD ^ ^ means fallback 2 versions, and HEAD~100 means fallback 100 versions. HEAD is actually a pointer to the current version.
*Fallback to the specified version: git reset --hard the commit id of the new version. The "new version of commit id" can only write the first few digits of the id number, and Git will find it automatically.
# First, use git log to view the submission record
$ git log
commit 759248856e205aaf5d27e2cb5fcfa699f2b2783e (HEAD -> master)
Author: lihua <1559465552@qq.com>
Date:   Wed Nov 3 17:53:55 2021 +0800

    Second submission

commit c5852f24b718b48f29654bfb0d6c25e4c8c3af3b
Author: lihua <1559465552@qq.com>
Date:   Wed Nov 3 17:47:24 2021 +0800

    # Tip: put the contents of the buffer into the resource area
$ git reset --hard c5852f24b718b48f29654bfb0d6c25e4c8c3af3b # Fallback to the specified version
HEAD is now at c5852f2 # Tip: put the contents of the buffer into the resource area

7, Undo changes - to a single file

(1) Before no add

$ vim test.txt

hs@DESKTOP-9D82N3U MINGW64 /d/lihua/java code/my/spring-cloud-H (master)
$ cat test.txt
123 hello git  Updated.
Second update
 Third modification, in remote gitee Modify, simulate other developers to modify the code.
Undo modification

Note: - there is a space between and test.txt

# Note: - there is a space between and test.txt
$ git checkout -- test.txt

hs@DESKTOP-9D82N3U MINGW64 /d/lihua/java code/my/spring-cloud-H (master)
$ cat test.txt
123 hello git  Updated.
Second update
 Third modification, in remote gitee Modify, simulate other developers to modify the code.

Note: the difference between the two commands

*git checkout branch name # switch to branch
*git checkout -- space filename # to undo the modification of the file

(2) After add

When you not only mess up the contents of a file in the work area, but also add it to the temporary storage area, you want to discard the modification in two steps. The first step is to undo the modification in the temporary storage area with the command git reset HEAD, put it back into the work area, and then undo the modification of the file through [git checkout -- Blank filename].

(3) After commit

Consistent with the operation after add:
When you have submitted inappropriate modifications to the version library, you want to cancel this submission. Refer to the section on version rollback, you can rollback to the previous version, but only if you have not pushed your local version library to the remote.

8, Delete file

After deleting files manually or with the command RM < filename >, execute git add / RM < filename > and git commit-m < message >.
  in another case, the deleted file is deleted incorrectly. Because there are still files in the version library, it is easy to restore the wrongly deleted file to the latest version git checkout -- file.
git checkout actually replaces the version of the workspace with the version in the version library. No matter whether the workspace is modified or deleted, it can be "restored with one click".

9, Bind local warehouse to remote warehouse

# Git REOTE add < warehouse name, customized, because multiple remote warehouses can be bound at the same time, such as gitee and GitHub >
$ git remote add giee https://gitee.com/wuyujitang/spring-cloud-h.git

# If the binding fails, it indicates that it may have been bound. Delete the previous before binding.
git remote rm gitee

10, Push all the contents of the resource area to the remote warehouse - push

Note that before pushing, the content of the remote warehouse needs to be synchronized to the local warehouse, that is, the remote files must be available locally. Only when the file is synchronized can the content of the resource area be pushed to the remote warehouse.

# Direct push of unsynchronized files. Will report an error
git push gitee master
To https://gitee.com/wuyujitang/spring-cloud-h.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/wuyujitang/spring-cloud-h.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# Synchronize remote files
$ git pull --rebase gitee master
From https://gitee.com/wuyujitang/spring-cloud-h
 * branch            master     -> FETCH_HEAD
Successfully rebased and updated refs/heads/master.
# Push after synchronizing files
$ git push gitee master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 396 bytes | 198.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/wuyujitang/spring-cloud-h.git
   c05dba1..afcb81b  master -> master

Before push:

After push:

11, Pull the contents of the remote warehouse - pull

$ git pull --rebase gitee master
From https://gitee.com/wuyujitang/spring-cloud-h
 * branch            master     -> FETCH_HEAD
Successfully rebased and updated refs/heads/master.

12, Check what the local and remote code has been modified

(1) Local

Use vim to modify the file and simulate modifying the code.

# When the code is modified, there is no add
$ git status test.txt
On branch master
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:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

# With this command, you can view what has been modified
$ git diff test.txt
diff --git a/test.txt b/test.txt
index db330f3..4ce8ffe 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 123 hello git  Updated.
+Second update

(2) Remote

Modify the test file on giee. git pull automatically performs git add /rm and git commit operations on the modified files pulled down. Therefore, the modification of the pulled files can be attributed to the comparison of the last submission.
git diff HEAD displays the difference between the working directory and the GIT warehouse, while git diff HEAD ^ displays the difference between the working directory and the GIT warehouse before the last submission. Therefore, after git pull, we can use git diff HEAD ^ to view the specific modifications of the pulled file.

# Note that the pulled items will automatically add and commit
$ git pull gitee master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 356 bytes | 44.00 KiB/s, done.
From https://gitee.com/wuyujitang/spring-cloud-h
 * branch            master     -> FETCH_HEAD
   ee53e34..5f37b5b  master     -> gitee/master
Updating ee53e34..5f37b5b
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)

$ git diff HEAD^
diff --git a/test.txt b/test.txt
index 4ce8ffe..86ec1ba 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
 123 hello git  Updated.
 Second update
+Third modification, in remote gitee Modify, simulate other developers to modify the code.

idea updates git remote code to see what changes have been made to the code

13, Version label

The command git tag < tagName > [commit id] is used to create a new label. By default, the label is printed on the latest committed commit. You can also specify a commit id. Get the commit id through git log

$ git commit -m "Release version v1.0"
[master f0e4823] Release version v1.0
 1 file changed, 1 insertion(+)

hs@DESKTOP-9D82N3U MINGW64 /d/lihua/java code/my/spring-cloud-H (master)
$ git tag v1.0

*The command git tag [commit id] is used to create a new label. By default, the label is printed on the latest committed commit. You can also specify a commit id
(find the commit id of historical submission: git log -- pretty = online -- abrev commit)
*The command git tag -a -m "describe" [commit id] can specify label information
*The command git tag allows you to view all tags
*You can use git show to view tag information

Tags: git gitee

Posted on Thu, 04 Nov 2021 08:57:40 -0400 by tidou