Git基础命令学习

参考链接:https://www.cnblogs.com/wangwenhui/p/10546635.html 

以前用 IDE 的时候只知道一个劲的 commit,现在该好好回想一下 Git 那些最基础的命令了。

Git的三种状态

已提交(commited),已修改(modified)和已暂存(staged)

1. 已提交(commited):表示数据已经安全的保存在本地数据库中。

2. 已修改(modified):已经修改了文件,但是还没有保存到本地数据库中。

3. 已暂存(staged):表示对一个已经修改的文件的当前版本做了标记,使其包含在了下次的提交中。

基本的Git工作流程

1. 在工作目录中修改文件。(modified)

2. 暂存文件,将文件的快照放入暂存区。(staged)

3. 提交更新。找到暂存区域的文件,将快照永久性的存储到Git仓库。(commited)

git init : 初始化Git仓库。 该命令会在你当前所在的目录创建一个.git文件夹

git add . :将当前所在目录中的所有文件加入到暂存区

git add README.md : 将README.md文件加入到暂存区(让git去跟踪它)

git commit -m "Initial commit" : 将暂存区的文件提交到Git仓库

git clone https://github.com/2392863668/GitTest.git

工作目录中的文件主要有两种状态:已追踪(tracked)和未追踪(untracked)

1.已追踪(tracked):已追踪的文件指的是已经被纳入到版本控制的文件。已追踪的这些文件又可能处于已修改(modified),未修改(unmodified),已暂存(staged)的状态。

当你克隆了一个远程仓库到本地的时候,工作目录中所有的文件都是已追踪的文件,而且都是处于未修改的状态。

2.未追踪(untracked):未追踪的文件表示不被纳入版本控制的文件,除了已追踪的文件之外所有的文件都是未追踪文件。例如当你克隆了一个项目到本地之后,你在本地的项目中新添加了一个文件,那么这个文件就是未追踪的状态。

1.未追踪的文件(Untracked)在 git add 之后会变成已暂存(staged)状态。

2.未修改的文件(Unmodified)在被修改编辑之后会变成已修改(modified)状态。

3.已修改(modified)的文件在 git add之后会变成已暂存(staged)状态。

4.已暂存的文件(staged)在 git commit 之后会变成未修改(Unmodified)状态。

5.未修改的文件(Unmodified)在 git rm 之后会变成未跟踪(Untracked)状态(注意git rm会删除掉指定的文件)。

git status

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

git diff 

 1. git diff :查看尚未暂存的文件更新了哪些部分。

 2. git diff --staged :查看已暂存的文件更新了哪些部分。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ vim c.txt  # 修改文件 c.txt ,新行添加内容 hello world (c.txt 已 tracked)

$ git diff
diff --git a/c.txt b/c.txt
index 7209f0a..c068359 100644
--- a/c.txt
+++ b/c.txt
@@ -1,3 +1,4 @@
 rebase test
 add one line in c.txt by user B
 add one line in c.txt by user B again
+hello world
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git diff -- staged

--------------------------------------------------------------------------
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git add c.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git diff

$ git diff --staged
diff --git a/c.txt b/c.txt
index 7209f0a..c068359 100644
--- a/c.txt
+++ b/c.txt
@@ -1,3 +1,4 @@
 rebase test
 add one line in c.txt by user B
 add one line in c.txt by user B again
+hello world

 git add 

  当在工作目录新建一个文件,运行 git status ,会显示这个文件是untracked状态。这时候需要使用 git add  将这个文件加入暂存区,也就是告诉Git需要去跟踪这个文件。git add 使用文件或者目录的路径作为参数;如果参数是目录的路径,那么将该目录下所有的文件加入暂存区,Git将会跟踪这个目录下面的所有文件。

  如果文件是tracked状态,当修改了Git追踪的一个文件之后,运行 git status 会看到 Changes not staged for commit ,说明已跟踪的文件发生了变化但是还没有被加入到暂存区,这时候需要运行 git add 将该文件放入暂存区。

.gitignore

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ touch e.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

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

        e.txt

