Git安裝配置及基本使用

pdkie1 9年前發布 | 259K 次閱讀 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或更高。若沒安裝或版本太低:

  1. $ sudo apt-get install git-core git-gui git-doc gitk  
  2. </ol> </div>

    3)mac

    http://sourceforge.net/projects/git-osx-installer/,不僅能裝Git本身(選1.9.5或以上版本),還有GUI的安裝包

    啟動git

    1)windows

    Windows:使用Windows自帶的命令行界面

    可以在Windows自己的命令行界面下可以直接運行Git命令行,比如

    1. D:/test> git help  
    2. </ol> </div>

      當命令中有些特殊參數的時候,要加上雙引號。比如

      1. D:/test> git log HEAD^  
      2. </ol> </div>
        特殊符號^會被Windows誤解,所以要加雙引號,寫成

        1. D:/test> git log "HEAD^"  
        2. </ol> </div>
          Windows:使用msysGit自帶的Bash

          使用Bash就不用像上面那樣加雙引號了。啟動Git Bash的簡便方法是,在Windows Explorer里,適當目錄的右鍵彈出菜單,Git Bash。此外,也可以從Windows開始菜單進入。

          初次使用時,點擊界面右上角,在菜單中選擇“屬性”項,在彈出對話框中,勾選上“快速編輯模式”和“插入模式”,這樣將來copy paste比較方便。

          注意,有利有弊,這個Bash對中文的支持不太好。

          2)linux

          1. $ git help  
          2. </ol> </div>

            設置git

            不論Windows還是Linux還是Mac,建議至少config下述內容

            1. git config --global user.name "test"                  # 請換成你自己的名字,除非你湊巧也叫wukong.sun  
            2. git config --global user.email "test@163.com"         # 同上  
            3. git config --global push.default simple               # 要是你非要用低版本的Git(比如1.7.x),好吧,那就不設simple設current,否則你的Git不支持  
            4. git config --global core.autocrlf false               # 讓Git不要管Windows/Unix換行符轉換的事  
            5. git config --global gui.encoding utf-8                # 避免git gui中的中文亂碼  
            6. git config --global core.quotepath off                # 避免git status顯示的中文文件名亂碼  
            7. </ol> </div> 其中最后兩個配置是關于中文亂碼的,基本夠用了。

              Windows上還需要配置:

              1. git config --global core.ignorecase false  
              2. </ol> </div>

                設置SSH

                在Linux的命令行下,或Windos上Git Bash命令行窗口中(總之不要用iOS),鍵入:

                1. $ssh-keygen -t rsa -C "test@163.com"  
                2. </ol> </div> 然后一路回車,不要輸入任何密碼之類,生成ssh key pair。然后就生成一個目錄.ssh ,里面有兩個文件:id_rsa , id_rsa.pub

                  如果在Linux上,需要把其中的私鑰告訴本地系統:

                  1. $ ssh-add ~/.ssh/id_rsa  
                  2. </ol> </div>
                    再把其中公鑰的內容復制到GitLab上。具體方法是:

                    顯示ssh公鑰的內容:

                    1. $ cat ~/.ssh/id_rsa.pub  
                    2. </ol> </div>

                      打開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倉庫

                      1. $ mkdir git_repo  
                      2. $ cd git_repo  
                      3. $ git init  
                      4. $ echo "test" > README.mkd  
                      5. $ git add README.mkd  
                      6. $ git commit -m "add README.mkd file"  
                      7. $ git remote add origin git@github.com:username/test.git  
                      8. $ git push -u origin master  
                      9. </ol> </div>
                        2)使用已存在的git倉庫

                        1. $ cd git_repo  
                        2. $ git remote add origin git@github.com:username/test.git  
                        3. $ git push -u origin master  
                        4. </ol> </div>
                          注意,如果提示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

                          1. $ git clone git@github.com:username/test.git  
                          2. $ cd test  
                          3. $ vim .git/config  
                          4. [core]  
                          5.     repositoryformatversion = 0  
                          6.     filemode = true  
                          7.     bare = false  
                          8.     logallrefupdates = true  
                          9. [remote "origin"]  
                          10.     url = git@github.com:username/test.git  
                          11.     url = git@gitshell.com:username/test.git  
                          12.     url = git@bitbucket.org:username/test.git  
                          13.     fetch = +refs/heads/*:refs/remotes/origin/*  
                          14. [branch "master"]  
                          15.     remote = origin  
                          16.     merge = refs/heads/master  
                          17. </ol> </div> 然后第一次提交時需要執行git push -u origin master,再往后就只需要執行git push就能把修改提交到上述三個遠端倉庫了。

                            注意:在 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)在現有倉庫上創建孤兒分支

                            孤兒分支意思為該分支中沒有任何內容,與之前創建的其他分支沒有任何關聯。

                            1. $ git clone git@github.com:username/test.git  
                            2. $ cd test  
                            3. $ git checkout --orphan new_branch  
                            4. Switched to a new branch 'new_branch'  
                            5. $ git rm -rf . # 刪除舊工作目錄樹中所有文件  
                            6. $ rm .gitignore # 如果有該文件的話就刪除  
                            7. $ echo "orphan branch" > README.mkd  
                            8. $ git add .  
                            9. $ git commit -m "add README.mkd file"  
                            10. $ git push origin new_branch  
                            11. </ol> </div>

                              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。

                              1. $ git config --global user.name "username"  
                              2. $ git config --global user.email test@163.com  
                              3. $ git config --global core.editor vim  
                              4. $ git config --global merge.tool vimdiff  
                              5. $ git config --global color.status auto  
                              6. $ git config --global color.branch auto  
                              7. $ git config --global color.interactive auto  
                              8. $ git config --global color.diff auto  
                              9. $ git config --global push.default simple  
                              10. $ git config --global alias.co checkout  
                              11. $ git config --global alias.ci commit  
                              12. $ git config --global alias.st status  
                              13. $ git config --global alias.last 'log -1 HEAD'  
                              14. $ git config --global alias.unstage 'reset HEAD --'  
                              15. </ol> </div>

                                2)git add

                                添加文件內容到索引中去(暫存文件),幾個簡單示例:

                                1. $ git add .  
                                2. $ git add --all  
                                3. $ git add *.txt  
                                4. $ git add directory/*.sh  
                                5. </ol> </div> 突然你又不想git add了,那么執行以下命令:

                                  1. $ git reset .  
                                  2. $ git reset *.txt  
                                  3. $ git reset directory/*.sh  
                                  4. </ol> </div> 3)git rm

                                    刪除索引和當時工作目錄中的文件。

                                    1. $ git rm filename  
                                    2. $ git rm -f *.txt  
                                    3. $ git rm -r .  
                                    4. </ol> </div> 4)git commit

                                      將當前改動記錄到倉庫中,即提交改動到本地倉庫中。

                                      1. $ git commit -m "add a file and remove a file"  
                                      2. </ol> </div>

                                        突然你又不想git commit了,那么執行以下命令:

                                        1. $ git reset HEAD^  
                                        2. </ol> </div> 你commit之后發現少添加了一個文件:

                                          1. $ git commit -m'msg'  
                                          2. $ git add forget_file  
                                          3. $ git commit --amend  
                                          4. </ol> </div> 你的 commit 已經 push 到遠程分支(master)了,現在你想反悔了:

                                            1. $ git clone git@github.com:username/test.git  
                                            2. $ cd test  
                                            3. $ git reset HEAD^  
                                            4. $ git push -f master   
                                            5. </ol> </div>

                                              5)git status

                                              查看當前工作目錄的狀態,即修改、添加及刪除了哪些文件。

                                              1. $ git status  
                                              2. </ol> </div> 6)git checkout

                                                檢出一個分支和目錄到當前工作目錄中,可以簡單理解為切換分支的命令。

                                                以下命令分別為切換到分支 branch1 和創建一個新的分支 new_branch 。

                                                1. $ git checkout branch1  
                                                2. $ git checkout -b new_branch  
                                                3. </ol> </div> 取消本地改動:

                                                  1. $ git checkout -- file_name  
                                                  2. </ol> </div> 7)git branch

                                                    列出、創建和刪除分支。

                                                    以下指令分別為列出本地分支、所有分支、遠端分支、創建、刪除、強制刪除分支。

                                                    1. $ git branch --list  
                                                    2. $ git branch --all  
                                                    3. $ git branch --remotes  
                                                    4. $ git branch new_branch  
                                                    5. $ git branch --delete branch_name  
                                                    6. $ git branch -D branch_name  
                                                    7. </ol> </div> 刪除remote tracking branch,就是git branch -r命令列出的分支。

                                                      1. $ git branch -r  
                                                      2. $ git branch -d -r origin/develop  
                                                      3. </ol> </div> 8)合并分支

                                                        如果出現沖突,那么手動解決沖突就可以了。

                                                        1. $ git checkout branch_name  
                                                        2. $ git checkout master  
                                                        3. $ git merge branch_name  
                                                        4. </ol> </div> 9)刪除遠程分支

                                                          合并分支之后如果不再需要以前的分支了,那么可以在本地及遠程刪除它。

                                                          1. $ git branch -d branch_name  
                                                          2. $ git branch -D branch_name  
                                                          3. $ git push origin :branch_name  
                                                          4. </ol> </div>

                                                            這條命令耐人尋味啊,其中origin是你的遠程倉庫名字(git remote -v可以查看到)。

                                                            10)git diff

                                                            查看改動內容。

                                                            1. $ git diff filename  
                                                            2. $ git diff .  
                                                            3. $ git diff revision1 revision2  
                                                            4. $ git diff branch1 branch2  
                                                            5. </ol> </div> DIFF暫存(添加到索引中)的文件:

                                                              1. $ git add .  
                                                              2. $ git diff --cached  
                                                              3. </ol> </div>

                                                                View the redundant Tab or Space in your codes:

                                                                1. $ git diff --check  
                                                                2. $ git diff --check --cached  
                                                                3. </ol> </div> 來自:http://blog.csdn.net/iam333/article/details/45023681

                                                                 本文由用戶 pdkie1 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
                                                                 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
                                                                 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!