標籤:
轉自:http://blog.csdn.net/weihan1314/article/details/8677800
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
目錄(?)[+]
安裝Git
[plain] view plain copy
- $sudo apt-get install git
- $sudo apt-get install git-core
更新Git
[plain] view plain copy
- $git clone git://git.kernel.org/pub/scm/git/git.git
安裝好git後在終端輸入git 命令會顯示git命令提示,證明git已經安裝成功。
初始化代碼倉庫
[plain] view plain copy
- $mkdir android4.2
- $cd android4.2
- $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
- $cd .git
- $vim config
該設定檔的原始內容為:
[plain] view plain copy
- [core]
- repositoryformatversion = 0
- filemode = true
- bare = false
- logallrefupdates = true
在該設定檔中加入以下內容:
[plain] view plain copy
- [receive]
- 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
- $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
- $touch spec.txt
- $git add spec.txt
- $git commit-a -m "first commit"
- $git push
此時,基本的代碼倉庫已經建立完成。本地代碼倉庫的Git庫為:/data/Downloads/Git/android4.2/.git
同步代碼
建立myprojects目錄,同步代碼
[plain] view plain copy
- $mkdir Download/myprojects
- $git clone /data/Downloads/Git/android4.2/.git
修改檔案
[plain] view plain copy
- $touch test.txt
(備忘:在這裡可以編寫和變更檔,本文只是講解Git庫的使用,所以只是簡單的建立了一個檔案)
使用git diff 和 git status 命令可以查看代碼當前的狀態
提交代碼
[plain] view plain copy
- $git add test.text
- $git commit -a -m "second commit"
- $git push origin master
查看log資訊
[plain] view plain copy
- $git log
查看代碼倉庫中代碼是否提交成功
進入代碼倉庫目錄,查看代碼提交log
[plain] view plain copy
- $cd android4.2
- $git log
能夠查看代碼提交的log資訊,但是卻無法看到我們提交的代碼,怎麼回事呢?
如果使用了git init初始化,則遠程倉庫的目錄下,也包含work tree,當本地倉庫向遠程倉庫push時, 如果遠程倉庫正在push的分支上(如果當時不在push的分支,就沒有問題), 那麼push後的結果不會反應在work tree上, 也即在遠程倉庫的目錄下對應的檔案還是之前的內容。
使用以下命令即可查看提交的內容
$git reset --hard
回退到當前最新(本地最新)提交的代碼資訊
Git管理本地代碼(一)【轉】