如今不少公司都用git來管理代碼,老一些的項目可能還在用svn,git比svn好的地方就在於其便利的分支管理功能,特別適用於多人協做開發,當年祖師爺linus開發git就是爲了方便Linux操做系統的開發。git
git的基本用法很簡單: 拉代碼、提交變動、推代碼!大部分公司都有本身內部的git服務器,通常都是使用gitlab,主要是安全和省錢,固然也有公司直接使用github的付費服務!無論咋樣,你都須要拿到一個項目的git地址, 爲了方便演示,我在github上面建立了一個演示的倉庫,裏面目前只有一個README.md文件:github
首先,你須要使用 git clone 拷貝一份項目代碼到你本身的電腦,這個命令很簡單就很少說了!shell
jwang@jwang:~$ git clone https://github.com/wangbjun/git_demo.git
Cloning into 'git_demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.
複製代碼
前面那步clone代碼到本地以後那就能夠寫你本身的代碼了,不過在你提交代碼前我強烈建議你先更新一下代碼!並且每次開始寫代碼以前最好都先pull一下,這樣能夠減小衝突,就算有衝突也能夠提早發現解決!vim
有些人長時間不pull,到最後過了不少天提交的時候一大堆衝突,根本無法merge,很坑,因此我建議你們有空就pull,絕對是沒毛病的!安全
改完以後固然要提交代碼了,使用 git status 能夠顯示有哪些文件有修改!bash
jwang@jwang:~/git_demo$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
複製代碼
若是你改動了多個文件可是你只想提交其中的某幾個文件,你就須要使用 git add 命令添加改動的文件,在這個例子裏面,就是 git add READEM.md
。服務器
若是你不想提交改動的文件,並且想撤銷以前本身的更改,那你就可使用 git checkout 命令, 在這個例子裏面,就是 git checkout READEM.md
。eclipse
這是緊接着第4步的,假設你已經使用 git add 命令添加了本身須要提交的文件,這時候就須要使用 git commit 來提交本身的修改,一般執行這個命令會彈出一個對話框讓你添加提交信息,提交信息就是相對於一個備註吧! 編輯器
在Linux下面默認使用的是nano編輯器,不少人看到這個對話框會很懵,不知道咋用,這和vim的操做徹底不同,但也不難,直接輸入你想寫的內容,而後按 Ctrl+X 就會彈出一個選項,按 Y,最後回車就能夠了ide
若是你實在不習慣這個編輯器,能夠更改爲vim,使用 git config --global core.editor vim
命令,若是你連vim都不會用。。。我建議你能夠不用看下去了,下載一個圖形化界面的工具吧,或者使用IDE也行,好比idea,eclipse都有自帶git插件可使用。
有一個小操做,假如你修改了不少文件,並且都須要提交,你就沒必要一個個 git add,跳過第4步,直接使用 git commit -a
便可。
最後一步,若是你只需本地使用git,這步就不須要了,可是大部分時候咱們須要把本身的修改提交到遠程倉庫,讓別人也能拉取看到,這時候咱們就須要使用 git push
命令推代碼。
jwang@jwang:~/git_demo$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
複製代碼
請注意上面一些提示,其大概意思是自從 git 2.0版本開始,默認使用 "simple" 模式提交代碼,simple模式是隻會把代碼提交到你 git pull 命令拉取代碼的分支。其實意思就是你從哪一個分支拉取的代碼就會默認push到哪一個分支,通常狀況下咱們不須要更改這個。
其實最經常使用的也就是這幾個命令,git clone 只須要最開始執行一次,平時用的最多的就是 git commit 和 git push,只要掌握這幾個命令就能夠了。
當你使用IDE或者一些圖形化界面工具時更簡單,好比我經常使用的PHPStorm (idea全家桶快捷鍵都同樣), 快捷鍵 Ctrl+T 就是pull,Ctrl+K 能夠列出全部修改文件,默認勾選全部修改過的文件,填一下提交信息,回車就是commit了。而後 Ctrl+Shift+K 就是push代碼,若是不須要修改默認設置,直接回車就行,熟練操做的話很是方便,比使用命令行的效率高不少。
使用IDE還能夠很是方便的查看歷史記錄、reset代碼、合併分支、對比代碼,可是命令行也是須要掌握的,畢竟有時候在服務器上面可木有圖形化界面工具。。。
接下來,我會繼續給你們講講git分支相關的操做!