查看、添加、提交、刪除、找回,重置修改文件
git add <file> # 將工作文件修改提交到本地暫存區(qū)
git add . # 將所有修改過的工作文件提交暫存區(qū)
git revert <$id> # 恢復(fù)某次提交的狀態(tài),恢復(fù)動作本身也創(chuàng)建次提交對象
git revert HEAD # 恢復(fù)最后一次提交的狀態(tài)
查看提交記錄
git log git log <file> # 查看該文件每次提交記錄
git log -p <file> # 查看每次詳細修改內(nèi)容的diff
git log -p -2 # 查看最近兩次詳細修改內(nèi)容的diff
git log --stat #查看提交統(tǒng)計信息
clone指定分支命令
git clone <remote_repo> -b <branch>
<分支管理>
git pull的作用是從一個倉庫或者本地的分支拉取并且整合代碼。
git pull 倉庫的名字 分支的名字(如果都不寫,會有一個默認值。按照git branch 設(shè)置的默認跟蹤的服務(wù)器和分支來拉取。)
git pull origin master 拉取遠程服務(wù)器origin的master分支
查看本地分支和遠程分支的映射關(guān)系
git branch -vv
git 本地分支關(guān)聯(lián)遠程分支
解決方式:git branch --set-upstream-to=origin/remote_branch your_branch
其中,origin/remote_branch是你本地分支對應(yīng)的遠程分支;your_branch是你當(dāng)前的本地分支。
情況二:遠程無對應(yīng)的分支
步驟如下:
1.新建一個本地的分支
git branch -b newbranch? //這個命令是新建一個分支,并切換到該分支上去
2.新建一個遠程分支(同名字的遠程分支)
git push origin newbranch:newbranch? //創(chuàng)建了一個遠程分支名字叫 newbranch
3.把本地的新分支,和遠程的新分支關(guān)聯(lián)
git push --set-upstream origin newbranch
這時就可以在這個分支下使用 git pull 推送支遠程的新分支上了
創(chuàng)建分支:
git branch branchname
拉取遠程分支并創(chuàng)建本地分支
git checkout -b 本地分支名x origin/遠程分支名x
git branch new_branch? commitId,用這個commit創(chuàng)建一個新的分支 這樣,該新的分支,就保留了commitId對應(yīng)的修改,我們可以使用git log 命令來查看是否正確
//恢復(fù)刪除的分支名稱 找回已刪除分支
1 git reflog
2 git branch xxx分支名稱 HEAD@{1}
把分支推到遠程分支:
git push <遠程主機名> <本地分支名>:<遠程分支名>
git push origin master:developer_branch(將本地的master 推送到遠程分支的developer_branch分支上)
git push origin branchname
git push -u origin branchname# 將本地主分支推到遠程(如無遠程主分支則創(chuàng)建,用于初 始化遠程倉庫)
git push origin master --force#如果git要強制覆蓋,那么可以使用--force命令。
$ git checkout -b [branch]
$ git branch --set-upstream
$ git push -u origin
這個時候第一次push需要網(wǎng)址:
$ git add --all? 或者使用 git add .(所得的文件) | git add file.js(對用指定文件)
$ git commit -m "提交信息"
$ git remote add origin '遠程倉庫url'
$ git push -u origin origin(對應(yīng)遠程分支名)
然后下一次就不用那么麻煩了,直接:
$ git add --all? | git add .(所得的文件) | git add file.js(對用指定文件)
$ git commit -m "信息"
$ git push
查看分支:
git branch <-a>
切換分支:
git checkout branchname
合并分支:
(快速合并,合并指定分支到當(dāng)前分支)git merge branchname
(普通合并) git merge --on-ff -m "msg" branchname
git merge origin/master-1.2.x
分支push代碼:
git push? push所有分支
刪除分支:
git branch -d branchname
刪除遠程分支:
git push origin --delete <branchName>
從遠程克隆某個分支:
git clone http:xxxx.git -b branchname
將本地修改存儲起來:
git stash
可以看到保存的信息:
git stash list
還原暫存的內(nèi)容:
git stash pop stash@{0}
清楚.iml文件的緩存,過濾掉iml文件不提交
git rm -r --cached .?
git add .?
git commit -m "update .gitignore'"
git branch # 列出目前有多少branch
git branch new-branch # 產(chǎn)生新的branch (名稱: new-branch), 若沒有特別指定, 會由目前所在的branch / master 直接復(fù)制一份.
git branch new-branch master # 由master 產(chǎn)生新的branch(new-branch)
git branch new-branch v1 # 由tag(v1) 產(chǎn)生新的branch(new-branch)
git branch -d new-branch # 刪除new-branch
git branch -D new-branch # 強制刪除new-branch
git checkout -b new-branch test # 產(chǎn)生新的branch, 并同時切換過去new-branch
git push origin 本地分支名:遠程分支名
# 與remote repository 有關(guān)
git branch -r # 列出所有Repository branch
git branch -a # 列出所有branch
tag 操作
先 git clone 整個倉庫,然后 git checkout tag_name 就可以取得 tag 對應(yīng)的代碼了。
但是這時候 git 可能會提示你當(dāng)前處于一個“detached HEAD" 狀態(tài),因為 tag 相當(dāng)于是一個快照,是不能更改它的代碼的,如果要在 tag 代碼的基礎(chǔ)上做修改,你需要一個分支:
git checkout -b branch_name tag_name
這樣會從 tag 創(chuàng)建一個分支,然后就和普通的 git 操作一樣了。
tag 分享標簽
默認情況下,git push 并不會把標簽傳送到遠端服務(wù)器上,只有通過顯式命令才能分享標簽到遠端倉庫。其命令格式如同推送分支,運行g(shù)it push origin [tagname] 即可:
$ git push origin v1.5.0
如果要一次推送所有本地新增的標簽上去,可以使用 --tags 選項:
$ git push origin --tags
1:打 tag 標簽
Git tag -a v1.0 -m “commit version 1.0“
加上 -f 覆蓋原有的tag
git tag -f v1.0
2:push 到遠程倉庫
push所有tag到遠程倉庫:git push origin –tags (注意tags前是兩個短橫杠)
push單個tag到遠程倉庫:git push origin [tagname]
3:刪除 tag 便簽
git tag -d v1.0
刪除遠程的
git push origin? --delete 【要刪除的tag】
4:查看 tag 標簽
git tag
5:切換標簽
git checkout v1.0
6 先根據(jù)這個tag新建一個分支
git checkout -b 新分支 tag名
用git show命令可以查看標簽的版本信息:
$ git show v0.1.2
完整的打tag
git add *
git commit -m "v0.3"
git tag v0.3
git push
git push origin v0.3
修改tag名稱
git tag? 新tag? 舊tag
git tag -d 舊tag
刪除遠程的
git push origin? --delete 【要刪除的tag】
如果回退到某個版本
切換到,master
git reset --hard efdec9d3d07bc26bf42cf5d6126e00f4f2922b3a
切換到master分支
git reset —hard xxx
git branch 分支名
git checkout 分支名
git push origin 分支名
切換到master分支
git reset --hard origin/master
git reset --hard HEAD ?終止合并,撤銷未提交的合并,會把工作目錄和索引都還原到git merge命令之前。
git reset --hard ORIG_HEAD? 撤銷已經(jīng)提交的合并??赡軙龅絹G失目錄中未提交的修改。用git reset --merge ORIG_HEAD 可避免。
比如多個OEM 分支同步提交的代碼
1 首先先提交A 分支的代碼
2 git reset --soft HEAD~1
3 切換到B分支上; 這時候; 會把A分支修改的代碼; 帶到B 分支上 修改提交;即可
4 切換A 更新一下代碼; 否會顯示你一直未提交代碼
git查看某個文件的修改歷史
1. git log filename
可以看到fileName相關(guān)的commit記錄
2. git log -p filename
可以顯示每次提交的diff
3. 只看某次提交中的某個文件變化,可以直接加上fileName
git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b filename
如果修復(fù)git 忽略不生效的問題
.gitignore只能忽略那些原來沒有被track的文件,如果某些文件已經(jīng)被納入了版本管理中,則修改.gitignore是無效的。解決方法就是先把本地緩存刪除(改變成未track狀態(tài)),然后再提交:
git rm -r --cached .
git add .
git commit -m 'update gitignore'
1、git log -- filename(git log filename)
可以看到該文件相關(guān)的commit記錄
2、git log -p filename
可以顯示該文件每次提交的diff
3、git show comit_id filename
可以查看某次提交中的某個文件變化
4、git show commit_id
查看某次提交
5、gitk --follow filename
以圖形化界面的方式顯示修改列表
————————————————
有時候我們希望只合并某個分支上的某次修改commit到指定的分支上.那么就可以使用cherry-pick命令來操作了.
使用git log 查看提交的信息,記住commit id.
git checkout 要修改的分支
git cherry-pick 某個commit id? // 把某個commit id的提交合并到當(dāng)前分支.
git remote -v
8.項目轉(zhuǎn)移 git 倉庫,例如 A 轉(zhuǎn)到 B
//—————情況一
如果 A 倉庫掛了無法恢復(fù),則查看項目中誰的代碼最全面,然后記得 commit 到本地。
然后在新的 git 服務(wù)器上新建 B 倉庫,
例如:git push --mirror http://git.oschina.net/xqf/JcenterTest.git? http://git.oschina.net/xqf/JcenterTest.git就是 B 倉庫地址
輸入 B 倉庫地址和密碼就 ok,這樣本地代碼就可以不用,之后的開發(fā)都在 B 倉庫進行
//————————情況二
如果遠程 A 倉庫正常,A 轉(zhuǎn)移到 B
a.從原地址克隆一份裸版本庫,比如原本托管于github,現(xiàn)在想轉(zhuǎn)到碼云上
例如:把 https://github.com/xuqingfeng77/AndroidMoreFunctionDemo 轉(zhuǎn)移到碼云上
git clone --bare https://github.com/xuqingfeng77/AndroidMoreFunctionDemo
--bare 創(chuàng)建的克隆版本庫都不包含工作區(qū),直接就是版本庫的內(nèi)容,這樣的版本庫稱為裸版本庫,執(zhí)行完命令可以看到AndroidMoreFunctionDemo.git 文件夾
b.然后到碼云上新建一個倉庫
c.以鏡像推送的方式上傳代碼到碼云服務(wù)器上
cd AndroidMoreFunctionDemo.git
git push --mirror http://git.oschina.net/xqf/AndroidMoreFunctionDemo.git
輸入碼云的賬號密碼就 ok
Unknown SSL protocol error in connection to github.com:443
//忽略證書
git config http.sslVerify "false"
本地工作區(qū)貯藏功能
(1)git stash push --keep-index --include-untracked --message 注釋 -- 文件1 文件2..? : --keep-index 清空工作區(qū)及未提交與版本庫一致 ,--include-untracked 包含未添加到暫存區(qū)的
(2)git stash list? :查看stash了哪些存儲
(3)git stash show :顯示做了哪些改動,默認show第一個存儲,如果要顯示其他存貯,后面加stash@{$num},比如第二個 git stash show stash@{1}
(4)git stash show -p : 顯示第一個存儲的改動,如果想顯示其他存存儲,命令:git stash show? stash@{$num}? -p ,比如第二個:git stash show? stash@{1}? -p
(5)git stash apply :應(yīng)用某個存儲,但不會把存儲從存儲列表中刪除,默認使用第一個存儲,即stash@{0},如果要使用其他個,git stash apply stash@{$num} , 比如第二個:git stash apply stash@{1}
(6)git stash pop :命令恢復(fù)之前緩存的工作目錄,將緩存堆棧中的對應(yīng)stash刪除,并將對應(yīng)修改應(yīng)用到當(dāng)前的工作目錄下,默認為第一個stash,即stash@{0},如果要應(yīng)用并刪除其他stash,命令:git stash pop stash@{$num} ,比如應(yīng)用并刪除第二個:git stash pop stash@{1}
(7)git stash drop stash@{$num} :丟棄stash@{$num}存儲,從列表中刪除這個存儲
(8)git stash clear :刪除所有緩存的stash