常用git命令

2021-09-24 18:05:09 字數 3011 閱讀 7069

整理了一些常用的git命令

建立版本庫:

$ git clone

#轉殖遠端版本庫到指定目錄

$ git clone [dir]

##轉殖指定遠端分支的版本庫

$ git clone -b [branch-name]

#在當前目錄新建乙個git**庫

$ git init

#新建乙個目錄,將其初始化為git**庫

$ git init [project-name]

增加/刪除檔案:

#新增指定檔案到暫存區

$ git add [file1] [file2] ...

##新增指定目錄到暫存區,包括子目錄

$ git add [dir]

#新增當前目錄得所有檔案到暫存區

$ git add .

#新增每個變化前都會要求確認,對於同一檔案的多處變化可以實現分次提交

$ git add -p

#刪除工作區檔案,並且將這次刪除放入暫存區

$ git rm [file1] [file2]

**提交:

#提交暫存區到倉庫區

$ git commit -m [message]

#提交暫存區的指定檔案到倉庫區

git commit [file1] [file2] ... -m [message]

#使用一次新的commit,替代上一次提交,如果**沒有任何新的變化,則來改寫上次commit的提交資訊

$ git commit --amend -m [message]

#重做上一次commit,幷包括指定檔案的新變化

$ git commit --amend [file1] [file2] ...

儲藏:#儲藏變更

$ git stash

#檢視現有的儲藏

$ git stash list

#使用最近的儲藏

#使用某次的儲藏

#重新應用被暫存的變更

#從棧中移除某個儲藏

$ git stash drop [stashname]

#重新應用儲藏,同事立刻將其從堆疊中移走

$ git stash pop

分支:#列出所有本地分支

$ git branch

#列出所有遠端分支

$ git branch -r

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

$ git branch -a

#新建乙個分支,但依然提留在當前分支

$ git branch [branch-name]

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

$ git checkout -b [branch-name]

#新建乙個分支,指向指定commit

$ git branch [branch] [commit]

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

git branch --trach [branch] [remote-branch]

#切換到指定分支,並更新工作區

$ git chenckout [branch-name]

#切換到上乙個分支

$ git checkout -

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

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

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

$ git merge [branch]

#選擇乙個commit,合併進當前分支

$ git cherry-pick [commit]

#刪除分支

$ git branch -d [branch-name]

#刪除遠端分支

$ git push origin --delete [branch-name]

$ git branch -dr [remote/branch]

檢視資訊:

#顯示所有變更的檔案

$ git status

#顯示當前分支的版本歷史

$ git log

#顯示commit歷史,以及每次commit發生變更的檔案

$ git log --stat

$ git log -s [keyword]

#顯示暫存區和工作區的差異

$ git diff

#顯示當前分支的最近幾次提交

$ git reflog

遠端同步:

$ git fetch [remote]

#顯示所有遠端倉庫

$ git remote -v

#顯示某個遠端倉庫的資訊

$ git remote show [remote]

#增加乙個新的遠端倉庫,並命名

$ git remote add [shortname] [url]

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

$ git pull [remote] [branch]

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

$ git push [remote] --force

#推送所有分支到遠端倉庫

$git push [remote] --all

撤銷:#恢復暫存區的指定檔案到工作區

$ git checkout [file]

#恢復某個commit的指定檔案到暫存區和工作區

$ git checkout [commit] [file]

#恢復暫存區的所有檔案到工作區

$ git checkout .

#重置暫存區的指定檔案,與上一次commit保持一致,但工作區不變

$ git reset [file]

#重置暫存區與工作區,與上一次commit保持一次

$ gitreset --hard

#新建乙個commit,用來撤銷指定commit,後者的所有變化都將被前者抵消,並且應用到當前分支

$ git revert [commit]

其他:#生成乙個可供發布的壓縮包

$ git archive

常用Git命令

下面是我在開發中常用的git命令 1.配置git git config global add user.email git config global add user.name 2.建立新的專案 git init git add git commit a m git remote add orig...

常用Git命令

附上一些git的常見命令 gitremote add origin git github.com myaccount myproject.git git push u origin master gitinit 建立repository git 命令 help 檢視幫助命令 touchfilenam...

常用Git命令

建立管理倉庫 git initadd和commit git add readme.txt git commit m wrote a readme file 簡略版log git log pretty oneline abbrev commit當前狀態 git status撤銷更改 git check...