我如何通过编程快速转发单个git commit? [英] How can I fast-forward a single git commit, programmatically?
问题描述
我定期从git获取消息,如下所示:
您的分支位于跟踪的远程分支'local-master / master'
提交3次,并且可以快进。
我希望能够在可以执行以下操作的shell脚本中编写命令: p>
-
如何判断当前分支是否可以从正在跟踪的远程分支进行快速转发?
-
我怎么知道我的分支后面有多少提交?
-
如何快速转发通过一个 提交,例如,我的本地分支将从后面3次提交到后面2次提交?
(对于那些有兴趣的人,我正在尝试组合一个高质量的git / darcs镜像。)
如果当前提交是远程分支头的祖先,远程分支可以被快速转发到本地分支。换句话说,如果远程分支的一分支历史包含当前提交(因为如果它确实如此,那么确定新提交已经提交到当前提交上)。
因此,一个安全的方式来确定远程分支是否可以被快速转发:
#转换参考提交ID的名称
current_commit = $(git rev-parse HEAD)
remote_commit = $(git rev -parse remote_name / remote_branch_name)
#调用git log,只提交ID
log = $(git log --topo-order --format ='%H'$ remote_commit | grep $ current_commit)
#检查当前提交的存在日志
if [! -z$ log]
然后echo'远程分支可以快进!'
fi
请注意,没有使用--all参数(它会列出所有分支)调用git log,所以当前提交不可能在side branch上并且仍然打印在输出上。
当前提交之前的提交数量等于$ current_commit之前的$ log中的行数。
如果只想快速转发一个提交,则将当前提交的前一行(例如grep -B 1)取出,并将本地分支重置为此提交。
UPDATE :您可以使用 git log commit1..commit2
来确定快速转发提交的数量:
if [! -z$ log]
然后
#打印当前提交之前的提交数
ff_commits = $(git log --topo-order --format ='%H' \
$ current_commit .. $ remote_commit | wc -l)
echo快进提交的数量:$ ff_commits
#快进只有一个提交
if [$ ff_commits -gt 1]
然后
next_commit = $(git log --topo-order --format ='%H'\
$ current_commit .. $ remote_commit | tail -1)
git reset --hard $ next_commit
fi
fi
当然,如果您将第一次调用的结果保存到文件中,您可以使用一次git日志调用完成此操作。
I periodically get message from git that look like this:
Your branch is behind the tracked remote branch 'local-master/master'
by 3 commits, and can be fast-forwarded.
I would like to be able to write commands in a shell script that can do the following:
How can I tell if my current branch can be fast-forwarded from the remote branch it is tracking?
How can I tell how many commits "behind" my branch is?
How can I fast-forward by just one commit, so that for example, my local branch would go from "behind by 3 commits" to "behind by 2 commits"?
(For those who are interested, I am trying to put together a quality git/darcs mirror.)
The remote branch can be fast-forwarded to the local branch if the current commit is the ancestor of the remote branch head. In other words, if the "one-branch history" of the remote branch contains the current commit (because if it does, it is sure that the new commits were committed "onto" the current commit)
So a safe way to determine whether the remote branch can be fast-forwarded:
# Convert reference names to commit IDs
current_commit=$(git rev-parse HEAD)
remote_commit=$(git rev-parse remote_name/remote_branch_name)
# Call git log so that it prints only commit IDs
log=$(git log --topo-order --format='%H' $remote_commit | grep $current_commit)
# Check the existence of the current commit in the log
if [ ! -z "$log" ]
then echo 'Remote branch can be fast-forwarded!'
fi
Note that git log was called without the --all parameter (which would list all branches), so it is not possible that the current commit is on a "side branch" and is still printed on the output.
The number of commits ahead of the current commit equals the number of rows in $log before $current_commit.
If you want to fast-forward only one commit, you take the row previous to the current commit (with grep -B 1, for example), and reset the local branch to this commit.
UPDATE: you can use git log commit1..commit2
to determine the number of fast-forwarding commits:
if [ ! -z "$log" ]
then
# print the number of commits ahead of the current commit
ff_commits=$(git log --topo-order --format='%H' \
$current_commit..$remote_commit | wc -l)
echo "Number of fast-forwarding commits: $ff_commits"
# fast-forward only one commit
if [ $ff_commits -gt 1 ]
then
next_commit=$(git log --topo-order --format='%H' \
$current_commit..$remote_commit | tail -1)
git reset --hard $next_commit
fi
fi
Of course, you can do this with one git log call if you save the result of the first call into a file.
这篇关于我如何通过编程快速转发单个git commit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!