nothing added to commit but untracked files present (use "git add" to track)

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ touch .gitignore

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ vi .gitignore  # 在 .gitignore 添加以下两行内容
.gitignore
e.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status  # e.txt 不再显示为 Untracked files
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

git commit

在暂存区准备完毕之后,就可以commit了。注意:在提交之前一定要确认还有什么没有修改过的活新建的文件还没有 git add 过,否则commit的时候不会记录这些还没暂存起来的变化,这些修改过的文件只会保留在本地磁盘(也就是工作区)。在每次提交之前,可以使用 git status 查看一下,文件是不是已经都暂存起来了。

1.  git commit 如果不指定-m参数,那么git会打开一个编辑器,让你输入提交信息。

2. git commit -m   指定-m参数,可以在后面直接指定提交信息。(推荐使用)

3. git commit -a -m 有时候,在修改某一个已经被 tracked 的文件之后,可以直接使用 git commit -a -m "info" 跳过add步骤而将这个文件提交。注意:这条命令会将已经被 tracked 的文件加入到暂存区,然后进行提交;对于 untracked 的文件(例如新添加的文件),在修改之后是不能使用这条命令直接进行提交的。

commit记录的是放在暂存区域的快照,每一次提交操作都是对项目做一次快照,以后可以回到这个状态,或者进行比较。

$ git commit -m "modify c.txt"
[master 70f19e6] modify c.txt
 1 file changed, 1 insertion(+)

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git show
commit 70f19e6d0788ad7fca07b129a8615084fe08f1e9 (HEAD -> master)
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 21:58:26 2020 +0800

    modify c.txt

diff --git a/c.txt b/c.txt
index 7209f0a..c068359 100644
--- a/c.txt
+++ b/c.txt
@@ -1,3 +1,4 @@
 rebase test
 add one line in c.txt by user B
 add one line in c.txt by user B again
+hello world

git push

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 286 bytes | 286.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:2392863668/GitTest.git
   082b011..70f19e6  master -> master

 git rm

1. git rm

  执行以下命令:删除一个文件。

$ git rm 
$ git commit -m 
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git rm d.txt
rm 'd.txt'

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        deleted:    d.txt


Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git commit -m "delete d.txt"
[master 32b9c4e] delete d.txt
 1 file changed, 4 deletions(-)
 delete mode 100644 d.txt

   如果被删除的文件修改过,而且已经被放入了暂存区,那么需要加上 -f 参数,强制删除。这是一种安全特性,用于防止删除还没有被添加快照的数据,这种数据无法被Git恢复。

2. git rm --cached

如果你只是想从暂存区删除文件,但是工作区的文件保持不变(将文件保存在磁盘),也就是说将文件保存在磁盘但是不想让 Git 进行跟踪,使用如下命令即可:

$ git rm --cached 
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ vi test.txt  # 新行添加 hello world
git
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   test.txt

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

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git rm --cached test.txt
rm 'test.txt'

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status  # 显示文件 test.txt 为 Untracked files
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        deleted:    test.txt

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

        test.txt


 git mv

  可以使用 git mv 来将文件重命名(在被 tracked 的情况下),会将 oldFile 重新命名为 newFile 。

$ git mv  
这条命令会:1.将文件改名;2.将改名之后的文件add进暂存区。等价于下面三条命令。

$ mv oldFile newFile
$ git rm oldFile
$ git add newFile
Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git add test.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git mv test.txt ./test/test.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        renamed:    test.txt -> test/test.txt


 git log

1. git log 

列出所有commit,最新的commit在最上面。会显示每个提交的作者,提交信息等。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git log
commit ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master)
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 22:28:31 2020 +0800

    mv test.txt to test directory

commit 32b9c4ee0b74b629e1d1e99f1d0173a831638d7c
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 22:15:01 2020 +0800

    delete d.txt

commit 70f19e6d0788ad7fca07b129a8615084fe08f1e9 (origin/master, origin/HEAD)
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 21:58:26 2020 +0800

    modify c.txt
...

2. git log -p -2

显示最近的两次提交每个文件修改了哪些地方。-p用来显示每次提交修改了哪些地方。-2用于指定只显示最近的两次提交。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git log -p -2
commit ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master)
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 22:28:31 2020 +0800

    mv test.txt to test directory

diff --git a/test.txt b/test/test.txt
similarity index 71%
rename from test.txt
rename to test/test.txt
index 42dfcfc..041d1e3 100644
--- a/test.txt
+++ b/test/test.txt
@@ -1,2 +1,3 @@
 I am looking
 nice to meet you
+hello world

commit 32b9c4ee0b74b629e1d1e99f1d0173a831638d7c
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 22:15:01 2020 +0800

    delete d.txt

diff --git a/d.txt b/d.txt
deleted file mode 100644
index 92faea9..0000000
--- a/d.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-testing
-hello, hello
-conflict testing
-rebase test
(END)

3. git log --stat

显示每次提交简略的统计信息。具体包括:在每次提交的下面列出所有被修改过的文件、有多少文件被修改了、被修改的文件有哪些行被移除或添加以及显示提交信息。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git log --stat
commit ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master)
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 22:28:31 2020 +0800

    mv test.txt to test directory

 test.txt => test/test.txt | 1 +
 1 file changed, 1 insertion(+)

commit 32b9c4ee0b74b629e1d1e99f1d0173a831638d7c
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 22:15:01 2020 +0800

    delete d.txt

 d.txt | 4 ----
 1 file changed, 4 deletions(-)

commit 70f19e6d0788ad7fca07b129a8615084fe08f1e9 (origin/master, origin/HEAD)
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 21:58:26 2020 +0800

    modify c.txt

 c.txt | 1 +
 1 file changed, 1 insertion(+)

4. git log --pretty=oneline

将指定使用不同与默认格式的方式展示提交历史。 比如oneline会将提交历史展示成一行,类似于下面这样。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git log --pretty=oneline
ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master) mv test.txt to test directory
32b9c4ee0b74b629e1d1e99f1d0173a831638d7c delete d.txt
70f19e6d0788ad7fca07b129a8615084fe08f1e9 (origin/master, origin/HEAD) modify c.txt
082b01177112fc3ccb93f234435fb06a930c19fb add hello world in b.txt
d38440866faa62ff1a2c383be1fc780bbbb859f7 mv a.txt to test directory
648e3e12da7fc4f1a13d21e3f0c50c94d5950c25 Merge branch 'dev' to 'master'
d2009f78a7f83f8ae431caabac79d6a3978ada7a add one line in c.txt by user B again
4505d98d63671b307aa5fcf093412d9be09012e5 add one line in b.txt by user B again
cc12a897b784bc4ca660650446e66d3e81b73263 add one line in a.txt by user A again
5afad542370810acaba829a1c7d8cdbb0c18dde9 add one line in c.txt by user B
a7b960d2fa25a0af07816bfd804cbf681894a180 add one line in b.txt by user B
3ec68b58c6a825e8e87cb6af42395832280531f1 add one line in a.txt by user A
26a6eb5ffbeafd862cfcf244453f27a14ad3b417 add one line in d.txt
c0527a3f99463edae7363a0776a7b1444330f161 add one line in a.txt
deba76262ef797c862feeb5c8560314d45589092 Merge branch 'master' of github.com:2392863668/GitTest
...

5. git log /path/to/file  

命令可以查看指定路径的历史记录

$ git log ./test
commit ffd9ca8866ee80716db00108e32b36dc59f8b77d (HEAD -> master)
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 22:28:31 2020 +0800

    mv test.txt to test directory

commit d38440866faa62ff1a2c383be1fc780bbbb859f7
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 20:53:13 2020 +0800

    mv a.txt to test directory

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$

6. git log --oneline --decorate --color --graph 

