转载请注明来自:Alex Zhou,本文链接:http://codingnow.cn/git/212.html
这一篇简单介绍一下git常用命令
1. 开始git
1.1 开始一个新的项目,初始化新的代码仓库
要对现有的某个项目开始用 Git 管理,只需到此项目所在的目录,执行:
git init
会在当前目录下生成一个.git目录,包含了所有git需要的数据和资源
1.2 把服务端的项目拷贝到本地
git支持许多传输协议,如http、https、git、ssh等,下面例子使用git协议
git clone git://github.com/andymccurdy/redis-py.git
2.跟踪文件
跟踪某个文件或者某个目录下所有文件,就是把需要跟踪的文件加入暂存区(stage),下面把当前目录下的所有文件加入到暂存区
git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm –cached …” to unstage)
#
#new file: Â c/file_operator/.file_cp.c.swp
#new file: c/file_operator/file_cp
#new file: c/file_operator/file_cp.c
#new file: c/file_operator/file_create
#new file: c/file_operator/file_create.c
#new file: c/file_operator/file_time
#new file: c/file_operator/file_time.cached可以看到,执行git add ./后,当前目录下这些文件已经被跟踪了,被添加到暂存区,同时(use “git rm –cached…” to unstage)提示你可以使用该命令取消跟踪。
注:如果你git add xx后又修改了xx文件,此时应该重新执行git add xx,把最新的xx文件添加到暂存区
4.取消跟踪文件
与跟踪文件相反,取消跟踪就是把该文件或目录从暂存区(stage)移除
git rm --cached c/file_operator/.file_cp.c.swp
ps:此时.file_cp.c.swp文件已经从暂存区域删除,但是仍在当前目录下,如果想把该文件也从工作目录中删除,执行
git rm c/file_operator/.file_cp.c.swp
git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm –cached …” to unstage)
#
#new file: c/file_operator/file_operatorle_cp
#new file: c/file_operator/file_cp.c
#new file: c/file_operatorle_cperator/file_create
#new file: c/file_operator/file_create.c
#new file_operatorile: c/file_operator/file_time
#new file: c/file_operator/file_timeime.c
# Untracked files:
# (use “git add …” to include in what will be committed)
#
#c/file_operator/.file_cp.c.swp
可以看到c/file_operatorrator/.file_cp.c.swp 已经不在stage区了。可以通过git add重新添加到stage区
5.忽略某些文件
在实际项目中,有些文件不需要使用git进行管理,比如:.pyc文件,.class文件,.o文件,.swp文件的等,我们可以在项目根目录下创建一个名为 .gitignore的文件,列出要忽略的文件。git默认会读取项目目录下的.gitignore文件(跟.git同目录)
vim .gitignore
#git进行管理时,忽略以下文件
c/file_operator/*.swp
*.o
所有空行或者以注释符号 # 开头的行都会被 Git 忽略,以上设置忽略所有.o文件,同时忽略c/file_operator/目录下的所有.swp文件
另外,还可以在配置项中通过core.excludesfile来指定ignore文件。
git config --system core.excludesfile ~/.gitignore_system
git config --global core.excludesfile ~/.gitignore_global
git config core.excludesfile .gitignore_local
注意:
git config –system 设置系统配置
git config –global 设置用户配置
git config 设置项目配置
6.提交更新
现在把暂存区域中的文件提交到仓库,每次准备提交之前,运行git status看看需要提交的文件是不是都已经暂存了。
git commit -m “注释”
[master (root-commit) 264c7c4] cc
59 files changed, 1266 insertions(+), 0 deletions(-)
create mode 100755 c/file_operator/file_cp
create mode 100755 c/file_operator/file_cp.c
create mode 100755 c/file_operator/file_create
create mode 100755 c/file_operator/file_create.c
…..
提交后它会告诉你,当前是在哪个分支(master)提交的,本次提交的完整 SHA-1 校验和是什么(264c7c4),以及在本次提交中,有多少文件修订过,多少行添改和删改过.
ps:在提交的时候,给git commit加上-a选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过git add步骤:git commit -a -m ‘xxxx’
7. 移动文件
git mv test1.c test2.c
相当于执行了
mv test1.c test2.c
git rm test1.c
git add test2.c
8.修改最后一次提交
有些时候我们执行git commit -m “cc” 执行后,发现还有文件没有添加到暂存区,想撤销刚才的提交操作,可以使用git commit –amend修改最后一次提交,重新提交,否则就需要多一次提交。
如:
git commit -m 'cc'
git add forgotten_file
git commit --amend
上面的三条命令最终只是产生一个提交,第二个提交命令修正了第一个的提交内容。
9.浏览更新历史
git log
commit 264c7c4c56aa398123015da290b7d710d6560cb4
Author: alexzhou
Date: Sun Jun 17 20:14:42 2012 +0800cc
git log可以指定输出格式,具体可以参考网上其它资料
如:列出user.py的所有改动历史,每条记录显示在一行
git log --pretty=oneline user.py
查看具体的某次改动
git show 哈希值
10. 恢复单个文件历史版本
查看文件历史记录:git log test.py
得到历史版本号,恢复该文件:
git reset 2e17053b4f5da3b378d6155a174523588d104338 test.py
11. 从服务端接收数据
git pull
12.推送数据到服务端
执行git commit后,只是把更新提交到本地仓库,执行git push后才把本地修改更新到服务端
git push
转载请注明来自:Alex Zhou,本文链接:http://codingnow.cn/git/212.html