我有以下情况:
* ab82147 (HEAD, topic) changes
* 8993636 changes
* 82f4426 changes
* 18be5a3 (master) first
我想将(非快进)topic
合并到master
。这需要我:
git checkout master
git merge --no-ff topic
但是检查master,然后将主题合并到它会导致git更改我的工作目录(虽然最终结果与检查master之前的结果相同),而我遇到的问题是由于我们的项目,它需要大约30分钟来构建它(使用IncrediBuild)虽然没有真正改变,但它简直无法忍受。
所以我想得到以下内容:
* 9075cf4 (HEAD, master) Merge branch 'topic'
|\
| * ab82147 (topic) changes
| * 8993636 changes
| * 82f4426 changes
|/
* 18be5a3 first
没有真正触及工作目录(或者至少在某种程度上欺骗git)。
答案 0 :(得分:9)
有趣!我不认为有这样做的内置方法,但你应该能够使用管道来捏造它:
#!/bin/bash
branch=master
# or take an argument:
# if [ $@ eq 1 ];
# branch="$1";
# fi
# make sure the branch exists
if ! git rev-parse --verify --quiet --heads "$branch" > /dev/null; then
echo "error: branch $branch does not exist"
exit 1
fi
# make sure this could be a fast-forward
if [ "$(git merge-base HEAD $branch)" == "$(git rev-parse $branch)" ]; then
# find the branch name associated with HEAD
currentbranch=$(git symbolic-ref HEAD | sed 's@.*/@@')
# make the commit
newcommit=$(echo "Merge branch '$currentbranch'" | git commit-tree $(git log -n 1 --pretty=%T HEAD) -p $branch -p HEAD)
# move the branch to point to the new commit
git update-ref -m "merge $currentbranch: Merge made by simulated no-ff" "refs/heads/$branch" $newcommit
else
echo "error: merging $currentbranch into $branch would not be a fast-forward"
exit 1
fi
有趣的是newcommit=
行;它使用commit-tree直接创建合并提交。第一个参数是要使用的树;那是树HEAD,你要保留其内容的分支。提交消息在stdin上提供,其余参数命名新提交应具有的父项。提交的SHA1打印到stdout,因此假设提交成功,您捕获它,然后合并该提交(这将是快进)。如果你很痴迷,你可以确保commit-tree成功 - 但这应该得到很好的保证。
限制:
--no-ff
时,git实际上会强迫自己使用默认(递归)策略,但是在reflog中写这个就是谎言。是的,我在一个玩具回购中测试了它,它似乎正常工作! (虽然我没有努力打破它。)
答案 1 :(得分:3)
我能想到的最简单的方法是git clone
到一个单独的工作副本,在那里进行合并,然后再回到git pull
。然后拉动将是快进,并且应该只影响真正改变的文件。
当然,有这么大的项目制作临时克隆并不理想,需要一大块额外的硬盘空间。只要您不需要磁盘空间,就可以通过保留合并副本来最小化(从长远来看)额外克隆的时间成本。
免责声明:我尚未证实这是有效的。我相信它应该(git不是版本文件时间戳)
答案 2 :(得分:0)
或者,您可以通过保存和恢复文件时间戳来直接修复症状。这有点难看,但写作很有意思。
Python时间戳保存/恢复脚本
#!/usr/bin/env python
from optparse import OptionParser
import os
import subprocess
import cPickle as pickle
try:
check_output = subprocess.check_output
except AttributeError:
# check_output was added in Python 2.7, so it's not always available
def check_output(*args, **kwargs):
kwargs['stdout'] = subprocess.PIPE
proc = subprocess.Popen(*args, **kwargs)
output = proc.stdout.read()
retcode = proc.wait()
if retcode != 0:
cmd = kwargs.get('args')
if cmd is None:
cmd = args[0]
err = subprocess.CalledProcessError(retcode, cmd)
err.output = output
raise err
else:
return output
def git_cmd(*args):
return check_output(['git'] + list(args), stderr=subprocess.STDOUT)
def walk_git_tree(rev):
""" Generates (sha1,path) pairs for all blobs (files) listed by git ls-tree. """
tree = git_cmd('ls-tree', '-r', '-z', rev).rstrip('\0')
for entry in tree.split('\0'):
print entry
mode, type, sha1, path = entry.split()
if type == 'blob':
yield (sha1, path)
else:
print 'WARNING: Tree contains a non-blob.'
def collect_timestamps(rev):
timestamps = {}
for sha1, path in walk_git_tree(rev):
s = os.lstat(path)
timestamps[path] = (sha1, s.st_mtime, s.st_atime)
print sha1, s.st_mtime, s.st_atime, path
return timestamps
def restore_timestamps(timestamps):
for path, v in timestamps.items():
if os.path.isfile(path):
sha1, mtime, atime = v
new_sha1 = git_cmd('hash-object', '--', path).strip()
if sha1 == new_sha1:
print 'Restoring', path
os.utime(path, (atime, mtime))
else:
print path, 'has changed (not restoring)'
elif os.path.exists(path):
print 'WARNING: File is no longer a file...'
def main():
oparse = OptionParser()
oparse.add_option('--save',
action='store_const', const='save', dest='action',
help='Save the timestamps of all git tracked files')
oparse.add_option('--restore',
action='store_const', const='restore', dest='action',
help='Restore the timestamps of git tracked files whose sha1 hashes have not changed')
oparse.add_option('--db',
action='store', dest='database',
help='Specify the path to the data file to restore/save from/to')
opts, args = oparse.parse_args()
if opts.action is None:
oparse.error('an action (--save or --restore) must be specified')
if opts.database is None:
repo = git_cmd('rev-parse', '--git-dir').strip()
dbpath = os.path.join(repo, 'TIMESTAMPS')
print 'Using default database:', dbpath
else:
dbpath = opts.database
rev = git_cmd('rev-parse', 'HEAD').strip()
print 'Working against rev', rev
if opts.action == 'save':
timestamps = collect_timestamps(rev)
data = (rev, timestamps)
pickle.dump(data, open(dbpath, 'wb'))
elif opts.action == 'restore':
rev, timestamps = pickle.load(open(dbpath, 'rb'))
restore_timestamps(timestamps)
if __name__ == '__main__':
main()
Bash测试脚本
#!/bin/bash
if [ -d working ]; then
echo "Cowardly refusing to mangle an existing 'working' dir."
exit 1
fi
mkdir working
cd working
# create the repository/working copy
git init
# add a couple of files
echo "File added in master:r1." > file-1
echo "File added in master:r1." > file-2
mkdir dir
echo "File added in master:r1." > dir/file-3
git add file-1 file-2 dir/file-3
git commit -m "r1: add-1, add-2, add-3"
git tag r1
# sleep to ensure new or changed files won't have the same timestamp
echo "Listing at r1"
ls --full-time
sleep 5
# make a change
echo "File changed in master:r2." > file-2
echo "File changed in master:r2." > dir/file-3
echo "File added in master:r2." > file-4
git add file-2 dir/file-3 file-4
git commit -m "r2: change-2, change-3, add-4"
git tag r2
# sleep to ensure new or changed files won't have the same timestamp
echo "Listing at r2"
ls --full-time
sleep 5
# create a topic branch from r1 and make some changes
git checkout -b topic r1
echo "File changed in topic:r3." > file-2
echo "File changed in topic:r3." > dir/file-3
echo "File added in topic:r3." > file-5
git add file-2 dir/file-3 file-5
git commit -m "r3: change-2, change-3, add-5"
git tag r3
# sleep to ensure new or changed files won't have the same timestamp
echo "Listing at r3"
ls --full-time
sleep 5
echo "Saving timestamps"
../save-timestamps.py --save
echo "Checking out master and merging"
# merge branch 'topic'
git checkout master
git merge topic
echo "File changed in topic:r3." > file-2 # restore file-2
echo "File merged in master:r4." > dir/file-3
git add file-2 dir/file-3
git commit -m "r4: Merge branch 'topic'"
git tag r4
echo "Listing at r4"
ls --full-time
echo "Restoring timestamps"
../save-timestamps.py --restore
ls --full-time
我将把它作为练习让读者清理Python脚本以删除无关的输出并添加更好的错误检查。
答案 3 :(得分:0)
这是一种作弊版本。
答案 4 :(得分:0)
您现在可以返回主题进行预合并提交:git reset HEAD〜
答案 5 :(得分:0)
绝对可以进行任何合并,甚至是非快进合并,而无需$scope.switchView = function(){
// I want to cfset session.remoteuser = userid
// and redirect to main page to change the view
}
,弄乱提交历史记录或克隆。秘诀是添加第二个“工作树”,这样您就可以在同一存储库中有效地进行主要和次要结帐。
git checkout
您现在已将本地工作分支合并到本地cd local_repo
git worktree add _master_wt master
cd _master_wt
git pull origin master:master
git merge --no-ff -m "merging workbranch" my_work_branch
cd ..
git worktree remove _master_wt
分支,而无需切换结帐。