1. 程式人生 > >40條最常用Git命令總結

40條最常用Git命令總結

Git的3層結構

  • working directory:工作區
  • staging index:暫存區
  • git directory(Repository):版本庫

Git中檔案的4種狀態

  • untracked:未被追蹤
  • Modified:表示工作區修改了某個檔案但是還沒有新增到暫存區
  • Staged:表示把工作區修改的檔案新增到了暫存區但是沒有提交到版本庫
  • Committed:表示資料被安全地儲存在本地庫中

Git基本命令

1.Git檔案操作

Git基本操作
  1. 初始化:git init
Initialized empty Git repository in E
:/git_test2018/.git/
  1. 查詢狀態: 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)
  1. 新增暫存區: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
  1. 提交:git commit -m 'first commit'
[master (root-commit) b32b353] first commit
 1 file changed, 139 insertions(+)
 create mode 100644 StringList.cpp
  1. 配置郵箱和姓名資訊:
    git config --global user.email "[email protected]"

git config --global user.name "Your Name"

  1. 檢視配置資訊: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
  1. 檢視提交資訊:git log [--oneline]
$ git log
commit b32b3537154da3e878f1ff07a4c38303ff46a4af
Author: Tom <[email protected]>
Date:   Tue Feb 13 22:36:57 2018 +0800

    first commit
  1. 提交所有檔案:git add .
  2. 直接將工作區檔案提交到版本庫中(跳過git add命令):git commit -am 'remodified'
Git撤銷操作
  1. git commit --amend: 撤銷上一次提交,並將暫存區的檔案重新提交
  2. git checkout -- filename / git checkout -- .: 拉取暫存區的檔案並將其替換工作區的檔案
  3. git reset HEAD -- filename : 拉取最近一次提交的版本庫中的這個檔案到暫存區,該操作不影響工作區
Git檔案刪除
  1. 刪除工作區及暫存區中的該檔案相當於刪除檔案後執行git add
  2. git rm --cache filename :在不小心將不需要追蹤的檔案新增到暫存區,想刪除暫存的檔案但是不想刪除工作區的檔案很有用
  3. git rm -f filename : 當工作區或者暫存區檔案修改了,防止把修改誤刪除了
  4. git mv oldname newname : 相當於 mv oldname newname; git rm oldname; git add newname

Git分支

Git分支的建立、修改、切換、刪除
  1. 檢視分支 : git branch
  2. 新建分支 : git branch branchname
  3. 切換分支 : git checkout branchname
  4. 刪除分支(先切換到另一分支) : git branch -d branchname
  5. 重名名分支 : git branch -m oldbranch newbranch
  6. 新建分支並切換到該分支 : git checkout -b branchname
Git分支的合併
  1. git diff : 比較工作區與暫存區檔案的差異
  2. git diff --staged : 比較暫存區與版本庫的檔案差異
  3. git diff 版本號 版本號 : 比較分支內的兩個版本的差異
  4. git diff 分支 分支 : 比較兩個分支的最新提交版本的差異
  5. git merge branchnam : 合併之前需要切換到master分支;快速合併與衝突合併
儲存變更
  1. git stash
  2. git stash list
  3. git stash apply stash@{0}
  4. git stash pop stash@{1}
  5. git stash drop stash@{0}

Git遠端倉庫

github上的倉庫
  1. git push https://github.com/*/*.git master : 本地推送到Github遠端倉庫
  2. ssh-keygen : 生成ssh私鑰
  3. git pull [email protected]:*/*.git master : 從遠端倉庫拉取到本地(需在遠端Github倉庫填寫本地公鑰資訊)
  4. 修改遠端倉庫名稱 : git remote add newname [email protected]:*/*.git
  5. 檢視遠端倉庫資訊 : git remote -v
  6. 新增遠端倉庫資訊 : git remote add origin ssh://root@*.*.*.*/*.git
  7. 拉取到本地 : git pull origin master
  8. git reset --hard HEAD :撤銷本地修改
Git ssh免密登入
  1. ssh-keygen
  2. ssh-copy-id user@host : 將本機的公鑰複製到遠端伺服器的authorized_keys檔案中
  3. 如果不是自己的伺服器,可以將本地公鑰發給伺服器管理員,新增在authorized_keys檔案後面
  4. git fetch origin master : 獲取遠端倉庫最新程式碼

Git幫助文件

  1. touch .gitignore : 無需版本控制的檔案納入其中
  2. git help 命令