git詳細命令

git詳細命令

1. 配置git

ssh-keygen -t rsa -C "email"    創建SSH Key
cd ~/.ssh
cat id_rsa.pub   獲取密鑰

git config --list   
git config --global user.name "用戶名"     全局配置
git config --global user.email "email"
git config --local user.name "用戶名"      本地配置
git config --local user.email "email"

2. 遠程操作

git init         初始化倉庫
git clone [url]  克隆數據庫
git remote add origin [url]    關聯遠程倉庫
git remote update origin --prune  同步遠程分支列表

3. 對文件操作

git add <file>  把文件修改添加到暫存區
git rm <file>  刪除文件,包括暫存區和工作區

如果誤刪除了,可執行以下命令
$ git reset head <file>
$ git checkout -- <file>
git commit -m "註釋"

提交更改,把暫存區的所有內容提交到當前分支
git commit -am的區別是git commit -m用於提交暫存區的文件(只提交添加的);git commit -am用於提交跟蹤過的文件(修改過的也能提交)

4. 撤銷操作

git reset --hard HEAD^   回退上一個版本
git log --pretty=oneline
git reset --hard 版本號

先從log獲取從最近到最遠的提交日誌,再回退到指定版本號,當回退後想返回,可通過git reflog查看命令歷史,獲取需要返回的版本號

git commit --amend

如果commit了還沒push,此命令可以重新提交,將之前的提交合併爲一個

git checkout -- [file]

如果file修改後還沒有被放到暫存區則變爲和版本庫一模一樣的狀態;
如果file已經添加到暫存區後又作了修改,則變爲暫存區的狀態。

git revert 

git revert用一次新的commit來回滾之前的commit,git reset是直接刪除指定的commit

git fetch --all && git reset --hard origin/master && git pull

當回滾到某個版本,想刪除本地回滾後的提交,使用上面語句

5. 比較差異

git diff

比較的是工作目錄中當前文件和暫存區域快照之間的差異,也就是修改之後還沒有暫存起來的變化內容

git diff --cached 
git diff --staged

效果一致,都是查看已暫存的將要添加到下次提交裏的內容,也就是commit時候的內容

git diff HEAD -- <file>

可以查看工作區和版本庫裏面最新版本的區別

6. 標籤

git tag      查看所有標籤
git tag <tagname> commitid commitid不填默認HEAD
git tag -a <tagname> -m "註釋" commitid
git tag -d <tagname>   刪除本地標籤
git push origin :refs/tags/<tagname>  刪除遠程標籤
git reset --hard <tagname>  回滾到此標籤

7. 分支

git branch -vva        查看詳細分支,包括和遠程分支的關聯
git branch <name>      創建分支

git checkout <name>     切換分支
git checkout -b <name>   創建+切換分支

git branch -d <name>      刪除本地分支,如果是-D表示強制刪除
git push origin --delete 分支名稱     刪除遠程分支

git checkout -b branch-name origin/branch-name 在本地創建分支並和遠程關聯
git branch --set-upstream 本地分支 origin/遠程分支  將本地分支關聯遠程分支

git merge <branchname>      合併某分支到當前分支
git merge --no-ff -m "description" <branchname>  本次合併要創建新的commit
git rebase <branchname>      衍和某分支到當前分支

git cherry-pick commitid  可以只合並別的分支的某個提交記錄而不是和整個分支合併

8. 日誌

git log
git reflog   查看命令歷史

常用參數:

選項 描述
-p 用來顯示每次提交的內容差異
-數字幾 僅顯示最近幾次提交
--stat 顯示每次提交中修改的文件的統計信息
--shortstat 僅顯示--stat命令中已更改/插入/刪除行
--name-only 顯示提交信息後修改的文件列表
--name-status 顯示受添加/修改/刪除信息影響的文件列表
--abbrev-commit 僅顯示SHA-1校驗和的前幾個字符,而不是全部40個字符
--relative-date 以相對格式顯示日期(例如,“2周前”),而不是使用完整日期格式
--graph 在日誌輸出旁邊顯示分支和合並歷史記錄的ASCII圖
--pretty 以備用格式顯示提交。選項包括oneline,short,full,fuller和format(您指定自己的格式)
--oneline --pretty=oneline --abbrev-commit一起使用的簡寫

9. 提交/拉取代碼

git pull --rebase 使用rebase是爲了去掉Merge branch的提交信息
git push origin 遠程分支名x:本地分支名
git push -u origin master  一般是第一次遠端倉庫爲空

10. 存儲

git stash      存儲代碼
git stash apply      恢復存儲的代碼
git stash pop      恢復的同時把stash內容也刪了
git stash list    查詢存儲列表
git stash apply stash@{序號}     恢復指定的存儲代碼
git stash drop stash@{0}   刪除指定的存儲代碼

11. Git 別名

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章