我正在尝试优化个人的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”分支继续编码。所以,另一个问题是:在这个过程中会有任何引起问题的地方吗?
更新:我应该在第一次发布时就把这个放进去,但我之所以不简单地使用合并,是因为在主分支上更改版本号,然后尝试与开发分支中的更改合并会导致合并冲突。我知道它们是无关紧要的,但它仍然会停止这个过程。我之前使用过“merge -Xtheirs {branch}”来处理它,但我对此不太确定。
更新2:这里有一些我知道行不通的东西。我已经组合了一个适用于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部分。
master
? - Abhishek Gupta