2 簡介GIT是一個分布式的版本控制系統。
分布式的意思是它不依賴一個中央服務器,每個開發者clone得到的倉庫都包含了完整的變更記錄。
3 GIT 的四種協議file: 只能訪問本地repository
git:后臺運行git-daemon,這種方式架設簡單,運行速度快,但是傳輸的數據沒有加密。
ssh:安全可靠的連接,如同訪問本地文件一樣,但是權限不好控制。
http/https:通過webdav實現,可以通過瀏覽器查看代碼,權限管理與svn協議相同,https保證了數據傳輸的安全,但是配置復雜。
4 配置git告訴其他開發者你是誰,怎么聯系你。
這些信息可以是全局的,也可以在某個項目中單獨設置。
全局的配置文件位于~/.gitconfig
項目的配置文件位于<project>/.git/config
5 配置git全局配置
git config --global user.name “yourname”
git config --global user.email you@site.com
單個項目配置
git config --local user.name “yourname”
git config --local user.email you@site.com
6 要把本地項目文件納入GIT管理,直接在本地項目目錄中執行
git init
git add *
git commit -a -m "sth."
新建GIT項目
在代碼服務器上:
mkdir <project>
cd <project>
git init
7 把本地git的庫導入代碼服務器中
代碼服務器新建GIT項目:
cd /work/projects
mkdir <project>
cd <project>
git init --bare
本地(必須是commit過了的):
cd <project>
git remote add origin <yourname>@server:/path/to/project
git push origin master
以后提交到代碼服務器只需要:
git push
pull測試:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
git pull
8 從repository中取出代碼
使用ssh協議
git clone yourname@192.168.8.91:/work/projects/cerberus
上一條命令取出的代碼,包含了該repository完整的修訂記錄,從項目的創建到目前的最新版本。分布式就表現在這個地方,遠程服務器上是一個完整的repository,你取出的代碼是完整的,另一名開發者取出的代碼也是完整。
9 git的常用命令查看提交的日志信息
git log
查看項目中所有文件的狀態
git status
增加了哪些文件
修改了哪些文件
哪些文件沒有被git追蹤(通常是自動生成的文件)
…
10 git的常用命令添加文件到項目中
git add <filename>
刪除項目中的文件
git rm <filename>
重命名
git mv <filename>
<filename>可以是一個文件,也可以是一個目錄。對于add,會自動添加該目錄下的所有文件。對于rm,加上-r參數可以刪除整個目錄。
11 git的常用命令只提交新增的文件
git commit
提交新增的文件,以及修改過的文件
git commit -a
git commit 需要填寫日志,如果為空,git放棄本次提交。git 的日志對格式有一定的要求,但不是強制的。
12 git 的日志格式short description (lesser than 50 letters)
detail description
* file1: yourchanges
* file2: yourchanges
…
13 更新和提交更新代碼
git pull
錯誤:
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.
If you often merge with the same branch, you may want to
use something like the following in your configuration file:
[branch "master"]
remote = <nickname>
merge = <remote-ref>
[remote "<nickname>"]
url = <url>
fetch = <refspec>
See git-config(1) for details.
解決:
Under [branch "master"], try adding the following to the repo's Git config file (.git/config):
[branch "master"]
remote = origin
merge = refs/heads/master
或者:
$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master
http://stackoverflow.com/questions/658885/how-do-you-get-git-to-always-pull-from-a-specific-branch
把修改后的代碼提交到遠程repository
git commit -a #提交 到本地git repository
git pull #取出遠程repository中最新的代碼
可能需要合并沖突
git push #提交到遠程 repository
git push出錯:
fatal: No destination configured to push to.
解決:
$ git remote add origin yourname@192.168.8.91:/work/projects/cerberus
// to push the master branch to the origin remote we added above:
$ git push origin master
// after that you can just do:
$ git push
http://blog.csdn.net/brave_heart_lxl/archive/2010/04/20/5507099.aspx
14 git checkoutgit checkout可以取出以下對象
一個版本
一個分支
一個文件
15 git 的 版本號git 使用一個40位數字來表示版本號;
HEAD 表示本地倉庫的當前版本號
HEAD^表示本地倉庫的上一個版本號
取出某個特定版本:
git checkout e9e986b7f1b7b1a0acf4b919e23e929705a8a209
git check “HEAD^”
16 放棄修改放棄某個文件或目錄的修改
git checkout <filename>
放棄所有修改,回到某一個版本
git reset HEAD
git reset --hard HEAD
17 版本回退有時候會發現新的代碼根本不能工作,我們可能需要回退到一個舊的版本,有兩種方式:
git revert HEAD
這種方式會記錄下錯誤的修改,以及回滾的歷史
git reset “HEAD^”
這種方式直接放棄當前的版本,退回上一個版本,不會記錄所做的修改。
18 分支管理查看分支列表
git branch
git branch -a #查看包括遠程repository在內的所有分支
創建并切換到新的分支
git branch <barnch_name>
git checkout <branch_name>
或者
git checkout -b <branch_name>
19 刪除分支:
如果分支已經合并到master
git branch -d <branch_name>
如果分支還沒有被合并,可以強制刪除
git branch -D <branch_name>
不能刪除當前的分支
20 合并分支git merge <branch>
git 會嘗試自動合并分支中沖突的內容
如果合并失敗,會在文件中以diff的形式顯示兩者的差異,用戶需要手動解決沖突
21 當合并失敗git會提示那些沖突無法自動合并,有兩種解決方法:
手工修改這些文件,合并其中的內容,然后commit。
使用當前分支,或者要合并分支的內容,然后提交
git checkout --ours <conflict file>
git checkout --theirs <conflict file>