40條最常用Git命令總結
阿新 • • 發佈:2019-02-19
Git的3層結構
- working directory:工作區
- staging index:暫存區
- git directory(Repository):版本庫
Git中檔案的4種狀態
- untracked:未被追蹤
- Modified:表示工作區修改了某個檔案但是還沒有新增到暫存區
- Staged:表示把工作區修改的檔案新增到了暫存區但是沒有提交到版本庫
- Committed:表示資料被安全地儲存在本地庫中
Git基本命令
1.Git檔案操作
Git基本操作
- 初始化:
git init
Initialized empty Git repository in E :/git_test2018/.git/
- 查詢狀態:
git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
StringList.cpp
StringList.html
nothing added to commit but untracked files present (use "git add" to track)
- 新增暫存區:
git add StringList.cpp
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: StringList.cpp
Untracked files:
(use "git add <file>..." to include in what will be committed)
StringList.html
- 提交:
git commit -m 'first commit'
[master (root-commit) b32b353] first commit
1 file changed, 139 insertions(+)
create mode 100644 StringList.cpp
- 配置郵箱和姓名資訊:
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
- 檢視配置資訊:
git config --list
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
http.sslcainfo=F:/Git/Git/mingw64/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
rebase.autosquash=true
credential.helper=manager
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
user.name=Tom
user.email=Tom@qq.com
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
- 檢視提交資訊:
git log [--oneline]
$ git log
commit b32b3537154da3e878f1ff07a4c38303ff46a4af
Author: Tom <[email protected]>
Date: Tue Feb 13 22:36:57 2018 +0800
first commit
- 提交所有檔案:
git add .
- 直接將工作區檔案提交到版本庫中(跳過git add命令):
git commit -am 'remodified'
Git撤銷操作
git commit --amend
: 撤銷上一次提交,並將暫存區的檔案重新提交git checkout -- filename / git checkout -- .
: 拉取暫存區的檔案並將其替換工作區的檔案git reset HEAD -- filename
: 拉取最近一次提交的版本庫中的這個檔案到暫存區,該操作不影響工作區
Git檔案刪除
- 刪除工作區及暫存區中的該檔案相當於刪除檔案後執行git add
git rm --cache filename
:在不小心將不需要追蹤的檔案新增到暫存區,想刪除暫存的檔案但是不想刪除工作區的檔案很有用git rm -f filename
: 當工作區或者暫存區檔案修改了,防止把修改誤刪除了git mv oldname newname
: 相當於 mv oldname newname; git rm oldname; git add newname
Git分支
Git分支的建立、修改、切換、刪除
- 檢視分支 :
git branch
- 新建分支 :
git branch branchname
- 切換分支 :
git checkout branchname
- 刪除分支(先切換到另一分支) :
git branch -d branchname
- 重名名分支 :
git branch -m oldbranch newbranch
- 新建分支並切換到該分支 :
git checkout -b branchname
Git分支的合併
git diff
: 比較工作區與暫存區檔案的差異git diff --staged
: 比較暫存區與版本庫的檔案差異git diff 版本號 版本號
: 比較分支內的兩個版本的差異git diff 分支 分支
: 比較兩個分支的最新提交版本的差異git merge branchnam
: 合併之前需要切換到master分支;快速合併與衝突合併
儲存變更
git stash
git stash list
git stash apply stash@{0}
git stash pop stash@{1}
git stash drop stash@{0}
Git遠端倉庫
github上的倉庫
git push https://github.com/*/*.git master
: 本地推送到Github遠端倉庫ssh-keygen
: 生成ssh私鑰git pull [email protected]:*/*.git master
: 從遠端倉庫拉取到本地(需在遠端Github倉庫填寫本地公鑰資訊)- 修改遠端倉庫名稱 :
git remote add newname [email protected]:*/*.git
- 檢視遠端倉庫資訊 :
git remote -v
- 新增遠端倉庫資訊 :
git remote add origin ssh://root@*.*.*.*/*.git
- 拉取到本地 :
git pull origin master
git reset --hard HEAD
:撤銷本地修改
Git ssh免密登入
ssh-keygen
ssh-copy-id user@host
: 將本機的公鑰複製到遠端伺服器的authorized_keys檔案中- 如果不是自己的伺服器,可以將本地公鑰發給伺服器管理員,新增在authorized_keys檔案後面
git fetch origin master
: 獲取遠端倉庫最新程式碼
Git幫助文件
touch .gitignore
: 無需版本控制的檔案納入其中- git help 命令