代码版本管理

  • svn

    集中式代码版本管理工具,它有一个中心库。我们在修改之前需要先进行代码迁出,改完之后在迁出

  • git

    分布式代码版本管理工具。任何一个人的代码仓库都可以作为一个中心库再分享给别人。

    本地分三块内容

    • 工作区

      我们平时写代码改代码的地方

    • 缓存区

      git add 就会把修改的代码加入缓存区

    • 本地仓库

      git commit 就会把代码提交到本地仓库

    还有一个远程仓库

    git push 把本地代码推送到远程

    git pull 拉取远程仓库代码到本地,git fetch+git merge

    git fetch 只拉取,不合并

初始化一个仓库

1
git init

克隆

默认克隆master分支

1
git clone <远程仓库地址>

克隆指定分支

1
git clone -b <远程分支名> <仓库地址>

代码提交流程

创建一个新的分支并且切换过去

1
git checkout -b <分支名>

将修改的代码加入本地暂存区

1
git add .

提交缓存区的代码到本地仓库

1
git commit -m '日志'

拉取远程仓库最新代码到本地

1
git pull

全名:

git pull origin master:master

git pull 远程仓库 远程仓库的分支名:本地仓库的分支名

推送本地代码到远程仓库

1.推送本地代码到远程

1
git push

全名:

git push origin master:master

git push 远程仓库 本地分支名字:远程分支名字

2.推送新分支到远程

1
git push origin <本地分支名>:<远程分支名>

合并到 master 操作流程

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

git checkout feature/1.0 ----------切换到feature/1.0分支

git rebase master ----------更新最新master信息 将head更新到最新状态

git checkout master ----------切换到master分支

git merge --no-ff feature/1.0 ----------合并feature/1.0分支到当前分支

<--no-ff:不使用fast-forward方式合并,保留分支的commit历史>

常用命令总结

1
git checkout -b 分支名 # 创建一个新的分支并且切换过去
1
git branch # 列出当前的所有分支
1
git branch # 查看分支
1
git branch -d 分支名 # 删除指定的分支
1
git merge 分支名 # 合并分支,合并指定分支名的分支到当前分支
1
git reflog # 查看所有的日志,包含已经回退和删除的
1
git log # 查看本地日志
1
git reset --hard 78cb76c5437cd4c11d17a4a595f1b045c0307bc4 # 回退代码到指定的位置
1
git push -f # 强制推送到远程仓库
1
git revert 版本号 # 远端倒退到指定版本号,会将代码反执行一次
1
git rebase # 变基

打标签:

在版本管理中,我们想要退回一个历史版本,要使用git reset --hard <版本编号/^HEAD>指令,使用指针^HEAD太麻烦,使用版本编号版本编号不容易记住,有没有好点的办法,更容易对各个版本的管理呢,办法当然有,那就是使用标签,创建标签很简单,使用git tag 命令就可以:

1
2
3
$ git branch
* master
$ git tag v1.0

这样就在master分支上创建了一个标签,标签名字v1.0,通过git tag可以查看创建的所有标签

1
2
$ git tag
v1.0

通过git log --pretty=oneline --abbrev-commit命令可以查看所有的历史版本。

1
2
3
4
5
6
7
$ git log --pretty=oneline --abbrev-commit

18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt

现在我们看看打标签有什么好处,我们先回到以前的历史版本_561ca86_

1
2
3
$ git reset --hard 561ca86

HEAD is now at 561ca86 add file test.txt 12

现在看看历史版本,已经回到了版本_561ca86_

1
2
3
4
5
6
$ git log --pretty=oneline --abbrev-commit

561ca86 (HEAD -> master, origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt

现在要想回到_v1.0_版本,就不用在记住原来的_commit_编号了,只要记着标签就行了:

1
2
3
4
5
6
7
8
9
10
$ git reset --hard v1.0

HEAD is now at 18233d5 test file add new

$ git log --pretty=oneline --abbrev-commit
18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt

版本又回到了_v1.0_,是不是很方便,这样只要给每个历史版本打上标签,就可以不用记_commit_编号就可以对各个历史版本进行方便的管理。

如果忘记了打标签,可以给历史版本打上标签,给历史版本打上标签的方法是使用命令_git tag <tag_name> <commit_id>_

1
2
3
4
5
6
7
8
9
$ git tag v0.9 561ca86

$ git log --pretty=oneline --abbrev-commit

18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (tag: v0.9, origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt

这样就给版本_561ca86_版本加上标_v0.9_
_还可以创建带说明的标签,使用命令 __git tag -a <tag_name> -m <“说明文字”> <commit_id>_

1
2
3
4
5
6
7
8
9
$ git tag -a v0.1 -m "this is version v0.1" 70e2010

$ git log --pretty=oneline --abbrev-commit

18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (tag: v0.9, origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt

使用_git show <tag_name>_可以看到文字说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ git show v0.1

tag v0.1
Tagger: ioolm <ioolm@qq.com>
Date: Sat Mar 23 21:21:01 2020 +0800

this is version v0.1

commit 70e2010d0a2b1ccd9d7734d5c2219f770c219588 (tag: v0.1)
Author: ioolm <ioolm@qq.com>
Date: Wed Mar 6 14:38:49 2020 +0800

wrote a readme file

diff --git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..4db76c8
--- /dev/null
+++ b/readme.txt
@@ -0,0 +1,2 @@
+Git is a version control system.
+Git is a free software.
\ No newline at end of file

现在我创建了一个标签,突然发现创建错了,怎么办,这时可以删除比如我们将v0.9标签删除:

1
2
3
$ git tag -d v0.9

Deleted tag 'v0.9' (was 561ca86)

再用git tag查看,发现v0.9标签不见了,但是版本561ca86还在,可见只是删除了标签,版本没有删除:

1
2
3
4
5
6
7
8
9
10
$ git tag

v0.1
v1.0
$ git log --pretty=oneline --abbrev-commit
18233d5 (HEAD -> master, tag: v1.0) test file add new
561ca86 (origin/master, origin/HEAD) add file test.txt
b0e2922 add file git1/git1.log
e1c4305 add dir git1
50bbda8 rm test.txt

因为创建的标签都只存储在本地,不会自动推送到远程。所以,打错的标签可以在本地安全删除。如果要推送某个标签到远程,使用命令_git push origin _

1
2
3
4
5
6
7
8
9
10
11
$ git push origin v1.0

Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 257 bytes | 128.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:hyd861001/my_python.git
* [new tag] v1.0 -> v1.0

或者一次性推送所有尚未推送的本地标签:

1
2
3
4
5
6
7
$ git push origin --tags

Counting objects: 1, done.
Writing objects: 100% (1/1), 160 bytes | 160.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To github.com:hyd861001/my_python.git
* [new tag] v0.1 -> v0.1

如果要删除远程标签,就要首先删除本地标签:

1
2
3
$ git tag -d v0.1

Deleted tag 'v0.1' (was 3ecb4b0)

然后再远程删除,删除命令也是使用push:

1
2
3
4
$ git push origin :refs/tags/v0.1

To gitee.com:hyd861001/my_python.git
- [deleted] v0.1

综上所述:
打标签 其实只有两个常用命令

添加标签:

1
git tag v1.0.0

推送标签到仓库:

1
git push origin --tags

生成秘钥

1
ssh-keygen
更新于

请我喝[茶]~( ̄▽ ̄)~*

Ming Liu 微信支付

微信支付

Ming Liu 支付宝

支付宝

Ming Liu 贝宝

贝宝