查看高级日志图

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git log --oneline --decorate --color --graph
*   cd3e336 (HEAD -> master, origin/master, origin/HEAD) Merge branch 'master' of github.com:2392863668/GitTest
|\
| * 3fceb8a add hello world in looking.txt
* | 640fd3c add another hello world in hello.txt
|/
* db41223 mv test.txt to test dir
* 32b9c4e delete d.txt
* 70f19e6 modify c.txt
* 082b011 add hello world in b.txt
* d384408 mv a.txt to test directory
*   648e3e1 Merge branch 'dev' to 'master'
|\
| * d2009f7 add one line in c.txt by user B again
| * 4505d98 add one line in b.txt by user B again
* | cc12a89 add one line in a.txt by user A again
|/
* 5afad54 add one line in c.txt by user B
* a7b960d add one line in b.txt by user B
* 3ec68b5 add one line in a.txt by user A
* 26a6eb5 add one line in d.txt
* c0527a3 add one line in a.txt
*   deba762 Merge branch 'master' of github.com:2392863668/GitTest
|\
| * 44fb153 testing again
* | 6565d76 (origin/dev) add something in b.txt
* | 2a79103 add something in a.txt
|/
* 2db0e2b conflict solved
* 28914cb testing again
* e9a2ce2 add new line in d.txt
* 45d0478 modify d.txt
* 3c85912 add d.txt
* 34a912e testing again

查看高级日志图

git commit --amend

        有时候我们在提交完成之后才发现有几个文件没有提交,或者发现提交信息填写错了,这时候可以使用 git commit --amend 尝试重新进行提交。

1.这条命令会将暂存区中的文件进行提交

2.在提交的时候可以修改上一次commit的提交信息

3.最终只有一次commit,第二次的commit会替代第一次的commit(也就是只会产生一个commit节点,使用 git log 会看到上一次的commit不见了,被这次的新commit替换)

$ git commit --amend

- mv test.txt to test directory
+ mv test.txt to test dir

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Jul 29 22:28:31 2020 +0800
#
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
# Changes to be committed:
#       renamed:    test.txt -> test/test.txt
#

$ git commit --amend
[master db41223] mv test.txt to test dir
 Date: Wed Jul 29 22:28:31 2020 +0800
 1 file changed, 1 insertion(+)
 rename test.txt => test/test.txt (71%)

git reset HEAD

 使用 git reset HEAD  可以将暂存的文件变为未暂存的文件。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ vi myGit.txt # 添加新行 hello world

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   myGit.txt

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

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git add myGit.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   myGit.txt


Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git reset HEAD myGit.txt
Unstaged changes after reset:
M       myGit.txt

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   myGit.txt

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

git checkout --

  当你对一个文件进行了修改,还没有add进暂存区,你想要放弃所有的修改,将文件恢复到没有修改之前的样子,这时候可以使用 git checkout --  

这条命令会将文件恢复到上一次 commit 的时候这个文件的样子。

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git checkout -- myGit.txt # 回到了我用 vi 修改文件 myGit.txt 之前的样子

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

强制拉取覆盖本地

如果你想以远程仓库的版本内容为准,丢弃本地所有修改(当然,本地的 untracked file 不属于版本控制的范围,不会受到这条命令的影响),又不想删除本地仓库以后再次 git  clone,就按下边这么做吧。

git fetch --all && git reset --hard origin/master && git pull

git rebase -i HEAD~2

如果你想把本地的多个 commit 合并成一个,就用这个吧(从 git log 可以看到,两次 commit 变成了一个,两次 commit 所做的修改也反映到了一个 commit 上):

[root@master GitTest]# git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
[root@master GitTest]# git rebase -i HEAD~2

pick 1a6cb23 add hello in new line of looking.txt
squash 636b8a7 add world in new line of looking.txt

# Rebase cd3e336..636b8a7 onto cd3e336
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
"~/GitTest/.git/rebase-merge/git-rebase-todo" 20L, 706C

[root@master GitTest]# git rebase -i HEAD~2
[detached HEAD f3b4d4a] This is a combination of 2 commits.
 1 file changed, 2 insertions(+)
Successfully rebased and updated refs/heads/master.
[root@master GitTest]# git log
commit f3b4d4abd65a59c8a03df6caccf28f33422b4614
Author: looking 
Date:   Tue Aug 4 21:42:27 2020 +0800

    This is a combination of 2 commits.
    
    add hello in new line of looking.txt
    
    add world in new line of looking.txt

