1. 程式人生 > >ubuntu下安裝git安裝及使用

ubuntu下安裝git安裝及使用

一、當前git已經成成為程式碼管理最受歡迎的工具之一,下面簡單說一下其使用。之前寫過一篇關於git關聯github的,可以檢視http://fuzhenwen.top:8000/article/22/preview 。

   一般專案開發者,首先得把專案fork到自己的profile中,一般人沒有前線直接向origin直接提交程式碼,只能向自己的profile提交,確定要找到對應的dev分支,然後pull request,把連結發給merge程式碼的人。

二、安裝
        1.在控制視窗輸入,開始安裝。

[email protected]
:~$ sudo apk-get install git


        2.輸入自己的github郵箱,進行關聯。

  [email protected]:~$ git config --global user.email "[email protected]"


        3.檢視關聯是否成功(成功結果如下:)

 [email protected]:~$ git config --list

  user.name=Fuge2008
  
[email protected]


        4.生成公私鑰,生成檔案是控制是否能提交程式碼的身份依據,請妥善保管,如果之前有的話,會提示是否覆蓋,請留意。(下面是成功的結果)。

 [email protected]:~/workspace$ ssh-keygen -trsa -C "[email protected]"

        Generating public/private rsa key pair.
        Enter file in which to save the key (/home/fuge/.ssh/id_rsa):
        Enter passphrase (empty for no passphrase):
        Enter same passphrase again:
        Your identification has been saved in /home/fuge/.ssh/id_rsa.
        Your public key has been saved in /home/fuge/.ssh/id_rsa.pub.
        The key fingerprint is:
        SHA256:YyZa/ayb6ge1zfXXXXXXXXWtMtNdm4tzyqgvZc9GjSiSE 
[email protected]
The key's randomart image is: +---[RSA 2048]----+ | | | oES==.o o .| | o.*o%B.+....| | . .*o+B . + | | oo+=. . .| | .+o=+.oo. | +----[SHA256]-----+


        5.下面是即可檢視到公鑰的內容,把ssh-rsa後面的部分複製到github的Setting->SSH keys->New SSH Key,建立一個key,儲存即可。(下面是檢視id_rsa.pub的方法)

 [email protected]:~$ cd .ssh
        [email protected]:~/.ssh$ ls
        d_rsa  id_rsa.pub  known_hosts
        [email protected]:~/.ssh$ cat id_rsa.pub
        ssh-rsa  B3NzaC1yc2EAAAAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXtD [email protected]


      
     三、常用操作命令
        #克隆專案

 [email protected]:~/mnt/workspace/$ git clone [email protected]:fuzhenwen3/onyx.git

         #檢視看git狀態

 [email protected]:~/mnt/workspace/3128_android_rom/kepler$ git st

#檢視指定檔案的git操作日誌

[email protected]:~/mnt/workspace/3128_android_rom/kepler$ git log -p android/kreader/app/common/OnyxPinyinIME/

#檢視git分支

  [email protected]:~/mnt/workspace/3128_android_rom/kepler$ git br

    #檢視git所有分支

 [email protected]:~/mnt/workspace/3128_android_rom/kepler$ git br -a

   #檢視當前父子倉庫

 [email protected]:~/mnt/workspace/3128_android_rom/kepler$git sub

#檢視當前分支的全部子模組

[email protected]:~/mnt/workspace/3128_android_rom$ git submodule foreach git br

#切換到origin

[email protected]:~/mnt/workspace/3128_android_rom$ git remote add origin [email protected]:onyx-intl/3128_android_rom.git

#來取更新當前分支的子模組

[email protected]:~/mnt/workspace/3128_android_rom$ git submodule foreach git pull

#來取更新

[email protected]:~/mnt/workspace/3128_android_rom$ git submodule update 

#開始同步
 

[email protected]:~/mnt/workspace/3128_android_rom$ git submodule sync

#取消dev分支的左右修改

[email protected]:~/mnt/workspace/3128_android_rom$ git submodule foreach git co dev

 

        #git提交記錄
       

 [email protected]:~/mnt/workspace/3128_android_rom/kepler$ git log

        #git變化情況對比
      

  [email protected]:~/mnt/workspace/3128_android_rom$ git git diff

        #git某個檔案變化情況對比
  

[email protected]:~/mnt/workspace/3128_android_rom/kepler$ git diff android/kreader/app/common/OnyxPinyinIME/src/main/java/com/onyx/pinyinime/SoftKeyboardView.java

        #展示某條記錄詳情,show後面的是在diff中查到的id號
     

   [email protected]:~/mnt/workspace/3128_android_rom/kepler$ git show 87d65dd8e724a84fba05bede6d79016856fe7e2a

        #檢視程式碼情況,如果分支不對的話不對的話,需要add一下新的路徑
       

 [email protected]:~/mnt/workspace/3128_android_rom/kepler$ git remote -v

     #要新增一個新的遠端倉庫,可以指定一個簡單的名字(mine),以便將來引用,執行 git remote add [shortname] [url]:

    

 [email protected]:~/mnt/workspace/3128_android_rom/kepler$git remote add mine git://github.com/paulboone/ticgit.git

#此時用git remote -v檢視分支的話會出現新加進去的分支,如下
   

 origin git://github.com/schacon/ticgit.git

 mine git://github.com/paulboone/ticgit.git

#切換分支,由orgin切換到mine

[email protected]:~/workspace/3128_android_rom$git fetch origin mine:mine

        #將改動後的程式碼add進去
  

[email protected]:~/mnt/workspace/3128_android_rom/kepler$ git add android/kreader/app/common/OnyxPinyinIME/src/main/java/com/onyx/pinyinime/SoftKeyboardView.java

#將單個檔案add進去

[email protected]:~/mnt/workspace/3128_android_rom/frameworks$ git add base/packages/DocumentsUI/res/layout/fragment_directory.xml

#git co . # 指定取消某些檔案的改動

[email protected]:~/mnt/workspace/3128_android_rom/frameworks$ git co base/packages/DocumentsUI/res/values-sw720dp/styles.xml base/packages/DocumentsUI/res/values/styles.xml base/packages/SettingsProvider/res/values/defaults.xml

        #commit程式碼
       

 [email protected]:~/mnt/workspace/3128_android_rom/kepler$ git commit -m "Screen adaptation to the keyboard"

        #如果自己的是fork過來的分支,那push前要記得先向母專案pull,更新專案後再push
      

   [email protected]:~/mnt/workspace/3128_android_rom/kepler$ git pull origin dev

        #push程式碼
        

[email protected]:~/mnt/workspace/3128_android_rom/kepler$ git push mine dev