2021-05-12 14:32:11
一些常用的Git命令
整理了非常由於的Git一些常用命令,Git是目前世界上最先進的分散式版本控制系統。由於它就沒有中央伺服器的,每個人的電腦就是一個完整的版本庫,這樣,工作的時候就不需要聯網了,因為版本都是在自己的電腦上。既然每個人的電腦都有一個完整的版本庫,那多個人如何共同作業呢?比如說自己在電腦上改了檔案A,其他人也在電腦上改了檔案A,這時,你們兩之間只需把各自的修改推播給對方,就可以互相看到對方的修改了。
1、init,建立一個git倉庫
[root@linuxidc git]# cd /usr/local/
[root@linuxidc local]# mkdir github
[root@linuxidc local]# cd github
[root@linuxidc git]# git init
Initialized empty Git repository in /usr/local/github/.git/
[root@linuxidc git]# ls -a
. .. .git
2、status,檢查狀態
[root@linuxidc git]# touch code1.py
[root@linuxidc git]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# code1.py
nothing added to commit but untracked files present (use "git add" to track)
3、add,檔案新增到索引,也就是接受版本控制
[root@linuxidc git]# git add code1.py
[root@linuxidc git]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: code1.py
#
4、commit,提交
[root@linuxidc git]# git commit -m "New add code1.py" #第一次會提示我們輸入資訊,名字與郵件
[master 7894d32] New add code1.py
Committer: root <root@linuxidc.(none)>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name <you@example.com>'
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 code1.py
[root@linuxidc git]# git config --global user.name sunshine #名字
[root@linuxidc git]# git config --global user.email sunshine@git.com #郵件地址
[root@linuxidc git]# git config --global color.ui #auto顯示顏色
5、branc 建立、檢視、刪除分支
[root@linuxidc git]# git branch
* master
[root@linuxidc git]# git branch dev
[root@linuxidc git]# git branch
dev
* master
[root@linuxidc git]# git branch -d dev
Deleted branch dev (was 7894d32).
[root@linuxidc git]# git branch
* master
#刪除分支會有另外一種詭異的現象發生
[root@linuxidc git]# git checkout -b dev1 #建立並切換dev1分支
Switched to a new branch 'dev1'
[root@linuxidc git]# cat code.py
[root@linuxidc git]# echo "print 'Sunshine Good~'" > code.py #輸出內容至code.py
[root@linuxidc git]# cat code.py
print 'Sunshine Good~'
[root@linuxidc git]# git add code.py
[root@linuxidc git]# git commit -m "New add rom 'Sunshine Good~'"
[dev1 865c84e] New add rom 'Sunshine Good~'
1 files changed, 1 insertions(+), 0 deletions(-)
[root@linuxidc git]# git checkout master #切換至master
Switched to branch 'master'
[root@linuxidc git]# cat code.py #檢視mastercode.py,沒有內容
[root@linuxidc git]# git branch -d dev1 #刪除分支dev1報錯,倆中方法,第一dev1確實不是我需要的,那麼小d換成大D即可,第二個就下面步驟
error: The branch 'dev1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev1'.
[root@linuxidc git]# git merge dev1 #合併分支dev1至master
Updating bf266f5..865c84e
Fast-forward
code.py | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[root@linuxidc git]# cat code.py
print 'Sunshine Good~'
[root@linuxidc git]# git branch -d dev1 #使用小d刪除分支dev1,刪除成功
Deleted branch dev1 (was 865c84e).
[root@linuxidc git]#
6、checkout 切換 建立分支
[root@linuxidc git]# git checkout -b dev
Switched to a new branch 'dev'
[root@linuxidc git]# git branch
* dev
master
[root@linuxidc git]# git checkout master
Switched to branch 'master'
[root@linuxidc git]# git branch
dev
* master
7、clone 克隆一個repository 資訊在本地目錄,哪自己之前做好的gitolite
[root@Redis_master ~]# git clone git@127.0.0.1:dev
Initialized empty Git repository in /root/dev/.git/
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9/9), done.
[root@redis_master ~]# ls
anaconda-ks.cfg appendonly.aof dev id_rsa.pub install.log install.log.syslog
[root@redis_master ~]# cd dev/
[root@redis_master dev]# ls
test.txt
8、diff 對比檔案
[root@linuxidc git]# ls
code1.py code.py
[root@linuxidc git]# git status
# On branch master
nothing to commit (working directory clean)
[root@linuxidc git]# vim code1.py
[root@linuxidc git]# git diff #這裡是對比暫存區與工作區對比
diff --git a/code1.py b/code1.py
index e69de29..66708be 100644
--- a/code1.py
+++ b/code1.py
@@ -0,0 +1 @@
+print 'Sunshine Good!'
[root@linuxidc git]# git diff --staged #這裡是暫存區對比staged也就是我們commit的staged區
[root@linuxidc git]# git add code1.py #新增到暫存區
[root@linuxidc git]# git diff #工作區對比暫存區
[root@linuxidc git]# git diff --staged #暫存區對比commit的staged
diff --git a/code1.py b/code1.py
index e69de29..66708be 100644
--- a/code1.py
+++ b/code1.py
@@ -0,0 +1 @@
+print 'Sunshine Good!'
[root@linuxidc git]# git commit -m "code1.py New add row Sunshine Good" #提交
[master bf266f5] code1.py New add row Sunshine Good
1 files changed, 1 insertions(+), 0 deletions(-)
[root@linuxidc git]# git diff --staged #對比暫存區以staged區
[root@linuxidc git]# git diff #對比工作區對比暫存區
9、log 就是紀錄檔咯,不過這裡顯示的可能比較詭異
[root@linuxidc git]# git log #詳細顯示
commit bf266f5673089439efdd632a38b7220390af5cc7
Author: sunshine <sunshine@git.com>
Date: Wed Oct 5 23:34:03 2016 +0800
code1.py New add row Sunshine Good
commit 7894d320ac92997fdb4d0c74487d87def3ebf756
Author: root <root@linuxidc.(none)>
Date: Wed Oct 5 23:20:29 2016 +0800
New add code1.py
commit b4213472064fbc292eff843b6a67549344197495
Author: root <root@linuxidc.(none)>
Date: Wed Oct 5 21:45:23 2016 +0800
New add code.py
[root@linuxidc git]# git log --oneline #簡要顯示
bf266f5 code1.py New add row Sunshine Good
7894d32 New add code1.py
b421347 New add code.py
[root@linuxidc git]# git log --color --graph #顯示版本分支示意圖
* commit bf266f5673089439efdd632a38b7220390af5cc7
| Author: sunshine <sunshine@git.com>
| Date: Wed Oct 5 23:34:03 2016 +0800
|
| code1.py New add row Sunshine Good
|
* commit 7894d320ac92997fdb4d0c74487d87def3ebf756
| Author: root <root@linuxidc.(none)>
| Date: Wed Oct 5 23:20:29 2016 +0800
|
| New add code1.py
|
* commit b4213472064fbc292eff843b6a67549344197495
Author: root <root@linuxidc.(none)>
Date: Wed Oct 5 21:45:23 2016 +0800
New add code.py
10、merge 合併分支
[root@linuxidc git]# ls
code1.py code.py
[root@linuxidc git]# git checkout -b dev #建立並切換至分支dev
Switched to a new branch 'dev'
[root@linuxidc git]# git branch #檢視當前位置所在分支
* dev
master
[root@linuxidc git]# vim code.py
[root@linuxidc git]# cat code.py
print 'Sunsine !'
[root@linuxidc git]# git checkout master #切換回master
M code.py
Switched to branch 'master'
[root@linuxidc git]# git merge dev #合併分支dev
Already up-to-date.
[root@linuxidc git]# cat code.py
print 'Sunsine !'
[root@linuxidc git]# git branch -d dev #刪除分支
Deleted branch dev (was bf266f5)
11、mv 重新命名
[root@linuxidc git]# ls
code1.py code.py
[root@linuxidc git]# git mv code1.py code.txt #重新命名code1.py-->code.txt
[root@linuxidc git]# ls
code.py code.txt
[root@linuxidc git]# git status #檢視狀態,顯示 # renamed: code1.py -> code.txt
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: code1.py -> code.txt
#
[root@linuxidc git]# git add code.txt
[root@linuxidc git]# git commit -m " Modify file name code1.py-->code.txt"
[master 924041d] Modify file name code1.py-->code.txt
1 files changed, 0 insertions(+), 0 deletions(-)
rename code1.py => code.txt (100%)
12、rm 刪除
[root@linuxidc git]# ll
total 8
-rw-r--r-- 1 root root 23 Oct 5 23:53 code.py
-rw-r--r-- 1 root root 23 Oct 5 23:32 code.txt
[root@linuxidc git]# git rm code.txt #刪除code.txt
rm 'code.txt'
[root@linuxidc git]# ll
total 4
-rw-r--r-- 1 root root 23 Oct 5 23:53 code.py
[root@linuxidc git]# git commit -am " Delete file code.txt"
[master 82ec99e] Delete file code.txt
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 code.txt
13、show等價於rev-parse,只不過一個是簡要一個是明細
[root@linuxidc git]# git show HEAD~ #看hash值
commit 924041d5e2924e945f67816775869d4ceb9694dd
Author: sunshine <sunshine@git.com>
Date: Wed Oct 5 23:57:25 2016 +0800
Modify file name code1.py-->code.txt
diff --git a/code.txt b/code.txt
new file mode 100644
index 0000000..66708be
--- /dev/null
+++ b/code.txt
@@ -0,0 +1 @@
+print 'Sunshine Good!'
diff --git a/code1.py b/code1.py
deleted file mode 100644
index 66708be..0000000
--- a/code1.py
+++ /dev/null
@@ -1 +0,0 @@
-print 'Sunshine Good!'
[root@linuxidc git]# git rev-parse HEAD~ #仔細看hash值是一樣的
924041d5e2924e945f67816775869d4ceb9694dd
14、cat-file 檢視tree與parent
[root@linuxidc git]# git cat-file -t HEAD~ #-t 顯示commit
commit
[root@linuxidc git]# git cat-file -p HEAD~ #-p 顯示tree與parent
tree ea16f4c3781c4dc1f5e142eba114b0d5a1cefeaa
parent 865c84e5f13fb239f83e3e27bcf0f3a28990034e
author sunshine <sunshine@git.com> 1475683045 +0800
committer sunshine <sunshine@git.com> 1475683045 +0800
Modify file name code1.py-->code.txt
15、reset 這個就比較複雜啦
[root@linuxidc git]# ll
total 4
-rw-r--r-- 1 root root 23 Oct 6 00:17 code.py
[root@linuxidc git]# git log --oneline #檢視歷史commit版本hash值82ec99e
82ec99e Delete file code.txt
924041d Modify file name code1.py-->code.txt
865c84e New add rom 'Sunshine Good~'
bf266f5 code1.py New add row Sunshine Good
7894d32 New add code1.py
b421347 New add code.py
[root@linuxidc git]# ll
total 4
-rw-r--r-- 1 root root 23 Oct 6 00:17 code.py
[root@linuxidc git]# git rm code.py #刪除檔案code.py
rm 'code.py'
[root@linuxidc git]# git commit -am "Delete file code.py" #commit -am等價於執行 #git add filename # git commit -m "Delete fie code.py",提示刪除成功
[master 3271a1d] Delete file code.py
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 code.py
[root@linuxidc git]# ll #檢視不存在
total 0
[root@linuxidc git]# git status #也沒報錯
# On branch master
nothing to commit (working directory clean)
[root@linuxidc git]# git log --oneline #檢視commit版本hash值
3271a1d Delete file code.py
82ec99e Delete file code.txt
924041d Modify file name code1.py-->code.txt
865c84e New add rom 'Sunshine Good~'
bf266f5 code1.py New add row Sunshine Good
7894d32 New add code1.py
b421347 New add code.py
[root@linuxidc git]# git reset --hard 82ec99e #回滾到上個版本
HEAD is now at 82ec99e Delete file code.txt
[root@linuxidc git]# ll #OK檔案回來了
total 4
-rw-r--r-- 1 root root 23 Oct 6 00:18 code.py
[root@linuxidc git]# git log --oneline #回滾成功
82ec99e Delete file code.txt
924041d Modify file name code1.py-->code.txt
865c84e New add rom 'Sunshine Good~'
bf266f5 code1.py New add row Sunshine Good
7894d32 New add code1.py
b421347 New add code.py
16、tag 標籤,由於commit都是以hash記錄,所以tag是為快速定義而準備的
[root@linuxidc git]# git log --oneline #檢視commit版本的hash
2989e89 New add rom Sunshine
bf266f5 code1.py New add row Sunshine Good
7894d32 New add code1.py
b421347 New add code.py
[root@linuxidc git]# git tag v0.1.2 2989e89 #為hash值2989e89打上標籤v0.1.2
[root@linuxidc git]# git tag v0.1.3 bf266f5 #為hash值bf266f5打上標籤v0.1.3
[root@linuxidc git]# git tag
v0.1.1
v0.1.2
v0.1.3
[root@linuxidc git]# git tag v0.1.4 #修改程式碼或者增加功能都可以直接給上tag
[root@linuxidc git]# git show 2989e89 #使用hash查詢
commit 2989e8927da83be9087933c17b074adeb2e91e2c
Author: sunshine <sunshine@git.com>
Date: Wed Oct 5 23:49:07 2016 +0800
New add rom Sunshine
diff --git a/code.py b/code.py
index e69de29..3a2a051 100644
--- a/code.py
+++ b/code.py
@@ -0,0 +1 @@
+print 'Sunsine !'
[root@linuxidc git]# git show v0.1.2 #使用標籤查詢
commit 2989e8927da83be9087933c17b074adeb2e91e2c
Author: sunshine <sunshine@git.com>
Date: Wed Oct 5 23:49:07 2016 +0800
New add rom Sunshine
diff --git a/code.py b/code.py
index e69de29..3a2a051 100644
--- a/code.py
+++ b/code.py
@@ -0,0 +1 @@
+print 'Sunsine !'
[root@linuxidc git]# git tag -d v0.1.1 #刪除標籤
Deleted tag 'v0.1.1' (was 2989e89)
[root@linuxidc git]# git tag
v0.1.2
v0.1.3
17、pull 獲取,這個用到我們之前搭建的gitolite
[root@redis_master dev]# git pull origin master #沒發現有人push,所以拉不到資訊
From 127.0.0.1:dev
* branch master -> FETCH_HEAD
Already up-to-date.
[root@redis_master dev]# git push -f #為了測試這邊使用-f強制覆蓋更新
Counting objects: 32, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (32/32), 2.94 KiB, done.
Total 32 (delta 0), reused 0 (delta 0)
To git@127.0.0.1:dev
+ 4197bf0...addfd8f master -> master (forced update)
[root@redis_master dev]# git pull #再試一下git pull
remote: Counting objects: 24, done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 23 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (23/23), done.
From 127.0.0.1:dev
+ 4197bf0...addfd8f master -> origin/master (forced update)
Merge made by recursive.
855.py | 4 ++++
class.php | 1 +
2 files changed, 5 insertions(+), 0 deletions(-)
create mode 100644 855.py
create mode 100644 class.php
[root@redis_master dev]# ll #拉取到程式碼了
total 8
-rw-r--r--. 1 root root 45 Oct 6 01:03 855.py
-rw-r--r--. 1 root root 4 Oct 6 01:03 class.php
18、push 推播,這個用到我們之前搭建的gitolite
12345678910111213141516 [root@redis_master dev]# git status #檢視狀態
# On branch master
nothing to commit (working directory clean)
[root@redis_master dev]# cat test.txt
this is test file
555
[root@redis_master dev]# echo 44 >> test.txt #修改內容
[root@redis_master dev]# git commit -am "New add string 44" #commit並說明
[master 4197bf0] New add string 44
1 files changed, 1 insertions(+), 0 deletions(-)
[root@redis_master dev]# git push #推播至我們的遠端程式碼託管
Counting objects: 5, done.
Writing objects: 100% (3/3), 262 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@127.0.0.1:dev
8a54bbe..4197bf0 master -> master
CentOS 6.7下Gitolite 服務搭建及TortoiseGit設定連線 見 http://www.linuxidc.com/Linux/2017-07/145766.htm。其實這裡只是簡簡單單的介紹了下命令的正常場景所遇見的問題!~更深層次的下次介紹唄^-^,
更多 Git 教學系列文章:
GitHub 使用教學圖文詳解 http://www.linuxidc.com/Linux/2014-09/106230.htm
Git使用圖文詳細教學 http://www.linuxidc.com/Linux/2016-11/136781.htm
Ubuntu Git安裝與使用 http://www.linuxidc.com/Linux/2016-11/136769.htm
Git 標籤管理詳解 http://www.linuxidc.com/Linux/2014-09/106231.htm
Git 分支管理詳解 http://www.linuxidc.com/Linux/2014-09/106232.htm
Git 遠端倉庫詳解 http://www.linuxidc.com/Linux/2014-09/106233.htm
Git 本地倉庫(Repository)詳解 http://www.linuxidc.com/Linux/2014-09/106234.htm
Git 伺服器搭建與用戶端安裝 http://www.linuxidc.com/Linux/2014-05/101830.htm
Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm
分享實用的GitHub 使用教學 http://www.linuxidc.com/Linux/2014-04/100556.htm
Git從入門到學會 http://www.linuxidc.com/Linux/2016-10/135872.htm
Git基本操作詳解 http://www.linuxidc.com/Linux/2016-10/135691.htm
Git部署與常用基本命令詳解 http://www.linuxidc.com/Linux/2017-06/144961.htm
分散式版本控制系統 Git 詳細教學 http://www.linuxidc.com/Linux/2017-05/143747.htm
相關文章