美文网首页
Git命令备忘

Git命令备忘

作者: noneinwind | 来源:发表于2018-06-11 09:23 被阅读0次

1. Merge without checkouts

The syntax of git fetch for (non-)fast-forward merges

If you want the fetch command to fail if the update is non-fast-forward, then you simply use a refspec of the form

git fetch <remote> <remoteBranch>:<localBranch>

If you want to allow non-fast-forward updates, then you add a + to the front of the refspec:

git fetch <remote> +<remoteBranch>:<localBranch>

Note that you can pass your local repo as the "remote" parameter using .:

git fetch . <sourceBranch>:<destinationBranch>

The Documentation

From the git fetch documentation that explains this syntax:

<refspec>

The format of a <refspec> parameter is an optional plus +, followed by the source ref <src>, followed by a colon :, followed by the destination ref <dst>.

The remote ref that matches <src> is fetched, and if <dst> is not empty string, the local ref that matches it is fast-forwarded using <src>. If the optional plus + is used, the local ref is updated even if it does not result in a fast-forward update.

Examples:

# Merge local branch foo into local branch master,
# without having to checkout master first.
# Here `.` means to use the local repository as the "remote":
git fetch . foo:master

# Merge remote branch origin/foo into local branch foo,
# without having to checkout foo first:
git fetch origin foo:foo

You cannot merge a branch B into branch A without checking out A first if it would result in a non-fast-forward merge. This is because a working copy is needed to resolve any potential conflicts.
However, in the case of fast-forward merges, this is possible, because such merges can never result in conflicts, by definition. To do this without checking out a branch first, you can use git fetch with a refspec.
This use-case is so common, that you'll probably want to make an alias for it in your git configuration file, like this one:

[alias]
    sync = !sh -c 'git checkout --quiet HEAD; git fetch upstream master:master; git checkout --quiet -'

What this alias does is the following:

  1. git checkout HEAD: this puts your working copy into a detached-head state. This is useful if you want to update master while you happen to have it checked-out. I think it was necessary to do with because otherwise the branch reference for master won't move, but I don't remember if that's really right off-the-top of my head.
  2. git fetch upstream master:master: this fast-forwards your local master to the same place as upstream/master.
  3. git checkout - checks out your previously checked-out branch (that's what the - does in this case).

See Also

  1. Merge, update, and pull Git branches without using checkouts
  2. Git checkout and merge without touching working tree
  3. Merging without changing the working directory

相关文章

  • git命令整理

    git常用命令 GIT常用命令备忘:http://stormzhang.com/git/2014/01/27/gi...

  • Git 相关

    Git相关的命令备忘 Git每次输入账号和密码在Git目录下执行命令: git config --global c...

  • Git常用命令备忘

    Git常用命令备忘 git config --global user.name "robbin" git conf...

  • 快速上手Git

    使用git 命令行时,经常会忘记相应的git命令,这次梳理最基本的git使用当做备忘,顺带帮助读者快速上手git。...

  • Git命令备忘

    1. Merge without checkouts The syntax of git fetch for (n...

  • Git命令备忘

    1. Merge without checkouts The syntax of git fetch for (n...

  • Git命令备忘

    Git文件的状态 已修改modified-文件被修改了,但是还没有提交保存。 已暂存staged-已修改的文件放在...

  • git 命令备忘

    记录个人 git 备忘,持续更新。 查看文件文件的提交记录 删除远程分支

  • Git 命令备忘

    创建新的git仓库 checkout repo 工作目录持有实际的文件暂存区(index)像个缓存区域。临时保存你...

  • git命令备忘

    在工作之余,做一些备忘是很重要的,之前在新浪博客真的是坑爹,吭哧吭哧几个小时的东西一不小心就全军覆没...2017...

网友评论

      本文标题:Git命令备忘

      本文链接:https://www.haomeiwen.com/subject/xoeceftx.html