git 命令總結

2021-10-09 04:04:00 字數 2988 閱讀 9033

常用的git命令: 

# clone 分支

git clone url

# status 檢視狀態

git status

# add 新增

git add .

git add -u #將修改的檔案加入到快取區間,不包括新增和刪除的。

git add -a #將新增和刪除檔案也新增到快取區

git add -p #對乙個檔案內的修改可以選擇性的新增到快取區間。

# 刪除檔案

git rm [file1] [file2] ...

# 停止追蹤指定檔案,但該檔案會保留在工作區

git rm --cached [file]

# 改名檔案,並且將這個改名放入暫存區

$ git mv [file-original] [file-renamed]

# 列出所有本地分支

git branch

# 列出所有遠端分支

git branch -r

# 列出所有本地分支和遠端分支

git branch -a

# 建立分支

git branch branchname

# 新建乙個分支,並切換到該分支

git checkout -b branchname

# 新建乙個分支,與指定的遠端分支建立追蹤關係

git branch --track [branch] [remote-branch]

eg:git checkout -b content-hub-1.2.1 建立本地分支並切換到content-hub-1.2.1

推送到遠端分支:git push origin content-hub-1.2.1

# 建立追蹤關係,在現有分支與指定的遠端分支之間

git branch --set-upstream [branch] [remote-branch]

eg:git branch --set-upstream-to=origin/content-hub-1.2.1

當前分支為content-hub-1.2.1,遠端分支為origin/content-hub-1.2.1

# 合併指定分支到當前分支

git merge [branch]

# 刪除本地分支

git branch -d [branch-name]

# 刪除遠端分支

git push origin --delete [branch-name]

git branch -dr [remote/branch]

git fetch [remote]

# 顯示所有遠端倉庫

git remote -v

# 取回遠端倉庫的變化,並與本地分支合併

git pull [remote] [branch]

# 上傳本地指定分支到遠端倉庫

git push [remote] [branch]

eg:git push origin dev //把當前本地分支推送到到遠端 並在遠端建立dev分支

# 強行推送當前分支到遠端倉庫,即使有衝突

git push [remote] --force

# 檢視標籤

git tag

# 建立本地tag

git tag tagname

#推送到遠端倉庫

git push origin #多個tag推送

git push origin --tags

#檢視遠端所有 tag:

git ls-remote --tags origin

# 本地 tag 的刪除:

git tag -d # 遠端 tag 的刪除:

git push origin :# 重新命名標籤

這個本質上是刪除掉舊名字 tag ,然後再新建新名字 tag ,然後實現重新命名的作用。

如果 tag 只存在本地,那麼只需要刪除本地的舊名字 tag ,然後新建新名字 tag:

git tag -d git tag git push origin //推送到遠端倉庫

若已經推送到遠端了,那麼不僅要刪除本地的,還要刪除遠端的,再重新建立和推送:

git tag -d git push origin :git tag git push origin //推送到遠端倉庫

# 檢出標籤

git checkout -b

#對比檔案內容

git diff

#對比快取中的內容

git diff --cached

2.當你對你當前提交的注釋不滿意時:git commit --amend 

git commit --amend -m "你的新的注釋"

git commit --amend -m "測試將已經提交的檔案從提交記錄中刪除"

[content-hub-1.1.0 a3913cb] 測試將已經提交的檔案從提交記錄中刪除

date: tue jan 5 15:01:45 2021 +0800

1 file changed, 1 insertion(+)

create mode 100644 工作日誌/elasticsearch/compare/txt/3

3.當你提交了乙個你本不想提交的檔案時 :git rm --cached 

git rm --cached 檔名稱路徑

$ git rm --cached 工作日誌/elasticsearch/compare/txt/3

rm '工作日誌/elasticsearch/compare/txt/3'

4.當你想切換分支,但是又不想提交在本分支的改動時:git stash

git stash

git checkout new-branch

#當你想回到之前分支,並且還原當前分支的狀態

git checkout branch

git stash pop

git命令總結

git config global user.name fangting git config global user.email 126.com git stash list 顯示git棧內的所有備份,可以利用這個列表來決定哪個地方恢復。git stash clear清空git棧。git stat...

Git命令總結

git 操作步驟 1.新建倉庫mkdir mygit 建立倉庫目錄 cd mygit 進入mygit進行操作 pwd 顯示mygit的路徑,此步驟可以不要 2.初始化git init 將目錄轉為git可以管理的倉庫 3.新增檔案新建乙個檔案 如txt檔案 一定要放到剛才新建的目錄下 子目錄也行 這裡...

git命令總結

1.使用repo命令批量提交多個庫的修改 repo forall project1 project2 c git add a repo forall project1 project2 c git commit m commit comment here repo forall project1 p...