歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Git管理本地代碼

Git管理本地代碼

日期:2017/2/28 14:51:28   编辑:Linux教程

安裝Git

$sudo apt-get install git
$sudo apt-get install git-core

更新Git

$git clone git://git.kernel.org/pub/scm/git/git.git

安裝好git後在終端輸入git 命令會顯示git命令提示,證明git已經安裝成功。

初始化代碼倉庫

$mkdir Android4.2
$cd android4.2
$git init

提示: Initialized empty Git repository in /data/Downloads/Git/android4.2/.git/證明git倉庫(repository)創建成功
配置config文件(全局)

$cd .git
$vim config

該配置文件的原始內容為:

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true

在該配置文件中加入以下內容:

[receive]
denyCurrentBranch = ignore

加入該配置的目的是:允許使用git push 提交代碼到服務器,否則會出現以下異常:

加入該配置的目的是:允許使用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'

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

$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
因此我們需要在初始化代碼倉庫之後,在倉庫中創建一個文件:

實例:

$touch spec.txt
$git add spec.txt
$git commit-a -m "first commit"
$git push

此時,基本的代碼倉庫已經創建完成。本地代碼倉庫的Git庫為:/data/Downloads/Git/android4.2/.git

Copyright © Linux教程網 All Rights Reserved