您的位置:首页 > 其它

Git常用命令和操作

yandaqijian 2015-03-11 16:58 92 查看
1、设置忽略的文件

我们在开发过程中肯定有些文件是不想放在版本库上的,比如项目配置文件等。

设置忽略的文件的步骤如下:

1)在项目目录中创建.gitignore文件

2)将要忽略的文件名写入.gitignore

3)运行以下命令将改动加入版本库,而且由于要忽略的文件已经写入了.gitignore文件,所以用以下命令没有问题:

[plain] view
plaincopy

git add .

4)提交修改

[plain] view
plaincopy

caoxuezhu@CY6898 /d/work/tool/version control tool/git/HelloWorld2 (master)

$ git commit -m "test file ignore"

[master adb0aaa] test file ignore

1 file changed, 1 insertion(+)

create mode 100644 .gitignore

caoxuezhu@CY6898 /d/work/tool/version control tool/git/HelloWorld2 (master)

$ git push origin master

Username for 'https://github.com': caoxuezhu@cyou-inc.com

Password for 'https://caoxuezhu@cyou-inc.com@github.com':

Counting objects: 4, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 360 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To https://github.com/caoxuezhu/HelloWorld2.git
79aa7e0..adb0aaa master -> master

2、设置只有自己要忽略的文件

比如我们有一个文件叫file_my_exclude,这个文件只是在自己本地有,而且也不用传到服务器上,那么我们进行下面的操作就可以了:

1)编辑exclude文件:

[plain] view
plaincopy

caoxuezhu@CY6898 /d/work/tool/version control tool/git/HelloWorld2 (master)

$ vim .git/info/exclude

2)将要忽略的文件名写入文件:

[plain] view
plaincopy

# git ls-files --others --exclude-from=.git/info/exclude

# Lines that start with '#' are comments.

# For a project mostly in C, the following would be a good set of

# exclude patterns (uncomment them if you want to use them):

# *.[oa]

# *~

file_my_exclude

3)经过以上步骤就可以了,我们可以再测试一下,现在再提交就显示没有要添加的文件了:

[plain] view
plaincopy

caoxuezhu@CY6898 /d/work/tool/version control tool/git/HelloWorld2 (master)

$ git add .

caoxuezhu@CY6898 /d/work/tool/version control tool/git/HelloWorld2 (master)

$ git commit -m "test my exclude"

# On branch master

nothing to commit, working directory clean

3、增量提交

我要说明的是这个增量提交功能可能跟我们想象的不太一样,使用它其实和一次新的提交是差不多的,并不是不生成新的提交记录。具体操作如下:

1)比如我们在上一次提交中修改了file_4和file_3,但是忘了提交file_3了,那就可以用到增量提交:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git commit -C origin --amend

[FireElement cdc6e70] modify file_4

这时会提示你上一次提交时的comment,这次提交还是使用上次的comment,这一步等于把忘记提交的所有文件全都提交了。

2)切换到主干并和分支合并,这时会出一个界面让你输入merge的comment。后面大家可以看到,git会把这次merge记录也做为一次提交:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git checkout master

Switched to branch 'master'

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git merge FireElement

Merge made by the 'recursive' strategy.

file_3 | 1 +

1 file changed, 1 insertion(+)

3)将改动上传到服务器:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git push origin master

Username for 'https://github.com': pire_cao@163.com

Password for 'https://pire_cao@163.com@github.com':

Counting objects: 7, done.

Delta compression using up to 2 threads.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (4/4), 497 bytes | 0 bytes/s, done.

Total 4 (delta 1), reused 0 (delta 0)

To https://github.com/caoxuezhu/HelloWorld2.git
b246c7f..d951525 master -> master

4)我们会看到项目的提交记录里多了两次提交:



第一次提交记录是增量提交的结果,里两对file_4的修改是上次的提交,对file_3的修改是本次的增量提交



第二次提交记录是merge的记录



* 有一点需要注意的是不要增量提交之前提交过的文件,这样会造成冲突

4、回滚修改

说是回滚,但是这种回滚还是有痕迹的。它真实的意思是再生成一次提交,将之前的某次提交反向修改回来。

比如我们之前做过一次修改:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git log

commit 9b0df9edc4ba46fd705fb5b5b2265f192633cf27

Author: Xuezhu Cao <pire_cao@163.com>

Date: Wed Jul 31 00:24:22 2013 +0800

test revert

commit 02b58b57dbeeb3f10079d5a72aad50871319adda

Author: Xuezhu Cao <pire_cao@163.com>

Date: Tue Jul 30 19:08:29 2013 +0800

test reset 1

commit 1906203b3da2f3a0d4c4e31d8ecc2ab49609c477

Author: Xuezhu Cao <pire_cao@163.com>

Date: Tue Jul 30 19:00:40 2013 +0800

test reset

