git常用命令總結(工作中常用到的)

來源:互聯網
上載者:User


前言

用git也好久了,感覺平時也就是git commit,git pull ,git push ,git add,git submodule,git stash,git branch,git checkout,git merge 等等,下面這個總結是分類進行的,比較清楚明了。

建立

複製一個已建立的倉庫:

$ git clone ssh://haorooms@domain.com/blog.git
建立一個新的本地倉庫:

$ git init
本地修改

顯示工作路徑下已修改的檔案:

$ git status
顯示與上次提交版本檔案的不同:

$ git diff
把當前所有修改添加到下次提交中:

$ git add
把對某個檔案的修改添加到下次提交中:

$ git add -p <file>
提交本地的所有修改:

$ git commit -a
提交之前已標記的變化:

$ git commit
附加訊息提交:

$ git commit -m 'message here'
提交,並將提交時間設定為之前的某個日期:

git commit --date="`date --date='n day ago'`" -am "Commit Message"
修改上次提交:請勿修改發行的提交記錄!

$ git commit --amend
把當前分支中未提交的修改移動到其他分支

git stash
git checkout branch2
git stash pop
搜尋

從目前的目錄的所有檔案中尋找常值內容:

$ git grep "Hello"
在某一版本中搜尋文本:

$ git grep "Hello" v2.5
提交曆史

從最新提交開始,顯示所有的提交記錄(顯示hash, 作者資訊,提交的標題和時間):

$ git log
顯示所有提交(僅顯示提交的hash和message):

$ git log --oneline
顯示某個使用者的所有提交:

$ git log --author="username"
顯示某個檔案的所有修改:

$ git log -p <file>
誰,在什麼時間,修改了檔案的什麼內容:

$ git blame <file>
分支與標籤

列出所有的分支:

$ git branch
切換分支:

$ git checkout <branch>
建立並切換到新分支:

$ git checkout -b <branch>
基於當前分支建立新分支:

$ git branch <new-branch>
基於遠程分支建立新的可追溯的分支:

$ git branch --track <new-branch> <remote-branch>
刪除本地分支:

$ git branch -d <branch>
給目前的版本打標籤:

$ git tag <tag-name>
更新與發布

列出當前配置的遠程端:

$ git remote -v
顯示遠程端的資訊:

$ git remote show <remote>
添加新的遠程端:

$ git remote add <remote> <url>
下載遠程端版本,但不合并到HEAD中:

$ git fetch <remote>
下載遠程端版本,並自動與HEAD版本合并:

$ git remote pull <remote> <url>
將遠程端版本合并到本地版本中:

$ git pull origin master
將本地版本發布到遠程端:

$ git push remote <remote> <branch>
刪除遠程端分支:

$ git push <remote> :<branch> (since Git v1.5.0)
或
git push <remote> --delete <branch> (since Git v1.7.0)
發布標籤:

$ git push --tags
合并與重設

將分支合并到當前HEAD中:

$ git merge <branch>
將當前HEAD版本重設到分支中:請勿重設發行的提交!

$ git rebase <branch>
退出重設:

$ git rebase --abort
解決衝突後繼續重設:

$ git rebase --continue
使用配置好的merge tool 解決衝突:

$ git mergetool
在編輯器中手動解決衝突後,標記檔案為已解決衝突

$ git add <resolved-file>

$ git rm <resolved-file>
撤銷

放棄工作目錄下的所有修改:

$ git reset --hard HEAD
移除緩衝區的所有檔案(i.e. 撤銷上次git add):

$ git reset HEAD
放棄某個檔案的所有本地修改:

$ git checkout HEAD <file>
重設一個提交(通過建立一個截然不同的新提交)

$ git revert <commit>
將HEAD重設到指定的版本,並拋棄該版本之後的所有修改:

$ git reset --hard <commit>
將HEAD重設到上一次提交的版本,並將之後的修改標記為未添加到緩衝區的修改:

$ git reset <commit>
將HEAD重設到上一次提交的版本,並保留未提交的本地修改:

$ git reset --keep <commit>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.