您的位置:首页 > 其它

Git学习记录

2015-04-23 14:24 351 查看

简单使用Git

安装git

ubuntu/debian:sudo apt-get install git

fedora:sudo yum install git

设置

git config –global user.name “LiTao”

git config –global user.email “litao19900710@foxmail.com”

注意:git config命令的–global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址

创建版本库

[fedora@localhost LearnGit]$ git init

Initialized empty Git repository in /home/fedora/LearnGit/.git/

把文件添加到版本库

[fedora@localhost LearnGit]$ git add LearnGit

[fedora@localhost LearnGit]$ git commit -m “First commit”

[master (root-commit) 30f73bf] First commit
1 files changed, 2 insertions(+), 0 deletions(-) #

查看仓库状态

没有文件需要添加、提交

[fedora@localhost LearnGit]$ git status

# On branch master

nothing to commit (working directory clean)

有文件未添加

[fedora@localhost LearnGit]$ git status

# On branch master

# Changed but not updated:

# (use “git add …” to update what will be committed)

# (use “git checkout – …” to discard changes in working directory)

#

# modified: LearnGit

#

no changes added to commit (use “git add” and/or “git commit -a”)

文件添加了,但是未提交

[fedora@localhost LearnGit]$ git status

# On branch master

# Changes to be committed:

# (use “git reset HEAD …” to unstage)

#

# modified: LearnGit

#

查看修改的内容

[fedora@localhost LearnGit]$ git diff

diff –git a/LearnGit b/LearnGit

index 3c3d50f..7753484 100644

— a/LearnGit

+++ b/LearnGit

@@ -1,3 +1,4 @@

Git is a version control system.

Git is a free software.

Second modified.
+Third modified.

查看记录

[fedora@localhost LearnGit]$ git log

commit 752cac868287ed05dcd640cadfd8cb32be1b05de

Author: LiTao litao19900710@foxmail.com

Date: Thu Apr 23 13:41:01 2015 +0800

Third


commit 90303ff33fc51649e408656d9ffc75f51175e1c9

Author: LiTao litao19900710@foxmail.com

Date: Thu Apr 23 13:37:48 2015 +0800

Second modified.


commit 30f73bfde250c7bd608d360a5e6b3350badf378a

Author: LiTao litao19900710@foxmail.com

Date: Thu Apr 23 13:30:06 2015 +0800

First commit


也可以这样

[fedora@localhost LearnGit]$ git log –pretty=oneline

752cac868287ed05dcd640cadfd8cb32be1b05de Third
90303ff33fc51649e408656d9ffc75f51175e1c9 Second modified.
30f73bfde250c7bd608d360a5e6b3350badf378a First commit

版本退回

退到上一个版本

[fedora@localhostLearnGit]$ git reset –hard HEAD^

HEAD is now at 90303ff Second modified.
只要当前的命令行窗口没关闭,能找到之前的commit id,就可以返回

[fedora@localhostLearnGit]$ git reset –hard 752c

HEAD is now at 752cac8 Third

查看历史操作

[fedora@localhost LearnGit]$ git reflog

752cac8 HEAD@{0}: 752c: updating HEAD

90303ff HEAD@{1}: HEAD^: updating HEAD

752cac8 HEAD@{2}: commit: Third
90303ff HEAD@{3}: commit: Second modified.
30f73bf HEAD@{4}: commit (initial): First commit
记录了每一次的命令

撤销修改

放弃工作区里面的修改,回到最近一次add或者commit的状态

[fedora@localhost LearnGit]$ git checkout – LearnGit

放弃暂存区里面的修改,工作区里面的没影响,相当于没有执行上一次的add

[fedora@localhost LearnGit]$ git reset HEAD LearnGit

Unstaged changes after reset:

M LearnGit

删除文件

将文件rm了,git是知道的,git status会提示,如果真的要删掉(这里的文件是test),操作如下

[fedora@localhost LearnGit]$ git rm test

rm ‘test’

[fedora@localhost LearnGit]$ git commit -m “remove rm”

[master 038a693] remove rm

0 files changed, 0 insertions(+), 0 deletions(-)

delete mode 100644 test

恢复已经删掉的文件

版本库里面有,永远都不用担心找不回来

[fedora@localhost LearnGit]$ git checkout – test

将test恢复到版本库的最新版本,没有提交的修改是找不回来的

远程仓库

创建远程库第1步:

创建SSH Key。在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:

$ ssh-keygen -t rsa -C “litao19900710@foxmail.com”

如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

创建远程库第2步:

登陆GitHub,打开“Account settings”,“SSH Keys”页面;

然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容;

点“Add Key”,你就应该看到已经添加的Key;

也可以在http://git.oschina.net/托管代码

为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议,所以,GitHub只要知道了你的公钥,就可以确认只有你自己才能推送。

使用下面命令关联远程git库

$git remote add origin https://git.oschina.net/akuma/LearnGit.git

使用下面命令第一次推送master分支的所有内容

$ git push -u origin master

可能会报一个错误:

[fedora@localhost LearnGit]$ (gnome-ssh-askpass:1659): Gtk-WARNING **: cannot open display:

执行下面语句就可以推送到远程git仓库了:

$ unset SSH_ASKPASS

远程库克隆

远程已经有了一个GitSkill的仓库,将它克隆到本地

[fedora@localhost ~]$ git clone https://git.oschina.net/akuma/GitSkill.git

Cloning into GitSkill…

remote: Counting objects: 3, done.

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

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

也可以写成

$ git clone git@git.oschina:akuma/GitSkill.git

一个是https协议,一个是ssh协议

分支管理

创建分支,然后切换到分支

[fedora@localhost LearnGit]$ git checkout -b branch1

Switched to a new branch ‘branch1’

git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:

$ git branch branch1

$ git checkout branch1

Switched to branch ‘branch1’

查看分支

[fedora@localhost LearnGit]$ git branch

* branch1

master

列出所有分支,前面有*的是当前分支

合并分支

在branch1分支修改文件并提交,回到master分支,发现文件并没有修改,因为刚才的修改是提交到了branch1分支,此刻master分支并没有改变。

在branch1分支干完了活,可以合并到master分支了:

[fedora@localhost LearnGit]$ git merge branch1

Updating 813a60f..b59b4e3

Fast-forward

README.txt | 1 +

1 files changed, 1 insertions(+), 0 deletions(-)

git merge命令用于合并指定分支到当前分支。

删除分支

合并完了,可以放心删除分支了。。

[fedora@localhost LearnGit]$ git branch -d branch1

Deleted branch branch1 (was b59b4e3).

解决冲突

新建分支feature1,并转到分支,修改最后一行,然后回到master也修改最后一行,再合并,出现冲突

[fedora@localhost LearnGit]$ git merge feature1

Auto-merging README.txt

CONFLICT (content): Merge conflict in README.txt

Automatic merge failed; fix conflicts and then commit the result.

使用git log –graph可以看到分支合并图

分支管理策略

通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息。

如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。

下面我们实战一下–no-ff方式的git merge:创建并切换分支branch1,修改、提交后回到master分支,使用–no-ff参数合并分支。

[fedora@localhost LearnGit]$ git merge –no-ff -m “merge with no-ff” branch1

Merge made by recursive.

README.txt | 1 +

1 files changed, 1 insertions(+), 0 deletions(-)

看一下分支合并图

[fedora@localhost LearnGit]$ git log –graph –pretty=oneline –abbrev-commit

* b97d251 merge with no-ff

|\

| * dc4f3a4 merge –no-f

|/

* 4496ca1 conflict fixed

|\

| * 9a01fe9 modify in feature1

* | db1d984 modify in master

|/

* b59b4e3 branch1