现在我想把commit 9b0df9edc4ba46fd705fb5b5b2265f192633cf27回滚掉,就用下面的命令:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git revert HEAD

[FireElement 24cc3e0] Revert "test revert"

1 file changed, 1 deletion(-)

执行上面这个命令时,会让你输入这出revert提交的注释,用它默认生成的即可。

执行完这个命令后,我们会发现多了一次提交:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git log

commit 24cc3e0a19c0d6c452f26bf7bae1d6b0870e22dd

Author: Xuezhu Cao <pire_cao@163.com>

Date: Wed Jul 31 00:27:32 2013 +0800

Revert "test revert"

This reverts commit 9b0df9edc4ba46fd705fb5b5b2265f192633cf27.

commit 9b0df9edc4ba46fd705fb5b5b2265f192633cf27

Author: Xuezhu Cao <pire_cao@163.com>

Date: Wed Jul 31 00:24:22 2013 +0800

test revert

commit 02b58b57dbeeb3f10079d5a72aad50871319adda

Author: Xuezhu Cao <pire_cao@163.com>

Date: Tue Jul 30 19:08:29 2013 +0800

test reset 1

接下来我们切换到主干,并把分支代码合并到主干,然后提交到服务器上:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git checkout master

Switched to branch 'master'

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git pull origin master

From https://github.com/caoxuezhu/HelloWorld2
* branch master -> FETCH_HEAD

Already up-to-date.

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git merge FireElement

Updating 02b58b5..24cc3e0

Fast-forward

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git push origin master

Username for 'https://github.com': pire_cao@163.com

Password for 'https://pire_cao@163.com@github.com':

Counting objects: 6, done.

Delta compression using up to 2 threads.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (4/4), 453 bytes | 0 bytes/s, done.

Total 4 (delta 1), reused 0 (delta 0)

To https://github.com/caoxuezhu/HelloWorld2.git
02b58b5..24cc3e0 master -> master

这时我们再登录到网站上查看提交记录,就会发现多了两条记录:



其中一条是对文件做的修改:



而另一条是回滚这次修改:



文件的最终结果也是没有这次修改:



5、撤销修改

撤销的概念和回滚不一样,它是取消一次 commit记录,当然可以加--soft和--hard等参数,默认是--mixed方式撤消

比如我对file_3和file_4都进行了修改且分两次进行了commit:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git add file_3

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git commit -m "mofidy file 3"

[FireElement 8ecfb82] mofidy file 3

1 file changed, 1 insertion(+)

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git add file_4

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git commit -m "modify file 4"

[FireElement 499b87b] modify file 4

1 file changed, 1 insertion(+)

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git log

commit 499b87b67d0ce711f3725e9a1586d17b6e4847d9

Author: Xuezhu Cao <pire_cao@163.com>

Date: Wed Jul 31 01:07:04 2013 +0800

modify file 4

commit 8ecfb825a701689de51910e6e9a64fa1865ffe40

Author: Xuezhu Cao <pire_cao@163.com>

Date: Wed Jul 31 01:06:45 2013 +0800

mofidy file 3

然后我想撤消息对file_4的那次修改,就可以用下面的命令:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git reset HEAD~

Unstaged changes after reset:

M file_4

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git log

commit 8ecfb825a701689de51910e6e9a64fa1865ffe40

Author: Xuezhu Cao <pire_cao@163.com>

Date: Wed Jul 31 01:06:45 2013 +0800

mofidy file 3

可以看到只剩下对file_3的那次修改了,不过这样也让修改过的file_4变为没有提交的状态。

当然你可以对file_4再做提交,或者是想要彻底放弃对file_4的修改,就可以用下面的命令(我还下倾向说恢复到与服务器相同的版本,当然你也可以恢复到其它版本):

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git checkout origin file_4

下面就可以将确定要修改的内容与主干合并并提交到服务器上了:

[plain] view
plaincopy

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git checkout master

Switched to branch 'master'

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git pull origin master

From https://github.com/caoxuezhu/HelloWorld2
* branch master -> FETCH_HEAD

Already up-to-date.

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git merge FireElement

Updating 24cc3e0..8ecfb82

Fast-forward

file_3 | 1 +

1 file changed, 1 insertion(+)

cxz@ubuntu:/data/work/tool/version control tool/git/HelloWorld2$ git push origin master

Username for 'https://github.com': pire_cao@163.com

Password for 'https://pire_cao@163.com@github.com':

Counting objects: 5, done.

Delta compression using up to 2 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done.

Total 3 (delta 1), reused 0 (delta 0)

To https://github.com/caoxuezhu/HelloWorld2.git
24cc3e0..8ecfb82 master -> master

这时到网站上去看也发现只有一次提交:



并且只有file_3的内容发生了变化:



file_4的内容没有发生改变:



好因呀,明天接着写吧,而且这篇博文也比较长了,我应该再启动一篇新的了。
标签: