安裝完Git後進行設置用戶名和郵箱:
git config --global user.name "xxx"
git config --global user.email "xxx"
Example:
git config --global user.name "test"
git config --global user.email "[email protected]"
初始化倉庫,將當前目錄變成Git倉庫,生成一個 .git 文件來管理此倉庫:
git init
添加文件到暫存區(添加未提交):
git add <file>
Example:
git add test.txt
添加所有修改文件到暫存區 :
git add .
刪除文件 :
git rm -- <file>
Example:
git rm -- test.txt
提交帶有說明的文件到版本庫的分支:
git commit -m "xxx"
Example:
git commit -m "這是一次測試提交"
查看倉庫文件當前的狀態:
git status
查看文件修改了什麼內容:
git diff
查看本地工作區和倉庫最新版本區別(–後有一個空格):
git diff HEAD -- test.txt
查看從最近到最遠所提交的日誌(版本回退後,不包括回退後版本日誌):
git log
查看從最近到最遠所提交的日誌(版本回退後,不包括回退後版本日誌),每條提交日誌一行顯示 :
git log --pretty=oneline
查看所有提交的日誌 :
git reflog
版本回退到上個版本(–hard HEAD^^上2個版本/–hard HEAD~100 上一百個版本) :
git reset --hard HEAD^
回退到指定版本 :
git reset --hard <版本提交ID>
Example:
git reset --hard 6d5123
重新將暫存區文件放回工作區(未添加狀態) :
git reset HEAD <file>
Example:
git reset HEAD test.txt
查看文件內容 :
cat <file>
Example:
cat test.txt
撤銷工作區(未添加到暫存區)文件的修改(- - 必須要,不然就是切換分支) :
git checkout -- <file>
Example:
git checkout -- test.txt
切換到名爲xxx的分支
git checkout <xxx>
Example:
git checkout master
創建名爲xxx的分支 :
git branch <xxx>
Example:
git branch test
創建名爲xxx的分支,並切換到該分支 :
git checkout -b <xxx>
Example:
git checkout -b test
查看所有分支,當前分支帶 * 號 :
git branch
查看當前所有分支(包含遠程分支)以及各自分支最新提交信息 :
git branch -av
刪除名爲xxx的分支 :
git branch -d <xxx>
Example:
git branch -d test
創建SSH KEY (ssh-keygen不能有空格) :
ssh-keygen -t rsa -C "郵箱"
Example:
ssh-keygen -t rsa -C "[email protected]"
添加本地倉庫內容到遠程倉庫 :
git remote add <ssh地址>
Example:
git remote add git@github.YourName/xxxxxxx.git
推送所有內容到遠程倉庫(第一次需要加 -u) :
git push -u origin master
將名爲xxx的本地分支推送到遠程倉庫爲遠程分支 :
git push origin <xxx>
Example:
git push origin test
拉取遠程庫內容 :
git pull
查看遠程庫信息 :
git remote
查看遠程庫詳細信息 :
git remote -v
合併名爲 xxx 分支到當前分支——不能看出合併歷史 :
git merge <xxx>
Example:
git merge test
禁用Fast forward,合併名爲 xxx 分支到當前分支——能看出合併歷史 :
git merge --no-ff -m "提交描述" <xxx>
Example:
git merge --no-ff -m "這是一個禁用FF的合併" <test>
儲藏當前工作狀態(工作區變成clean狀態) :
git stash
查看儲藏的工作狀態 :
git stash list
恢復儲藏工作狀態,但是該狀態的內容依然存在 :
git stash apply
刪除使用git stash apply恢復儲藏狀態的內容
git stash drop
恢復儲藏工作狀態,並刪除內容 :
git stash pop
多次stash,恢復指定stash :
git stash apply stash@{0}
給當前分支添加xxx標籤,方便查找 :
git tag <xxx>
Example:
git tag v1.0
給commit id的提交打上xxx標籤 :
git tag <xxx> <commit id>
Example:
git tag <v1.0> <6da123>
查看標籤 :
git tag
根據標籤名查看標籤詳細信息 :
git show <xxx>
Example:
git show <v1.0>
刪除名爲 xxx 的標籤 :
git tag -d <xxx>
Example:
git tag -d v1.0
推送名爲 xxx 的標籤至遠程庫
git push origin <xxx>
Example:
git push origin v1.0
推送所有標籤至遠程庫 :
git push --tags
克隆遠程庫代碼 :
git clone <xxx>
Example :
git clone [email protected]/xxxxxxx.git
刪除遠程庫標籤分2步:
1.本地刪除名爲xxx的標籤
git tag -d <xxx>
Example:
git tag -d v1.0
2.刪除遠程庫xxx標籤
git push origin :refs/tags/<xxx>
Example:
git push origin :refs/tags/v1.0