bash 更改/添加文件到 git 标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26362067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing / adding files to a git tag?
提问by dot
I'm just familiarizing myself with git and now, git tags. Based on what I've understood to date, tags are just a snapshot of a point in time in history that we want to track. For example, for version numbers.
我只是熟悉 git 和 git 标签。根据我迄今为止的理解,标签只是我们想要跟踪的历史时间点的快照。例如,对于版本号。
I have a client who wants me to add / backport a bug fix to version 1.0 when we are now on version 2.0. The idea being that when we image a new v1.0 box, the bug fix is included.
当我们现在使用 2.0 版时,我有一个客户希望我将错误修复添加/向后移植到 1.0 版。这个想法是,当我们对一个新的 v1.0 盒子进行映像时,就会包含错误修复。
I'm not sure how I would do this.
我不确定我将如何做到这一点。
I have the following tags in the repo:
我在回购中有以下标签:
v2.0
v1.0
v0.1
I've tried to check out the tagged version by running the command
我试图通过运行命令来检查标记的版本
git checkout v1.0
Then I made my bug fix changes. And then I tried:
然后我进行了错误修复更改。然后我尝试:
git add .
git commit
git push
When I do the push, I get an error message stating that the updates were rejected because a pushed branch tip is behind its remote counterpart.
I'm currently googling this error but I would like to know if fundamentally, I'm doing something that I shouldn't.
当我执行推送时,我收到一条错误消息,指出更新被拒绝,因为推送的分支提示落后于其远程副本。
我目前正在谷歌上搜索这个错误,但我想知道从根本上说,我是否在做我不应该做的事情。
回答by Ionic? Biz?u
Overriding a tag/version is a bad practice in general. Since you only want to fix a bug from an older version, they way git
works is to create a new tag with that bug fixed.
覆盖标签/版本通常是一种不好的做法。由于您只想修复旧版本的错误,因此他们的工作方式git
是创建一个修复了该错误的新标签。
That's why we have CHANGELOG thing-- it helps humans to view the differences between versions/releases.
这就是为什么我们有CHANGELOG 的东西——它帮助人们查看版本/发布之间的差异。
In the case of fixing a bug in an older version (v0.1.0
) while a new version (v0.2.0
) has log of changes and cannot be synced with the old one, you probably want to create a new tag like v0.1.1
:
在修复旧版本 ( v0.1.0
) 中的错误而新版本 ( v0.2.0
) 具有更改日志且无法与旧版本同步的情况下,您可能想要创建一个新标签,例如v0.1.1
:
# creating v0.1.0
git init
echo "console.log('Hello Wrold')" > main.js
git add .
git commit -m 'Initial commit'
git tag v0.1.0
git remote add origin ...
git push --all
git push --tags
# creating v0.2.0
echo "console.log('Hello Mars')" > main.js
git commit -m 'Hello Mars instead of Hello world' .
git tag v0.2.0
git push --all
git push --tags
# fixing typo from v0.1.0 (edit/add files) and creating v0.1.1
git checkout v0.1.0
echo "console.log('Hello World')" > main.js
git commit -m 'Fixed typo.' .
git tag v0.1.1
git push --tags
# go back to main branch
git checkout master
Fixed typo.
commit will not appear in master
branch since it was added only in v0.1.1
tag.
Fixed typo.
commit 不会出现在master
分支中,因为它只添加到v0.1.1
标签中。
So, what if I want to override v0.1.0
? If that's a bad practice, doesn't make it impossible. Instead of creating a new version (git tag v0.1.1
) we can delete the old v0.1.0
and create it again, forcing pushing on remote:
那么,如果我想覆盖v0.1.0
怎么办?如果这是一个不好的做法,并不使它成为不可能。git tag v0.1.1
我们可以删除旧版本v0.1.0
并重新创建,而不是创建新版本 ( ) ,强制推送远程:
... # fixed typo
git tag -d v0.1.0
git tag v0.1.0
git push --tags -f
# go back to main branch
git checkout master
On GitHub, it releases will be listed like this:
在 GitHub 上,它的发布将如下列出:
Let's see the differences:
让我们看看差异:
$ git diff v0.1.0 v0.2.0
diff --git a/main.js b/main.js
index 07e8cb2..7b366c5 100644
--- a/main.js
+++ b/main.js
@@ -1 +1 @@
-console.log('Hello Wrold')
+console.log('Hello Mars')
$ git diff v0.1.1 v0.2.0
diff --git a/main.js b/main.js
index ba72797..7b366c5 100644
--- a/main.js
+++ b/main.js
@@ -1 +1 @@
-console.log('Hello World')
+console.log('Hello Mars')
$ git diff v0.1.0 v0.1.1
diff --git a/main.js b/main.js
index 07e8cb2..ba72797 100644
--- a/main.js
+++ b/main.js
@@ -1 +1 @@
-console.log('Hello Wrold')
+console.log('Hello World')
回答by Andrew C
You are confusing tags
with commits
. Usually tags
are analogous to pointers to commits
.
你混淆tags
了commits
。通常tags
类似于pointers to commits
.
For your scenario a typical model would be as follow
对于您的场景,典型模型如下
# Create a release branch for v1
git checkout -b v1_release v1.0
# make your bug fixes, `git commit`, etc.
emacs foo
git add foo
git commit -m "critical bug fix"
# tag your new release
git tag v1.1
# push the branch and the tags to the server
git push ...
To see what versions have the fix you would use git branch --contains FIX_SHA
or git tag --contains FIX_SHA
要查看哪些版本具有您将使用的修复程序git branch --contains FIX_SHA
或git tag --contains FIX_SHA
You could also create a v2_release branch and git merge
the fix there.
您还可以在那里创建一个 v2_release 分支和git merge
修复程序。
I would never recommend reusing the same version number for two different code images that were flashed onto hardware. That sounds like a customer relations and support nightmare.
我永远不会建议对闪存到硬件上的两个不同代码映像重复使用相同的版本号。这听起来像是客户关系和支持的噩梦。