您的位置:首页 > 其它

Git情景使用

2014-08-09 12:07 106 查看

Git情景使用

1:Git使用原理

git就是一个分布式的code管理工具



客户端并不只提取最新版本的文件快照,而是把代码仓库完整地镜像下来。这么一来,任何一处协同工作用的服务器发生故障,事后都可以用任何一个镜像出来的本地仓库恢复。因为每一次的提取操作,实际上都是一次对代码仓库的完整备份

参考书:
1:http://git-scm.com/book/zh
2:http://gitbook.liuhui998.com/index.html

2:Git使用操作

2.1 情景操作

一、git首先是得有个git仓库,获取仓库有两种
1:git init (新建仓库)
2:git clone git://github.com/schacon/grit.git(clone一个仓库)
Tips:,第一个平常自己管理代码很实用,第二个也就是从服务器拷贝一份完整的代码下来,然后在本地操作。

二、git branch相关的操作
1:git checkout -b remotes/origin/test-branch -b test-branch
检出远程的branch到本地并且生成一个本地的branch
2:git branch -a (显示所有branch,客户端和远程服务器的branch)
3:git branch -r (显示远程服务器的branch)
4:git diff test-branch remotes/origin/test-branch或者 git diff test-branch origin/test-branch(显示本地和远程branch里的区别)
5:git push origin master

origin指定了你要push到哪个remote

master其实是一个“refspec”,正常的“refspec”的形式为”+<src>:<dst>”,冒号前表示local branch的名字,冒号后表示remote repository下 branch的名字。注意,如果你省略了<dst>,git就认为你想push到remote repository下和local branch相同名字的branch。听起来有点拗口,再解释下,push是怎么个push法,就是把本地branch指向的commit push到remote repository下的branch,比如

6:git push origin master:master
(在local repository中找到名字为master的branch,使用它去更新remote repository下名字为master的branch,如果remote repository下不存在名字是master的branch,那么新建一个)

7:git push origin master (省略了<dst>,等价于“git push origin master:master”)

8:git push origin master:refs/for/test-branch (在local repository中找到名字为master的branch,用他去更新remote repository下面名字为test-branch的branch)

9:git push origin HEAD:refs/for/test-branch (HEAD指向当前工作的branch,master不一定指向当前工作的branch,所以我觉得用HEAD还比master好些)
Tips:ref/for代表的含义可以参考:http://stackoverflow.com/questions/10461214/why-do-git-push-gerrit-headrefs-for-master-instead-of-git-push-origin-master

10:git push origin :test-branch (再origin repository里面查找test-branch,删除它。用一个空的去更新它,就相当于删除了)

三、git查看类、检出类命令
1:git status(查看当前的状态,可以看到哪些文件修改了以及对应可以参考的命令)
2:git log(显示提交的log信息)
3:git show [commit-id] hello-world.c (可以查看提交的某次中的修改)
4:git relog (commt-id记录丢失了可用此命令首先找到最前面显示的ID,然后git cherry-pick即可找回(git cherry-pick用于把另一个本地分支的commit修改应用到当前分支))
5:git checkout [commit-id] hello-world.c (将code的版本恢复到某次提交的状态,如果通过amend修改的也可以通过git reflog来切换到某个版本)
6:git reset --hard [commit-id] (回退到commit-id的那次提交,并且源码也返回到commit-id的那次提交)
7:git reset --soft [commit-id] (回退了commit)
8:从服务器上拉下某个版本gerrit上有提示:git pull ssh://xxx.xxx.xx refs/change/xx/xx

四、git config相关
1:git remote -v 显示远程fetch和push的URL(如果不对可以使用,git config --global -e来进行修改)
2:

(要用到的到时候在更新吧)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: