git 經常使用操做

查看某文件的某些行的變化歷史:html

$ git log --pretty=short -u -L 2003,2005:Executor.cppgit

http://stackoverflow.com/questions/8435343/retrieve-the-commit-log-for-a-specific-line-in-a-filegithub

 

Undo a commit and redo

$ git commit -m "Something terribly misguided"              (1)
$ git reset --soft HEAD~                                    (2)
<< edit files as necessary >>                               (3)
$ git add ...                                               (4)
$ git commit -c ORIG_HEAD                                   (5)
  1. This is what you want to undo緩存

  2. This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message1, or both. Leaves working tree as it was before git commit.服務器

  3. Make corrections to working tree files.app

  4. git add whatever changes you want to include in your new commit.ide

  5. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEADcommit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option instead.fetch

 

 

 

git ignore 對某些文件失效:ui

To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use: git rm --cached filename

To untrack every file that is now in your .gitignore:

First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"

To undo git rm --cached filename, use git add filename.

http://stackoverflow.com/questions/6964297/untrack-files-from-git

git update-index should do what you want

This will tell git you want to start ignoring the changes to the file
git update-index --assume-unchanged path/to/file

When you want to start keeping track again
git update-index --no-assume-unchanged path/to/file

Github Documentation: update-index 

 
 
分支操做
我先開個分支
$ git checkout -b transform
push 上去
$ git push -u origin transform
 
同事:
先到 目錄
$ cd 
 
獲取遠程倉庫
$ git fetch
 
在本地新建 transform 分支並設置該分支 track 遠程 transform
$ git checkout --track -b transform origin/transform
 
在這個分支下開發程序變換的功能。git add, commit 後 push 到服務器上
$ git push
上面這個命令會將本地 transform 分支上的修改 push 到遠程對應的 transform 分支上
 
以上操做不影響 master 分支,若是要切換到 master 分支,可用
$ git checkout master
要切換到 transform分支可用
$ git checkout transform

 

 

 

 

 

 

 

from:  http://rogerdudler.github.io/git-guide/index.zh.html

 

git - 簡明指南

助你入門 git 的簡明指南,木有高深內容 ;)

做者:羅傑·杜德勒 
感謝:@tfnico@fhd 和 Namics
其餘語言 englishdeutschespañolfrançaisindonesianitalianonederlandspolskiportuguêsрусскийtürkçe
မြန်မာ日本語한국어 Vietnamese 
若有紕漏,請在 github 提報問題

Frontify - Collaboration for Web Designers & Front-End Developers

建立新倉庫

建立新文件夾,打開,而後執行 
git init
以建立新的 git 倉庫。

檢出倉庫

執行以下命令以建立一個本地倉庫的克隆版本:
git clone /path/to/repository 
若是是遠端服務器上的倉庫,你的命令會是這個樣子:
git clone username@host:/path/to/repository

工做流

你的本地倉庫由 git 維護的三棵「樹」組成。第一個是你的 工做目錄,它持有實際文件;第二個是 暫存區(Index),它像個緩存區域,臨時保存你的改動;最後是 HEAD,它指向你最後一次提交的結果。

添加和提交

你能夠提出更改(把它們添加到暫存區),使用以下命令:
git add <filename>
git add *
這是 git 基本工做流程的第一步;使用以下命令以實際提交改動:
git commit -m "代碼提交信息"
如今,你的改動已經提交到了 HEAD,可是還沒到你的遠端倉庫。

推送改動

你的改動如今已經在本地倉庫的 HEAD 中了。執行以下命令以將這些改動提交到遠端倉庫:
git push origin master
能夠把 master 換成你想要推送的任何分支。 

若是你尚未克隆現有倉庫,並欲將你的倉庫鏈接到某個遠程服務器,你能夠使用以下命令添加:
git remote add origin <server>
如此你就可以將你的改動推送到所添加的服務器上去了。

分支

分支是用來將特性開發絕緣開來的。在你建立倉庫的時候,master 是「默認的」分支。在其餘分支上進行開發,完成後再將它們合併到主分支上。

建立一個叫作「feature_x」的分支,並切換過去:
git checkout -b feature_x
切換回主分支:
git checkout master
再把新建的分支刪掉:
git branch -d feature_x
除非你將分支推送到遠端倉庫,否則該分支就是 不爲他人所見的
git push origin <branch>

更新與合併

要更新你的本地倉庫至最新改動,執行:
git pull
以在你的工做目錄中 獲取(fetch) 並 合併(merge) 遠端的改動。
要合併其餘分支到你的當前分支(例如 master),執行:
git merge <branch>
在這兩種狀況下,git 都會嘗試去自動合併改動。遺憾的是,這可能並不是每次都成功,並可能出現衝突(conflicts)。 這時候就須要你修改這些文件來手動合併這些衝突(conflicts)。改完以後,你須要執行以下命令以將它們標記爲合併成功:
git add <filename>
在合併改動以前,你能夠使用以下命令預覽差別:
git diff <source_branch> <target_branch>

標籤

爲軟件發佈建立標籤是推薦的。這個概念早已存在,在 SVN 中也有。你能夠執行以下命令建立一個叫作 1.0.0 的標籤:
git tag 1.0.0 1b2e1d63ff
1b2e1d63ff 是你想要標記的提交 ID 的前 10 位字符。能夠使用下列命令獲取提交 ID:
git log
你也能夠使用少一點的提交 ID 前幾位,只要它的指向具備惟一性。

替換本地改動

假如你操做失誤(固然,這最好永遠不要發生),你能夠使用以下命令替換掉本地改動:
git checkout -- <filename>
此命令會使用 HEAD 中的最新內容替換掉你的工做目錄中的文件。已添加到暫存區的改動以及新文件都不會受到影響。

假如你想丟棄你在本地的全部改動與提交,能夠到服務器上獲取最新的版本歷史,並將你本地主分支指向它:
git fetch origin
git reset --hard origin/master

實用小貼士

內建的圖形化 git:
gitk
彩色的 git 輸出:
git config color.ui true
顯示歷史記錄時,每一個提交的信息只顯示一行:
git config format.pretty oneline
交互式添加文件到暫存區:
git add -i

評論

相關文章
相關標籤/搜索