??最近心血來潮,突然想把自己這些年所學到的東西梳理一下,所以先從Git開始。本文為Git常用命令篇,可為初學者快速查找使用。若對其中一些命令感到迷惑,推薦以下文章進行解惑:
《Git教程》基本涵蓋所有git命令,并進行了分類和詳細說明。
《廖雪峰Git教程》初學者的入門教程,含視頻教學
《Git合并指定文件到另一個分支》合并指定提交請看這里
《Git--分支管理策略(Git flow)》分支管理實戰篇,給自己打廣告的?
不向前走,不知路遠;不努力學習,不明白真理。有些東西寫出來,或許成全了別人,也方便了自己。話不多說,直接開干!
## 全局配置 ##
配置名字和郵箱
git config --global user.name "Some One"
git config --global user.email "someone@gmail.com"
配置命令別名alias
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
命令變為
git co
git br
git ci
git st
git unstage <file>
## 倉庫 ##
初始化倉庫
git init
克隆倉庫(將遠程倉庫克隆到本地)
git clone <倉庫地址>
關聯遠程倉庫(在本地倉庫與遠程倉庫間建立關聯)
git remote add origin <倉庫地址>
## 分支 ##
查看本地分支列表
git branch
創建分支
git branch <branch name>
切換分支
git checkout <branch name>
創建并切換到該分支
git checkout -b <branch name>
分支重命名
git branch -m <old> <new>
刪除本地分支
git branch -D <branch name>
刪除遠程分支
git push origin :<branch name>
手動建立本地分支與遠程分支的追蹤關系
git branch --set-upstream <local branch name> <remote branch name>
合并某分支到當前分支
git merge <branch name>
合并分支代碼 保持兩個分支提交在一條線上
git rebase <branch name>
## 文件 ##
查看工作區和暫存區的狀態
git status
添加指定文件
git add -- <文件/文件夾全路徑>
添加修改文件
git add .
提交到本地
git commit -m "這里寫備注"
先添加修改再提交
git commit -a -m "這里寫備注"
git commit -am "這里寫備注"
拉取遠程分支但不合并
git fetch
拉取遠程分支并合并
git pull
#建立追蹤關系后可簡寫
拉取遠程分支并合并
git pull origin <遠程分支名>
推送代碼到遠程
git push
#建立追蹤關系后可簡寫
git push origin <遠程分支名>
撤銷文件修改
git checkout --<file>
刪除沒有被track(追蹤)的文件
git clean
比較當前文件和暫存區文件差異
git diff
#比較全部
git diff <file>
#比較指定文件
比較兩次提交之間的差異
git diff <commit1><commit2>
在兩個分支之間比較
git diff <branch1> <branch2>
表暫存區和版本庫之間的差別
git diff --cached
git diff --staged
將暫存區指定文件還原至工作區
git reset HEAD --<file>
將暫存區所有文件還原至工作區
git reset HEAD --.
## 標簽 ##
查看tag列表
git tag
添加tag至本地
git tag <tag name>
推送tag至遠程倉庫
git push origin <tag name>
推送本地所有tag至遠程倉庫
git push --tags
刪除本地tag
git tag -d <tag name>
刪除遠程倉庫tag
git push origin :refs/tags/<tag name>
切換到tag
git checkout <tag name>
## 日志 ##
查看日志
git log
每條log只顯示一行
git log --oneline
圖形化地表示出分支合并歷史
git log --oneline --graph
查看命令歷史
git reflog
## 合并代碼 ##
合并某分支到當前分支
git merge <branch name>
合并某個分支上的單個commit
git cherry-pick <commit>
合并某個分支上的一系列commits
git rebase --onto <branch name> <commit_start>^
合并某個文件
git checkout --patch <branch name> <file name>
合并分支代碼 保持兩個分支提交在一條線上
git rebase <branch name>
版本回退
回退到上次提交
git reset --hard HEAD^
回退到上兩次提交
git reset – hard HEAD^^
回退到指定提交節點
git reset --hard <commit id>
代碼收藏
將未提交的內容(暫存和非暫存)藏起來(入棧)
git stash
查看收藏列表
git stash list
刪除棧頂stash
git stash drop
刪除指定stash
git stash drop <stash編號 如:stash@{0}>
將棧頂stash恢復到工作區并刪除該收藏
git stash pop
將指定stash恢復到工作區并刪除該收藏
git stash pop <stash id>
將棧頂stash恢復到工作區不刪除該收藏
git stash apply
將指定stash恢復到工作區不刪除該收藏
git stash apply <stash id>
刪除所有緩存的stash
git stash clear
查看指定stash的diff即保存的內容
git stash show
git stash show <stash id>
從stash創建分支
git stash branch <branch name>
撤銷提交
撤銷本次提交并作為一次新的提交
git revert HEAD
撤銷上次提交并作為一次新的提交
git revert HEAD^
撤銷指定提交并作為一次新的提交
git revert <commit id>