你通常用git diff來找你當前工作目錄和上次提交與本地索引間的差異。
$ git diff
上面的命令會顯示在當前的工作目錄里的,沒有 staged(添加到索引中),且在下次提交時 不會被提交的修改。
如果你要看在下次提交時要提交的內容(staged,添加到索引中),你可以運行:
$ git diff --cached
上面的命令會顯示你當前的索引和上次提交間的差異;這些內容在不帶"-a"參數運行 "git commit"命令時就會被提交。
$ git diff HEAD
上面這條命令會顯示你工作目錄與上次提交時之間的所有差別,這條命令所顯示的 內容都會在執行"git commit -a"命令時被提交。
你可以用 git diff 來比較項目中任意兩個版本的差異。
$ git diff master..test
上面這條命令只顯示兩個分支間的差異,如果你想找出‘master’,‘test’的共有 父分支和'test'分支之間的差異,你用3個‘.'來取代前面的兩個'.' 。
$ git diff master...test
當有幾個commit時,合并為一個commit然后提交到遠程的方法:
checkout 到commit所在分支,例如ticket-123123
git checkout ticket-123123gitk 圖形化界面查看目前有幾個commit要合并, 假設為3
git rebase HEAD~3 --onto origin/develop
onto后面為要提交的遠程分支
此時會顯示
First, rewinding head to replay your work on top of it...
Applying: refs# 172198 fix Japanese project announcement notification refs #172198
Applying: refs# 172198 fix Japanese project announcement notification refs #172198
Applying: refs #172198 fix Japanese project announcement notificationgitk查看狀態
git rebase -i HEAD~3
在其中,把要合并過去的(新的)分支前的選項改為s或squash, 最早的commit 前的選項保留位pick,即:
pick xxxxx messages...
s yyyyy messages...
s zzzzzz messages...
其中zzzzz是最新的commit,xxxxx是最早的commit
改完之后顯示更改信息:
[detached HEAD aec7724] refs #172198 fix Japanese project announcement notification
Date: Wed Jan 4 11:08:34 2017 +0800
3 files changed, 3 insertions(+), 17 deletions(-)
delete mode 100644 company-timeline-version-up/src/main/resources/cassandra/data/framework/TextDefDto/20161229142900_INSERT_com.worksap.company.hue.core.dto.TextDefDto.json
delete mode 100644 company-timeline-version-up/src/main/resources/cassandra/data/framework/TextDefDto/201701041026172198_INSERT_com.worksap.company.hue.core.dto.TextDefDto.json
create mode 100644 company-timeline-version-up/src/main/resources/cassandra/data/framework/TextDefDto/20170104172198_INSERT_com.worksap.company.hue.core.dto.TextDefDto.json
Successfully rebased and updated refs/heads/ticket-172198.最后push
git push origin ticket-172198 -f
會顯示成功信息:
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (11/11), 987 bytes | 0 bytes/s, done.
Total 11 (delta 1), reused 9 (delta 0)
To git@scm.hue.workslan:company/collabo-verup-files.git
- 776dc7a...aec7724 ticket-172198 -> ticket-172198 (forced update)
參考ref
http://www.lxweimin.com/p/964de879904a
http://www.lxweimin.com/p/4f1bad9e0a2d
重新在遠程建新分支
先切換到遠程develop
git checkout origin/develop
然后建一個新分支newbranch2
git checkout -b newbranch2
如果是空的,直接push,相等于覆蓋
git push origin newbranch2
把自己的修改代碼保存,在ticket分之下,checkout一個新的備份分支就可以
git checkout -b ticket-xxx-backup
然后切回ticket分支,rebase到新分支上去,有幾個commit 參數就是幾
git rebase HEAD~1 -- onto newbranch2
最后push,如果已經push過,就加-f參數
git 切換到遠程tag
git fetch origin tag <tagname>
git checkout <tagname>
git通過rebase,形成一條線
https://segmentfault.com/q/1010000000430041
對應到我司工程:
git fetch origin <branchName>
git rebase HEAD~n -- onto origin/branchName
git push ticket-xxxxxx
git 把本地分支branch1更新到最新
如果本地沒有改動,也不用fetch merge了
先git fetch origin branch1
再切到一個別的分支上
把本地分支刪除
git branch -D branch1
git checkout origin/branch1 -b branch1
或者直接fetch origin 再git rebase origin/branch1
**** 如何合并兩個倉庫****
假設現在有兩個repo:repo1,repo2,每個repo中都已經有一堆提交記錄了,現在想把repo2中的記錄合并到repo1中,命令如下:
cd repo1
git remote add other ../repo2
git fetch other
git checkout -b repo2 other/master
git checkout master
git merge repo2
解釋:
進入repo1文件夾
添加repo2作為repo1的遠程倉庫,并命名為other
將repo2的內容獲取到repo1注意,使用fetch而不是pull,關于fetch和pull的區別[請戳這里](https://stackoverflow.com/questions/292357/what-are-the-differences-between-git-pull-and-git-fetch)
在repo1中創建名為repo2的新分支,同時切換到該分支,并且使用上一步獲取的內容中的master分支的內容
切換到repo1的master分支
將repo2分支的內容合并到master分支
[](http://sabrinaluo.com/tech/2015/12/24/how-to-merge-repos-with-git/#一句話總結)一句話總結
將一個倉庫的內容checkout到另一個倉庫的一個分支,將該分支與master分支合并
注:不一定是master,可以是任意你要合并過去的分支。
## 強制批量刪除本地分支
git branch | grep 'matched branch name' |xargs git branch -D