commit cd3e33625578c3aaf6f19b69c878559a1dcf552a
Merge: 640fd3c 3fceb8a
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 23:49:34 2020 +0800

    Merge branch 'master' of github.com:2392863668/GitTest

commit 640fd3c8eb8ab632691d448b1df216ab031a9071
[root@master GitTest]# git log --stat
commit f3b4d4abd65a59c8a03df6caccf28f33422b4614
Author: looking 
Date:   Tue Aug 4 21:42:27 2020 +0800

    This is a combination of 2 commits.
    
    add hello in new line of looking.txt
    
    add world in new line of looking.txt

 looking.txt | 2 ++
 1 file changed, 2 insertions(+)

commit cd3e33625578c3aaf6f19b69c878559a1dcf552a
Merge: 640fd3c 3fceb8a
Author: 2392863668 <[email protected]>
Date:   Wed Jul 29 23:49:34 2020 +0800

    Merge branch 'master' of github.com:2392863668/GitTest

git tag

1. git tag

git tag 会按照字母顺序列出所有的标签

git tag -l 'v1.0*' 列出v1.0系列的标签。

[root@master GitTest]# git tag
v1.0
v1.1

                                             Git基础命令学习_第1张图片

2. git show

显示标签的相关信息,比如被打在了哪个 commit 上。

[root@master GitTest]# git show v1.1
commit f376be22b18d839c18cf6596f1c156a757c734e8
Author: 2392863668 <[email protected]>
Date:   Wed Jun 5 14:11:52 2019 +0800

    add something to test.txt

diff --git a/test.txt b/test.txt
index e69de29..b7d1e74 100644
--- a/test.txt
+++ b/test.txt
@@ -0,0 +1 @@
+I am looking

3. git push origin

如果想将本地的tag上传到远程仓库,则必须显示的将tag push上去。默认情况下 git push  是不会将tag上传到远程仓库的。一次推送许多标签: git push origin --tags  会将不在远程仓库的所有标签上传上去。

[root@master GitTest]# git tag -a v1.2 -m "version 1.2"
[root@master GitTest]# git tag
v1.0
v1.1
v1.2
[root@master GitTest]# git push origin v1.2
Counting objects: 1, done.
Writing objects: 100% (1/1), 154 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To [email protected]:2392863668/GitTest.git
 * [new tag]         v1.2 -> v1.2

                                       Git基础命令学习_第2张图片

4. git tag -d

[root@master GitTest]# git tag -d v1.2
Deleted tag 'v1.2' (was d332b35)
[root@master GitTest]# git push origin :refs/tags/v1.2
To [email protected]:2392863668/GitTest.git
 - [deleted]         v1.2

git remote

1. git remote -v 

查看远程URL,如果有多个的话会都列出来(比如可能会同时 push 到多个仓库的话,就有多个 push 的 URL)

[root@master GitTest]# git remote -v
origin	[email protected]:2392863668/GitTest.git (fetch)
origin	[email protected]:2392863668/GitTest.git (push)
[root@master GitTest]# 

2. git push origin master

注意:只有当你对远程仓库具有写入权限的时候,这条命令才会生效。当你在与别人合作开发一个项目的时候,如果,他们先推送了自己的代码到了远程仓库,你在直接进行推送,会被拒绝,你必须先将他们的代码先拉取下来进行合并(merge or rebase)之后,才能进行推送。

[root@master GitTest]# git push origin master
Everything up-to-date

3. git remote show origin 

[root@master GitTest]# git remote show origin
* remote origin
  Fetch URL: [email protected]:2392863668/GitTest.git
  Push  URL: [email protected]:2392863668/GitTest.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

4. git remote rm 

移除远程仓库(这块我就不多尝试了)

[root@master GitTest]# git remote rm origin
[root@master GitTest]# git remote add origin [repo-url]

 

你可能感兴趣的:(Git,git,版本控制)