您的位置:首页 > 编程语言

虚拟项目学习git/github原理与基本操作5

2013-01-03 18:50 393 查看
首先查看一下我们当前项目的提交历史,可以使用git log
$ git log
watkins@watkins:~/watkins/finance$ git logcommit f2f51bd6eefdd115ec4be37540f97dfb81828ad1Author: weixingstudio <watkins.song@gmail.com>Date: Thu Jan 3 13:32:15 2013 +0800add function1commit 8c4472fc02544d491435de8e47bba2bff87209a8Author: weixingstudio <watkins.song@gmail.com>Date: Thu Jan 3 13:02:02 2013 +0800add configuration filecommit 79ec82e68f6366dab0cc2ec96bee0831cd877776Author: weixingstudio <watkins.song@gmail.com>Date: Wed Jan 2 21:33:24 2013 +0800my first commitwatkins@watkins:~/watkins/finance$可以看到现在有三次提交。至于git log的其他的带参数的详细用法,请自己参考其他资料或者手册。当然也可以使用git的图形工具gitk命令图形显示当前的版本提交信息。

修改最后一次提交

有时候我们提交完了才发现漏掉了几个文件没有加,或者提交信息写错了。想要撤消刚才的提交操作,可以使用 
--amend
 选项重新提交:
$ git commit --amend
此命令将使用当前的暂存区域快照提交。如果刚才提交完没有作任何改动,直接运行此命令的话,相当于有机会重新编辑提交说明,而所提交的文件快照和之前的一样。这个时候我们创建一个新的文件function2,但是我们又不想把这个文件的加入创建一个新的软件版本记录,只是想和上一次提交的时候的版本一样,不引入新的版本,就可以使用git commit --amend这个时候提交代码,就会提示你输入提交的说明,注意,这里的提交说明和上一次的是一样的,你只需要在这个基础上做修改,你修改的是上一次的提交,而不是新的提交。watkins@watkins:~/watkins/finance$ git push origin masterUsername for 'https://github.com': weixingstudioPassword for 'https://weixingstudio@github.com':To https://github.com/weixingstudio/finance.git ! [rejected] master -> master (non-fast-forward)error: failed to push some refs to 'https://github.com/weixingstudio/finance.git'To prevent you from losing history, non-fast-forward updates were rejectedMerge the remote changes (e.g. 'git pull') before pushing again. See the'Note about fast-forwards' section of 'git push --help' for details.watkins@watkins:~/watkins/finance$ git pull origin masterFrom https://github.com/weixingstudio/finance * branch master -> FETCH_HEADMerge made by the 'recursive' strategy.watkins@watkins:~/watkins/finance$ git push origin masterTo https://github.com/weixingstudio/finance.git f2f51bd..23293b0 master -> masterwatkins@watkins:~/watkins/finance$下面我们再次修改function1,function2两个文件,看看怎么取消文件的暂存和文件的修改。watkins@watkins:~/watkins/finance$ git status# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: funciton1# modified: funciton2#no changes added to commit (use "git add" and/or "git commit -a")watkins@watkins:~/watkins/finance$
watkins@watkins:~/watkins/finance$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   funciton1
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   funciton2
#
watkins@watkins:~/watkins/finance$
从上面中可以看到function1已经在暂存区中,根据提示取消暂存watkins@watkins:~/watkins/finance$ git reset HEAD funciton1Unstaged changes after reset:M funciton1M funciton2watkins@watkins:~/watkins/finance$如果觉得function2的修改没有必要,可以取消文件的修改,回到修改之前的状态。根据提示取消文件的修改watkins@watkins:~/watkins/finance$ git checkout -- funciton2然后查看文件,确实已经恢复到修改之前的代码样子。

远程仓库的使用

$ git remote -v
origin	git://github.com/schacon/ticgit.git
查看当前项目对应的远端仓库。watkins@watkins:~/watkins/finance$ git remote -vorigin https://github.com/weixingstudio/finance.git (fetch)origin https://github.com/weixingstudio/finance.git (push)watkins@watkins:~/watkins/finance$要添加一个新的远程仓库,可以指定一个简单的名字,以便将来引用,运行 
git remote add [shortname] [url]
:可以用下面的命令从远程仓库抓取数据到本地:
$ git fetch [remote-name]
此命令会到远程仓库中拉取所有你本地仓库中还没有的数据。运行完成后,你就可以在本地访问该远程仓库中的所有分支,将其中某个分支合并到本地,或者只是取出某个分支,一探究竟。(我们会在第三章详细讨论关于分支的概念和操作。)如果是克隆了一个仓库,此命令会自动将远程仓库归于 origin 名下。所以,
git fetch origin
 会抓取从你上次克隆以来别人上传到此远程仓库中的所有更新(或是上次 fetch 以来别人提交的更新)。有一点很重要,需要记住,fetch 命令只是将远端的数据拉到本地仓库,并不自动合并到当前工作分支,只有当你确实准备好了,才能手工合并。项目进行到一个阶段,要同别人分享目前的成果,可以将本地仓库中的数据推送到远程仓库。实现这个任务的命令很简单: 
git push [remote-name] [branch-name]
。如果要把本地的 master 分支推送到 
origin
 服务器上(再次说明下,克隆操作会自动使用默认的 master 和 origin 名字),可以运行下面的命令:
$ git push origin master
我们可以通过命令 
git remote show [remote-name]
 查看某个远程仓库的详细信息,比如要看所克隆的
origin
 仓库,可以运行:
$ git remote show origin* remote originURL: git://github.com/schacon/ticgit.gitRemote branch merged with 'git pull' while on branch mastermasterTracked remote branchesmasterticgit
在新版 Git 中可以用 
git remote rename
 命令修改某个远程仓库的简短名称,比如想把 
pb
 改成 
paul
,可以这么运行:
$ git remote rename pb paul$ git remoteoriginpaul
注意,对远程仓库的重命名,也会使对应的分支名称发生变化,原来的 
pb/master
 分支现在成了
paul/master
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: