Git管理本地代码(一)【转】
转自:http://blog.csdn.net/weihan1314/article/details/8677800
版权声明:本文为博主原创文章,未经博主允许不得转载。
安装Git
- $sudo apt-get install git
- $sudo apt-get install git-core
- $git clone git://git.kernel.org/pub/scm/git/git.git
初始化代码仓库
- $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文件(全局)
- $cd .git
- $vim config
- [core]
- repositoryformatversion = 0
- filemode = true
- bare = false
- logallrefupdates = true
- [receive]
- denyCurrentBranch = ignore
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 git@192.168.1.X:/var/git.server/.../web
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@192.168.1.X:/var/git.server/.../web'
在代码仓库创建一个说明文档(该文档可以随便创建)
- $touch spec.txt
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
同步代码
创建myprojects目录,同步代码
- $mkdir Download/myprojects
- $git clone /data/Downloads/Git/android4.2/.git
- $touch test.txt
使用git diff 和 git status 命令可以查看代码当前的状态
提交代码
- $git add test.text
- $git commit -a -m "second commit"
- $git push origin master
- $git log
查看代码仓库中代码是否提交成功
进入代码仓库目录,查看代码提交log
- $cd android4.2
- $git log
如果使用了git init初始化,则远程仓库的目录下,也包含work tree,当本地仓库向远程仓库push时, 如果远程仓库正在push的分支上(如果当时不在push的分支,就没有问题), 那么push后的结果不会反应在work tree上, 也即在远程仓库的目录下对应的文件还是之前的内容。
使用以下命令即可查看提交的内容
$git reset --hard
回退到当前最新(本地最新)提交的代码信息