Having a hard time understanding git-fetch

后端 未结 4 696
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 15:43

I am having a hard time understanding the nuances of git-fetch. I understand that doing a fetch, fetches the remote refs into a local tracking branch.

I

4条回答
  •  一整个雨季
    2020-11-22 15:51

    felipec have answered most of issues in question in his answer.

    A few remaining (most taken from git fetch manpage; which is a bit dated in some places, unfortunately):

    • If remote-tracking branch (branch which tracks some branch in some remote repository) does not exists, it would be created.

    • The branch you fetch into (the in [+]:) doesn't need to reside in remotes// namespace. For example for mirroring repositories (git clone --mirror) refspec is 1 to 1. In old days before separate remotes layout (before remotes// namespace for remote-tracking refs) master branch was fetched into branch called origin. Even currently tags are fetched directly into tags/ namespace in mirroring fashion.

    • If branch you are fetching into (the right hand side of refspec : does exist, Git would check if download would result in fast-forward, i.e. if current state in is ancestor of state in in given remote repository. If it isn't, and you don't use -f/--force option to git-fetch, or prefix refspec with '+' (use +: refspec) fetch would refuse to update that branch.

    • git fetch origin master is equivalent to git fetch origin master:, not to git fetch origin master:master; it stores fetched value of master branch (of remote origin) in FETCH_HEAD, and not in master branch or remote-tracking remotes/origin/master branch. It can be followed by git merge FETCH_HEAD. Usually not used directly, but as part of one-time pull without setting remote-tracking branch: git pull .

    • +refs/heads/*:refs/remotes/origin/* as value for remote.origin.fetch configuration variable means that each branch (ref in refs/heads/ namespace) in remote origin is fetched into respectively named remote-tracking branch in refs/remotes/origin/ namespace, e.g. master branch in origin (i.e. refs/heads/master ref) would be fetched into origin/master remote-tracking branch (i.e. refs/remotes/origin/master ref). The '+' prefix means that fetch would succeed even in non fast-forward case, which means when branch on remote is rebased, or rewound (reset to some state in past) or otherwise amended.

    Sidenote: You would probably want to use higher level git remote command to manage remote repositories and get updates.

提交回复
热议问题