git 的常用用法

刪除遠程分支: 
git push origin --delete <branchName> 如: git push origin --delete release/release
切換分支:git checkout name
撤銷修改:git checkout -- file
刪除文件:git rm file
查看狀態:git status
添加記錄:git add file 或 git add .
添加描述:git commit -m "miao shu nei rong"
同步數據:git pull
提交數據:git push origin name
分支操作
查看分支:git branch
創建分支:git branch name
切換分支:git checkout name
創建+切換分支:git checkout -b name
合併某分支到當前分支:git merge name
刪除分支:git branch -d name
刪除遠程分支:git push origin :name


git push <遠程主機名> <本地分支名>:<遠程分支名>  將本地分支push到遠程分支上


git log --graph 最近的接交圖


git branch -vv 查看分支最近的提交圖

git clone url #克隆新的版本庫
git init 
git pull repo_name #有關聯的遠程庫,抽取並和本地合併
git fetch remote_repo_name #抽取並新建分支


rename:
git branch -m <oldname> <newname>

If you want to rename the current branch, you can simply do:

git branch -m <newname>


 #在當前commit對象上新建分支 指針head#head指向正在工作中的本地分支的指針(別名)#不會切換到新建的分支上git branch branch1#切換分支將head指向branch1git checkout branch1 #工作流程卡#在不同的分支裏反覆切換,並在時機成熟時把他們合併到一起#git的分支實際是一個包含所指向對象校驗和的文件(40個字符長度SHA-1字串)#分支的新建和合並#0、自己工作分支mybranch,工作的好好的 #1、突然有新需求,先切換到生產環境分支product;git checkout product#2、爲新需求新建分支branch_pack,切到其中,並在其中編碼,直到通過測試用例# -b 新建分支並切換到其上git checkout -b issueXXXX #3、切換到生產環境分支product,將2中的開發工作分支branch_pack合並進來,然後推送到生產服務器上git checkout product# 合併git merge branch_pach#衝突解決,merge失敗時很可能因對同一文件的同時更改,所以必須手動人工解決 取捨代碼,確認衝突解決後需要把衝突文件 git add到staged area,運行git status查看所有狀態確保無誤後再git commit提交,註釋好衝突爲什麼這麼解決git push 遠程倉庫名 本地倉庫名#分支刪除git branch -d branch_pach4、切換到mybranch繼續自己的工作git checkout mybranch #切換分支最好沒有待提交的文件,即stage area是清潔的#查看各個分支最後一個提交對象的信息git branch -vgit branch --merge/no-merged#查看哪些分支已被併入當前分支(譯註:也就是說哪些分支是當前分支的直接上游#給分支生成patch文件可以給git format-patch #把本地名爲serverfix的分支推送到遠程origin庫的awesomebranch分支git push origin serverfix:awesomebranch #跟蹤遠程分支 從遠程分支checkout出的本地分支成爲 跟蹤分支git checkout -b sf origin/serverfixgit checkout --track origin/serverfix #刪除遠程分支git push origin :serverfix git兩種開發分支方法長期分支:一個主分支保持穩定代碼,其餘多個開放分支特性topic分支:多個短期單一功能的分支#給當前文件 打特定版本標籤git tag -a beta0.1 -m "some 註釋"#回退到某個taggit tag beta0.1






git cherry-pick   <commit-id>                      //能把別的分支的commit-id移到當前分支。   也可能恢復刪除的commit;

git cherry-pick  <deleted commit-id>

 

git reset HEAD^                                           //刪除本次的commit,恢復到上次的提交狀態。本次提交的內容並不刪除.

git reset --hard  HEAD^                              //刪除本次的commit,   並將當前的代碼恢復到上次提交的狀態。

git reset --hard <delete commit-id>         //恢復已刪除的commit.

 

git log                                                            //查看所有的commit歷史.

git reflog                                                       //查看所有的log, 包括刪除的log.

 

git blame    <commit-id>  <file>               //查看file的修改記錄.

 

git am    -3    <patch-file>                         //應用一個patch

git apply                                                     //應用一個path到某個文件或commit;

git format-patch  HEAD^                         //將當前的commit 生成patch文件

 

git   bisect   start   <commit-id>              //開始一個二分法查錯。

git   bisect   good                                     //標記一個正確的版本

git   bisect   bad                                       //標記一個錯誤的版本

 

git diff     <HEAD1> <HEAD2>              //對比這兩個HEAD之間的區別.

git difftool  <HEAD1>  <HEAD2>         //同git diff,     但可以用可視化工具對比.

git difftool   -g  -t

 

git fetch                                                     //fetch一個別的地方的分支到當前倉庫

git checkout                                             //切換分支

git checkout ./                                          //撤消當前項目所有未提交的改動

git checkout -b <new branch>  branch   //創建一個基於branch的新分支new branch

git branch                                                 //查看當前倉庫的本地branch

git branch -a                                            //查看當前倉庫的所有 branch,   包括遠端的信息

git branch --track <new branch>   <remote branch>

//創建一個追蹤branch,   用於本地與遠端的同步。git pull, git push使用.

git pull                                                       //同步遠端分支.

git push                                                    //將本地的commit提交的遠端.

git remote -v                                            //查看遠端信息

git remote add <remote name>  <remote url>

//添加一個遠端,

git remote add r   [email protected].2.x:path/xx.git

//添加一個可提交的遠端.

git  clone   <remote url>                       //從遠端複製一個git倉庫,   和fetch的功能類似

git  tag   <new tag>                               //給當前HEAD打一個tag

git  tag -d <tag name>                         //刪除一個tag;

git add  .                                                 //將當前項目的改動添加到git cache中

git  commit -am 'msg'                           //將當前改動提交.

git grep    <str>                                     //在倉庫中搜索字符串

 

git stash                                                //將未commit的index緩存起來。

git stash   list                                        //查看所有的index緩存.

git stash  apply                                    //應用最後一次緩存的index.

git stash apply stash@{2} //應用指定的緩存.

git stash  apply --index

git stash  pop                                      //應用最後的存儲並從緩存中移除

git stash  branch testchanges          //從存儲中創建分支.

git stash show -p stash@{0}  | git apply -R

git stash show -p | git apply -R


git下修改commit的message的方法

1.用git rebase 切換到要修改的commit上,接下來修改當前commit的信息,最後再還原commit.

一不小心commit時message寫錯了,找了好長時間,走了不少彎路,才找到方法……
git rebase -i master~5
這個是找出master分支最近5次的commit,看見那個寫錯的了吧?把pick改成edit,保存退出。
此時工作目錄已經變成這次commit的樣子了,不要慌,然後:
git commit --amend -m "message"
修改這次的message。最後:
git rebase --continue
怎麼樣,成功了吧?

利用這個方法也可以修改commit的文件。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章