Git管理本地代碼(一)【轉】

來源:互聯網
上載者:User

標籤:

轉自:http://blog.csdn.net/weihan1314/article/details/8677800

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

 

目錄(?)[+]

 安裝Git

 

[plain] view plain copy 
  1. $sudo apt-get install git  
  2. $sudo apt-get install git-core  
更新Git

 

 

 

 

[plain] view plain copy 
  1. $git clone git://git.kernel.org/pub/scm/git/git.git  
安裝好git後在終端輸入git 命令會顯示git命令提示,證明git已經安裝成功。

 

 

 

初始化代碼倉庫

 

[plain] view plain copy 
  1. $mkdir android4.2  
  2. $cd android4.2  
  3. $git init  

 

 

 

git init 和git --bare init 的具體區別參考:http://blog.haohtml.com/archives/12265 

 

提示: Initialized empty Git repository in /data/Downloads/Git/android4.2/.git/證明git倉庫(repository)建立成功配置config檔案(全域)

 

[plain] view plain copy 
  1. $cd .git  
  2. $vim config  
該設定檔的原始內容為:

 

 

 

 

[plain] view plain copy 
  1. [core]  
  2.         repositoryformatversion = 0  
  3.         filemode = true  
  4.         bare = false  
  5.         logallrefupdates = true  
在該設定檔中加入以下內容:

 

 

 

 

[plain] view plain copy 
  1. [receive]  
  2. denyCurrentBranch = ignore  
加入該配置的目的是:允許使用git push 提交代碼到伺服器,否則會出現以下異常:

 

 

 

 

remote: error: refusing to update checked out branch: refs/heads/master

remote: error: By default, updating the current branch in a non-bare repository

remote: error: is denied, because it will make the index and work tree inconsistent

remote: error: with what you pushed, and will require ‘git reset --hard‘ to match

remote: error: the work tree to HEAD.

remote: error:

remote: error: You can set ‘receive.denyCurrentBranch‘ configuration variable to

remote: error: ‘ignore‘ or ‘warn‘ in the remote repository to allow pushing into

remote: error: its current branch; however, this is not recommended unless you

remote: error: arranged to update its work tree to match what you pushed in some

remote: error: other way.

remote: error:

remote: error: To squelch this message and still keep the default behaviour, set

remote: error: ‘receive.denyCurrentBranch‘ configuration variable to ‘refuse‘.

To [email protected]:/var/git.server/.../web

! [remote rejected] master -> master (branch is currently checked out)

error: failed to push some refs to ‘[email protected]:/var/git.server/.../web‘

在代碼倉庫建立一個說明文檔(該文檔可以隨便建立)

 

[plain] view plain copy 
  1. $touch spec.txt  
備忘:如果初始的代碼倉庫為空白,git push origin master提交代碼的時候會出現以下異常:

 

 

 

error: src refspec master does not match any.
error: failed to push some refs to ‘/data/Downloads/Git/android4.2/.git
因此我們需要在初始化代碼倉庫之後,在倉庫中建立一個檔案:

執行個體:

 

[plain] view plain copy 
  1. $touch spec.txt  
  2. $git add spec.txt  
  3. $git commit-a -m "first commit"  
  4. $git push  
此時,基本的代碼倉庫已經建立完成。本地代碼倉庫的Git庫為:/data/Downloads/Git/android4.2/.git

 

 

 

同步代碼

建立myprojects目錄,同步代碼

 

[plain] view plain copy 
  1. $mkdir Download/myprojects  
  2. $git clone /data/Downloads/Git/android4.2/.git  
修改檔案
[plain] view plain copy 
  1. $touch test.txt  
(備忘:在這裡可以編寫和變更檔,本文只是講解Git庫的使用,所以只是簡單的建立了一個檔案)

 

 

 

使用git diff 和 git status 命令可以查看代碼當前的狀態

提交代碼

 

[plain] view plain copy 
  1. $git add test.text  
  2. $git commit -a -m "second commit"  
  3. $git push origin master  
查看log資訊

 

 

 

 

[plain] view plain copy 
  1. $git log  

 

 

 

查看代碼倉庫中代碼是否提交成功

進入代碼倉庫目錄,查看代碼提交log

 

[plain] view plain copy 
  1. $cd android4.2  
  2. $git log  
能夠查看代碼提交的log資訊,但是卻無法看到我們提交的代碼,怎麼回事呢?

 

 

 

如果使用了git init初始化,則遠程倉庫的目錄下,也包含work tree,當本地倉庫向遠程倉庫push時, 如果遠程倉庫正在push的分支上(如果當時不在push的分支,就沒有問題), 那麼push後的結果不會反應在work tree上,  也即在遠程倉庫的目錄下對應的檔案還是之前的內容。

使用以下命令即可查看提交的內容
$git reset --hard

回退到當前最新(本地最新)提交的代碼資訊

Git管理本地代碼(一)【轉】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.