十个基本的Git命令

来源:互联网 发布:腓骨肌腱滑脱 知乎 编辑:程序博客网 时间:2024/05/04 21:08

一、Git Tutorial : Starting with git using just 10 commands

http://blog.xkoder.com/2008/08/13/git-tutorial-starting-with-git-using-just-10-commands/

是一个面向individual developer的简单Git教程,讲解了Git的10个常用命令。

1、  创建一个新的Git Repository

        git init

        注意:在执行这个命令之前,需要cd到你的工程根目录下,因为该命令是在当前目录下建立一个空的repository。

2、  往Git Repository里添加文件

        git add .

         将把该目录(包括子目录)下的所有文件添加到Repository。删除文件用:git rm

3、Commit命令

         更改repository后,变更并不生效,需要执行commit命令才能使之生效。

        git commit –a –m “Commit Message”

         -a选项可以使commit之前无需再执行add步骤,-m选项可以为此次commit添加信息/注释。还原回commit之前的状态用:git reset。

4、查看commit记录

        git log

         注意:每次commit记录都会对应一个唯一的commit hash。Git就是用这个commit hash来标识commit记录的。Commit hash会在其他的指令里用到,如下面要介绍的git diff。

 

5、查看当前repository状态

        git status

         该命令会列出最后一次commit后所有被修改的文件。

6、比较commit间的不同之处

        git diff

         该命令会比较当前状态和最后一次commit,列出修改的地方。

        git diff commit_hash

         该命令将当前状态与指定的commit比较,其中commit_hash指‘4’中讲的commit hash。需要注意的是:commit_hash只要写为commit hash的前8个字节就可以了。

         如何读懂git diff的内容?可参看 :http://www.ruanyifeng.com/blog/2012/08/how_to_read_diff.html  合并格式的diff。

7、创建分支

        git branch branch_name

8、Moving to a branch and listing all branches

        git checkout branch_name

         Moving到branch_name分支。Git Bash在repository的目录后面后指出现在所在的branch。

        git branch

         该命令会列出所有的branch。

9、合并两个分支

        git merge branch_name

         该命令将branch_name分支合并到当前分支。

         删除分支用:git branch –d branch_name

10、从repository里删除文件

        git rm --cached path/to/the/folder_or_file

         只从当前repository里删除该文件,并不会删除磁盘上的文件,之前的repository仍然包含该文件。

 

11、打开graphical repository viewer

         Windows下用:gitk

         Ubuntu下需安装git-gui: apt-get install git-gui,然后用:git-gui


Git常用命令速查

http://blog.csdn.net/ithomer/article/details/7529841


0 0