git基本命令

生成自己key

1
$ ssh-keygen -t rsa -C "youremail@example.com"

当前目录下.ssh文件,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥。

版本回退

查看提交历史

1
$ git log

版本回退

1
$ git reset --hard 3628164

版本1,版本2,版本3(最新)
回退到版本2

1
$ git reset --hard HEAD^

找回最新版本3

1
2
$ git reflog
$ git reset --hard "版本号"

暂存区(stage)

工作区 –> 暂存区

1
$ git add .

暂存区 –> master

1
$ git commit -m "introduction"

至此暂存区无任何信息。

每次修改,如果不add到暂存区,那就不会加入到commit中。

撤销修改

当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时

1
$ git checkout -- file

当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步

1
2
$ git reset HEAD file
$ git reset checkout --file

删除文件

a.确实删除版本库文件

1
2
$ git rm file
$ git commit -m 'rm file'

b.误删了工作区文件

1
$ git checkout -- file

分支

1
2
3
4
5
6
7
8
9
10
11
查看分支:git branch

创建分支:git branch branchName

切换分支:git checkout branchName

创建+切换分支:git checkout -b branchName

合并某分支到当前分支:git merge branchName (快速合并)

删除分支:git branch -d branchName

一组测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ git checkout -b feature1
$ modify readme.txt (add a line)
$ git add & git commit -m '& simple'

$ git checkout master
$ modify readme.txt (add a line)
$ git add & git commit -m 'AND simple'

合并分支(当前分支master)
$ git merge
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

$ git status
$ modify readme.txt
$ git add & git commit

$ git log --graph --pretty=oneline --abbrev-commit
* 59bc1cb conflict fixed
|\
| * 75a857c AND simple
* | 400b400 & simple
|/
* fec145a branch test
...

分支管理策略

1
2
3
$ git merge feature1 (属于Fast forward 模式,但这种模式下,删除分支后,会丢掉分支信息)

$ git merge --no-ff -m "merge with no-ff" feature1

首先,master 分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;

那在哪干活呢?干活都在 dev 分支上,也就是说,dev 分支是不稳定的,到某个时候,比如 1.0 版本发布时,再把dev分支合并到master上,在master分支发布 1.0 版本;

你和你的小伙伴们每个人都在dev分支上干活,每个人都有自己的分支,时不时地往dev 分支上合并就可以了。

bug分支

临时要修复master 上的bug(issue-101)
当前在分支yang.zhang3工作

1
2
3
4
5
6
7
8
9
10
$ git stash(保护现场)

$ git checkout master
$ git checkout -b issue-101
$ modify
$ git checkout master
$ git merge --no-ff -m "repair issue-101" issue-101

$ git checkout yang.zhang3
$ git stash pop (恢复现场)

临时开发新功能,最终需要删除

1
2
3
$ git checkout -b newfeature
$ add..
$ git checkout master

不合并newfeature分支,需要删除

1
2
3
4
$ git branch -d newfeature
error: The branch 'newfeature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D newfeature'.
$ git branch -D newfeature

多人协作

1.查看远程库信息,使用git remote -v

2.本地新建的分支如果不推送到远程,对其他人就是不可见的;

3.从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;

4.在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;

5.建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name

6.从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。

如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name

日常工作

问题描述
服务器两个分支 4.19(1),4.19_yang(2)
本地两个分支4.19(3),4.19_yang(4)

若(1)有人更新了,而且(4)也新增了内容
执行代码顺序

1
2
3
4
$git checkout (3)
$git pull
$git checkout (4)
$git merge (3)

然后在提交。

1
2
3
$git add .
$git commit -m "comment"
$git push

若自己还没改完,可以在自己的分支先git stach
在git merge 完成以后 执行git stach pop

git fetch & git pull & git push

参考阮一峰

先看一张图,来看下git

一旦远程主机的版本库有了更新(Git术语叫做commit),需要将这些更新取回本地,这时就要用到git fetch命令。

1
$ git fetch <远程主机名> <分支名>

取回远程主机的更新以后,可以在它的基础上,使用git checkout命令创建一个新的分支。

1
$ git checkout -b newBrach origin/master

git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。它的完整格式稍稍有点复杂。

1
$ git pull <远程主机名> <远程分支名>:<本地分支名>

比如,取回origin主机的next分支,与本地的master分支合并,需要写成下面这样。

1
$ git pull origin next:master

在某些场合,Git会自动在本地分支与远程分支之间,建立一种追踪关系(tracking)。比如,在git clone的时候,所有本地分支默认与远程主机的同名分支,建立追踪关系,也就是说,本地的master分支自动”追踪”origin/master分支。

举例:本地master分支追踪origin/next分支。

1
$ git branch --set-upstream master origin/next

如果合并需要采用rebase模式,可以使用–rebase选项。

1
$ git pull --rebase <远程主机名> <远程分支名>:<本地分支名>

git push命令用于将本地分支的更新,推送到远程主机。它的格式与git pull命令相仿。

1
$ git push <远程主机名> <本地分支名>:<远程分支名>

0%