我正在尝试将个人 git 工作流程改进为更容易处理的东西。
以下是我在本文中如何使用 git 的一些背景知识:
一名开发人员是唯一在存储库上工作的开发人员。
存储在本地计算机上的存储库的单个副本。
只有两个分支“dev”和“master”。
所有工作都在“dev”上完成。
我想要完成的是达到这样的程度:对“master”分支所做的唯一提交是基于已确认的稳定“dev”代码的工作生产版本。
实际上,我想做的是:
当“dev”中的所有内容都经过测试并准备就绪后,将“master”更新为“dev”分支文件树的精确克隆。
对“master”进行细微修改以更新版本号等...
提交更新的“master”分支。
从“master”分支创建一个新标签。
我接近第一步的方法是签出“master”分支,然后运行:
'git diff master dev | git apply -'
据我了解,这有效地消除了“master”中的任何内容,并用“dev”的内容替换了整个树。运行“git status”似乎表明这正在执行基于上面#1 的预期操作。
那么,第一个问题:那是对的吗?
在“master”分支收到这些更新后,我运行脚本来更新文件中的版本号。然后,我只需运行标准的“git add”。和“git commit -a”来添加所有更改。最后,我创建一个新标签并返回“dev”分支再次开始编码。
那么,另一个问题:在此过程中是否有任何事情会导致问题?
更新:我应该在第一时间就把这个放在一起,但我不简单地使用合并的原因是更改 master 上的版本号,然后尝试与 dev 中的更改合并会导致合并冲突。我知道它们无关紧要,但它仍然停止了该过程。我之前使用过“merge -Xtheirs {branch}”来处理它,但我也不确定。
UPDATE2:这是一些我知道不起作用的东西。我编写了一个可在 Mac OSX 上运行的 bash 脚本。第一个尝试使用合并:
#!/bin/bash -x
####################
### file: merge1 ###
####################
### clear out the old stuff so you can rerun
rm -rf .git
rm *.txt
### setup the repository
git init
### ignore merge and output files for clarity sake
echo -e "output*\nmerge*" > .gitignore
git add .gitignore
### make the intial commit and move over to dev
git commit -m "Initial commit"
git checkout -b dev
### add stuff to test1.txt in dev
echo -e "FILE1 LINE\nVERSION-XXX\nFILE1 LINE" > test1.txt
echo -e "File2 LINE\nVERSION-XXX\nFILE2 LINE" > test2.txt
### add the files and commit
git add .
git commit -m "Created test1.txt and test2.txt in dev."
### output the state of test1.
cat test1.txt > output-dev-test1-a.txt
cat test2.txt > output-dev-test2-a.txt
### move to master and do a first merge which will work
git checkout master
git merge dev
### Update the version numbers in master and commit it
sed -i "" -e 's/VERSION-XXX/VERSION-1.0/g' test*.txt
git commit -am "Updated version to 1.0 on master"
cat test1.txt > output-master-test1-a.txt
cat test2.txt > output-master-test2-a.txt
### switch back to dev and commit an update to test1.txt
git checkout dev
sed -i "" -e 's/LINE/CHANGED/' test*.txt
git commit -am "Updated content in test*.txt on dev"
### dump test1.txt for reference.
cat test1.txt > output-dev-test1-b.txt
cat test2.txt > output-dev-test2-b.txt
### swtich back to master
git checkout master
######################################################################
### BREAK
######################################################################
### this is where the merge fails because of a conflict
git merge dev
我尝试的另一种方法是使用 -Xtheirs,它看起来一开始可以工作,但它不会更新所有内容。要查看这一点,请删除上面的 BREAK 之后的最后几行并将其替换为:
### merge with -Xtheirs works here. Proper version "XXX" is showing.
git merge -Xtheirs dev
### but if we update the version number one more time on master
sed -i "" -e 's/VERSION-XXX/VERSION-2.0/g' test*.txt
git commit -am "Updated version to 2.0 on master"
### dump reference file
cat test1.txt > output-master-test1-b.txt
cat test2.txt > output-master-test2-b.txt
### Now, go back to dev and change something in only one of the files
git checkout dev
sed -i "" -e 's/CHANGED/ALTERED/g' test2.txt
git commit -am "Altered only test2.txt on dev."
cat test1.txt > output-dev-test1-c.txt
cat test2.txt > output-dev-test2-c.txt
### are finally return to master and merge again
git checkout master
git merge -Xtheirs dev
### dump reference file
cat test1.txt > output-master-test1-c.txt
cat test2.txt > output-master-test2-c.txt
没有冲突,但“output-master-test1-c.txt”显示“VERSION-2.0”而不是所需的“VERSION-XXX”。发生这种情况似乎是因为文件没有发生任何更改。 “output-master-test2-c.txt”文件具有预期的“VERSION-XXX”字符串。当然,问题是尝试更新到版本 3.0 的查找和替换会在 test1-c 中丢失,因为它无法识别 VERSION 刺痛的 2.0 部分。