* 813a60f first commit

Bug分支

修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场。

保护现场:

[fedora@localhost LearnGit]$ git stash

Saved working directory and index state WIP on branch1: dc4f3a4 merge –no-f

HEAD is now at dc4f3a4 merge –no-f

查看保护的现场:

[fedora@localhost LearnGit]$ git stash list

stash@{0}: WIP on branch1: dc4f3a4 merge –no-f

恢复现场:

有两个办法:一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;另一种方式是用git stash pop,恢复的同时把stash内容也删了。

[fedora@localhost LearnGit]$ git stash pop

# On branch branch1

# Changed but not updated:

# (use “git add …” to update what will be committed)

# (use “git checkout – …” to discard changes in working directory)

#

# modified: README.txt

#

no changes added to commit (use “git add” and/or “git commit -a”)

Dropped refs/stash@{0} (fcce4ab7266dabc5065ddd2b2da91f23f1ceeed1)

可以继续在branch1干活了

Feature分支

软件开发中,总有无穷无尽的新的功能要不断添加进来。添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支。

如果要放弃这个尚未合并的feature分支,就得使用以下命令:

[fedora@localhost LearnGit]$ git branch -D feature-xxx

Deleted branch feature-xxx (was babad21).

多人协作

查看远程仓库信息

git remote

git remote -v 显示更详细的信息

推送分支

$ git push origin master

master分支是主分支,因此要时刻与远程同步;

dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;

bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;

feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。

抓取分支

如果你的小伙伴的最新提交和你试图推送的提交有冲突,先用git pull把最新的提交从origin/dev抓下来,然后,在本地合并,解决冲突,再推送。

$ git pull

remote: Counting objects: 5, done.

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

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

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

From github.com:michaelliao/learngit

fc38031..291bea8 dev -> origin/dev

There is no tracking information for the current branch.

Please specify which branch you want to merge with.

See git-pull(1) for details

git pull <remote> <branch>


If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream dev origin/<branch>


如果git pull失败,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置dev和origin/dev的链接:

$ git branch –set-upstream dev origin/dev

Branch dev set up to track remote branch dev from origin.

再git pull,然后解决冲突,最后push

标签管理

创建标签

切换到需要打标签的分支上,敲命令git tag name 就可以打一个新标签。

可以用命令git tag查看所有标签。

默认标签是打在最新提交的commit上的。若果要想在之前的commit上打标签,就要找到历史commit id

$ git tag v0.9 6224937

注意,标签不是按时间顺序列出,而是按字母排序的。可以用git show tagname查看标签信息。

还可以创建带有说明的标签,用-a指定标签名,-m指定说明文字:

$ git tag -a v0.1 -m “version 0.1 released” 3628164

还可以通过-s用私钥签名一个标签:

$ git tag -s v0.2 -m “signed version 0.2 released” fec145a

签名采用PGP签名,因此,必须首先安装gpg(GnuPG),如果没有找到gpg,或者没有gpg密钥对,就会报错:

gpg: signing failed: secret key not available

error: gpg failed to sign the data

error: unable to sign the tag

如果报错,请参考GnuPG帮助文档配置Key。

用命令git show 可以看到PGP签名信息:

$ git show v0.2

tag v0.2

Tagger: Michael Liao askxuefeng@gmail.com

Date: Mon Aug 26 07:28:33 2013 +0800

signed version 0.2 released

—–BEGIN PGP SIGNATURE—–

Version: GnuPG v1.4.12 (Darwin)

iQEcBAABAgAGBQJSGpMhAAoJEPUxHyDAhBpT4QQIAKeHfR3bo…

—–END PGP SIGNATURE—–

commit fec145accd63cdc9ed95a2f557ea0658a2a6537f

Author: Michael Liao askxuefeng@gmail.com

Date: Thu Aug 22 10:37:30 2013 +0800

branch test




标签操作

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