Git 与 master 合并,无需结帐到 master [英] Git merge with master without checkout to master

查看:26
本文介绍了Git 与 master 合并,无需结帐到 master的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我需要merge developmaster 我都会这样做:

Every time I need to merge develop with master I do:

git checkout master

git merge develop

有时我忘记切换出master.因此,我在 master 上错误地更改了代码.它可能很乱.我已经错误地向 master 承诺了几次.

Sometimes I forget to switch out of master. Due to this, I mistakenly make changes to code while still on master. It can be messy. I've committed to master a few times by mistake.

有没有办法不用先切换到mastermergemaster?

Is there a way to merge to master without switching first to master?

推荐答案

如果你想做一个 real 合并到 master,不.真正的合并至少可能需要一个工作树来进行工作.它还使用索引,它需要匹配操作外部的当前分支(参见下面的 git worktree add 方法).

If you want to do a real merge into master, no. A real merge needs, at least potentially, a work-tree in which to do work. It also uses the index, which needs to match the current branch around the outside of the operation (see git worktree add method below).

如果您想做一个快进操作(不是实际的合并),这是可能的.例如:

If you want to do a fast-forward operation (not an actual merge), it is possible. For instance:

git push . develop:master

(注意缺少 + 符号或 --force 选项)将尝试将您的 master 快进到与您的开发.使用 HEAD 表示当前分支:

(note the lack of + sign or --force option) will attempt to fast-forward your master to the same commit as your develop. Use HEAD to mean the current branch:

git push . HEAD:master

这些只有在可以快进的情况下才有效.如果没有,您将收到表单错误:

These only work if the fast-forward is possible. If not, you will get an error of the form:

 ! [rejected]              [name] -> master (non-fast-forward)

它告诉你你需要一个分支是 master 的工作树来运行 git merge.

which tells you that you need a work-tree whose branch is master in which to run git merge.

要在不更改当前工作树的分支的情况下执行此操作,请使用 git worktree add,如果您的 Git 至少为 2.5(最好至少为 2.15).例如,假设您位于存储库的顶层,在分支 feature/X 上:

To do that without changing the branch of your current work-tree, use git worktree add, if your Git is at least 2.5 (preferably at least 2.15). For instance, suppose you are in the top level of your repository, on branch feature/X:

git worktree add ../master   # NB: assumes ../master is available as a directory name
cd ../master
git merge feature/X
...                 # do what it takes to complete the merge here

cd -                # return to your main repository
rm -rf ../master    # remove added worktree
git worktree prune  # clean up list of added worktrees

添加的工作树有它自己的索引(和它自己的 HEAD),所以 git merge 现在有一个索引和工作树来完成它的工作,或者让你解决它的混乱,以实际发生的为准(见 kostix 的评论).

The added work-tree has its own index (and its own HEAD) so git merge now has an index and work-tree in which to do its job, or leave its mess for you to fix, whichever actually occurs (see kostix's comment).

这篇关于Git 与 master 合并,无需结帐到 master的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