Git安裝配置及基本使用
安裝git
1)windows
安裝msysgit,下載地址:http://msysgit.github.io/
安裝的時候,基本選擇默認設置,但是:
在Adjusting your PATH environment頁,勾選Run Git from the Windows Command Prompt
2)ubuntu
用命令“git --version”查看是否已安裝,且版本為1.9.5或更高。若沒安裝或版本太低:
- $ sudo apt-get install git-core git-gui git-doc gitk </ol> </div>
- D:/test> git help </ol> </div>
- D:/test> git log HEAD^ </ol> </div>
- D:/test> git log "HEAD^" </ol> </div>
- $ git help </ol> </div>
- git config --global user.name "test" # 請換成你自己的名字,除非你湊巧也叫wukong.sun
- git config --global user.email "test@163.com" # 同上
- git config --global push.default simple # 要是你非要用低版本的Git(比如1.7.x),好吧,那就不設simple設current,否則你的Git不支持
- git config --global core.autocrlf false # 讓Git不要管Windows/Unix換行符轉換的事
- git config --global gui.encoding utf-8 # 避免git gui中的中文亂碼
- git config --global core.quotepath off # 避免git status顯示的中文文件名亂碼 </ol> </div> 其中最后兩個配置是關于中文亂碼的,基本夠用了。
- git config --global core.ignorecase false </ol> </div>
- $ssh-keygen -t rsa -C "test@163.com" </ol> </div> 然后一路回車,不要輸入任何密碼之類,生成ssh key pair。然后就生成一個目錄.ssh ,里面有兩個文件:id_rsa , id_rsa.pub
- $ ssh-add ~/.ssh/id_rsa </ol> </div>
- $ cat ~/.ssh/id_rsa.pub </ol> </div>
- $ mkdir git_repo
- $ cd git_repo
- $ git init
- $ echo "test" > README.mkd
- $ git add README.mkd
- $ git commit -m "add README.mkd file"
- $ git remote add origin git@github.com:username/test.git
- $ git push -u origin master </ol> </div>
- $ cd git_repo
- $ git remote add origin git@github.com:username/test.git
- $ git push -u origin master </ol> </div>
- $ git clone git@github.com:username/test.git
- $ cd test
- $ vim .git/config
- [core]
- repositoryformatversion = 0
- filemode = true
- bare = false
- logallrefupdates = true
- [remote "origin"]
- url = git@github.com:username/test.git
- url = git@gitshell.com:username/test.git
- url = git@bitbucket.org:username/test.git
- fetch = +refs/heads/*:refs/remotes/origin/*
- [branch "master"]
- remote = origin
- merge = refs/heads/master </ol> </div> 然后第一次提交時需要執行git push -u origin master,再往后就只需要執行git push就能把修改提交到上述三個遠端倉庫了。
- $ git clone git@github.com:username/test.git
- $ cd test
- $ git checkout --orphan new_branch
- Switched to a new branch 'new_branch'
- $ git rm -rf . # 刪除舊工作目錄樹中所有文件
- $ rm .gitignore # 如果有該文件的話就刪除
- $ echo "orphan branch" > README.mkd
- $ git add .
- $ git commit -m "add README.mkd file"
- $ git push origin new_branch </ol> </div>
- $ git config --global user.name "username"
- $ git config --global user.email test@163.com
- $ git config --global core.editor vim
- $ git config --global merge.tool vimdiff
- $ git config --global color.status auto
- $ git config --global color.branch auto
- $ git config --global color.interactive auto
- $ git config --global color.diff auto
- $ git config --global push.default simple
- $ git config --global alias.co checkout
- $ git config --global alias.ci commit
- $ git config --global alias.st status
- $ git config --global alias.last 'log -1 HEAD'
- $ git config --global alias.unstage 'reset HEAD --' </ol> </div>
- $ git add .
- $ git add --all
- $ git add *.txt
- $ git add directory/*.sh </ol> </div> 突然你又不想git add了,那么執行以下命令:
- $ git reset .
- $ git reset *.txt
- $ git reset directory/*.sh </ol> </div> 3)git rm
- $ git rm filename
- $ git rm -f *.txt
- $ git rm -r . </ol> </div> 4)git commit
- $ git commit -m "add a file and remove a file" </ol> </div>
- $ git reset HEAD^ </ol> </div> 你commit之后發現少添加了一個文件:
- $ git commit -m'msg'
- $ git add forget_file
- $ git commit --amend </ol> </div> 你的 commit 已經 push 到遠程分支(master)了,現在你想反悔了:
- $ git clone git@github.com:username/test.git
- $ cd test
- $ git reset HEAD^
- $ git push -f master </ol> </div>
- $ git status </ol> </div> 6)git checkout
- $ git checkout branch1
- $ git checkout -b new_branch </ol> </div> 取消本地改動:
- $ git checkout -- file_name </ol> </div> 7)git branch
- $ git branch --list
- $ git branch --all
- $ git branch --remotes
- $ git branch new_branch
- $ git branch --delete branch_name
- $ git branch -D branch_name </ol> </div> 刪除remote tracking branch,就是git branch -r命令列出的分支。
- $ git branch -r
- $ git branch -d -r origin/develop </ol> </div> 8)合并分支
- $ git checkout branch_name
- $ git checkout master
- $ git merge branch_name </ol> </div> 9)刪除遠程分支
- $ git branch -d branch_name
- $ git branch -D branch_name
- $ git push origin :branch_name </ol> </div>
- $ git diff filename
- $ git diff .
- $ git diff revision1 revision2
- $ git diff branch1 branch2 </ol> </div> DIFF暫存(添加到索引中)的文件:
- $ git add .
- $ git diff --cached </ol> </div>
- $ git diff --check
- $ git diff --check --cached </ol> </div> 來自:http://blog.csdn.net/iam333/article/details/45023681
3)mac
http://sourceforge.net/projects/git-osx-installer/,不僅能裝Git本身(選1.9.5或以上版本),還有GUI的安裝包
啟動git
1)windows
Windows:使用Windows自帶的命令行界面
可以在Windows自己的命令行界面下可以直接運行Git命令行,比如
當命令中有些特殊參數的時候,要加上雙引號。比如
特殊符號^會被Windows誤解,所以要加雙引號,寫成
Windows:使用msysGit自帶的Bash
使用Bash就不用像上面那樣加雙引號了。啟動Git Bash的簡便方法是,在Windows Explorer里,適當目錄的右鍵彈出菜單,Git Bash。此外,也可以從Windows開始菜單進入。
初次使用時,點擊界面右上角,在菜單中選擇“屬性”項,在彈出對話框中,勾選上“快速編輯模式”和“插入模式”,這樣將來copy paste比較方便。
注意,有利有弊,這個Bash對中文的支持不太好。
2)linux
設置git
不論Windows還是Linux還是Mac,建議至少config下述內容
Windows上還需要配置:
設置SSH
在Linux的命令行下,或Windos上Git Bash命令行窗口中(總之不要用iOS),鍵入:如果在Linux上,需要把其中的私鑰告訴本地系統:
再把其中公鑰的內容復制到GitLab上。具體方法是:
顯示ssh公鑰的內容:
打開github頁面:https://github.com/settings/profile,選擇SSH Keys,然后點擊Add SSH Key,把剛才ssh公鑰id_rsa.pub(windows下的用戶目錄找到.ssh文件夾進去就可以看到)的內容paste進去。不需要填title,title會自動生成。
注意:需要copy最開頭的“ssh-rsa ”這幾個字。
開始使用
1)創建新的git倉庫
2)使用已存在的git倉庫
注意,如果提示fatal: remote origin already exists.,那么說明該本地倉庫已經有遠端地址了。你可以先使用git remote rm origin刪除origin,或者使用git remote add other_name git@github.com:username/test.git來添加(提交時記得使用git push -u other_name master)。
3)一次提交到多個遠端倉庫
假設現有倉庫地址為: git@github.com:username/test.git
注意:在 Git 2.0 將會更改默認的push動作為【只 push 當前 branch 到遠端倉庫】。如果想繼續使用git push both命令需要手動設置一下git push的默認動作git config --global push.default matching。
push.default有幾個簡單動作,這里介紹matching和simple,二者意思分別是 push 本地所有的分支到遠端倉庫和 push 本地當前分支到上游分支。這個解釋貌似還不夠精確,可以man git-config來查看詳細說明。
4)在現有倉庫上創建孤兒分支
孤兒分支意思為該分支中沒有任何內容,與之前創建的其他分支沒有任何關聯。
5)提交單個分支到遠端git倉庫
git push命令默認是將所有分支(branch)都提交到git倉庫,有時你只想提交某個分支到遠端倉庫,那么就就需要使用git push origin HEAD。當然也可以使用git config --global push.default tracking命令來改變git push的默認操作,意思是執行git push時默認只提交當前分支到遠端git倉庫。
git常用指令
以下幾個是git常用的指令,可以簡單了解一下。
1)git config
在使用git前最好先配置一下你的個人信息及使用偏好。以下命令的意思就不用解釋了吧,執行完以下命令就會在你的家目錄(~)下生成一個文件~/.gitconfig。
2)git add
添加文件內容到索引中去(暫存文件),幾個簡單示例:
刪除索引和當時工作目錄中的文件。
將當前改動記錄到倉庫中,即提交改動到本地倉庫中。
突然你又不想git commit了,那么執行以下命令:
5)git status
查看當前工作目錄的狀態,即修改、添加及刪除了哪些文件。
檢出一個分支和目錄到當前工作目錄中,可以簡單理解為切換分支的命令。
以下命令分別為切換到分支 branch1 和創建一個新的分支 new_branch 。
列出、創建和刪除分支。
以下指令分別為列出本地分支、所有分支、遠端分支、創建、刪除、強制刪除分支。
如果出現沖突,那么手動解決沖突就可以了。
合并分支之后如果不再需要以前的分支了,那么可以在本地及遠程刪除它。
這條命令耐人尋味啊,其中origin是你的遠程倉庫名字(git remote -v可以查看到)。
10)git diff
查看改動內容。
View the redundant Tab or Space in your codes: