git fetch
gets some refs and their related objects that don't exist locally yet so that we can manipulate these refs and objects. By default, we can't fetch a single commit without any ref. But we can if the remote repository allows it with some config.
Besides git merge
and git rebase
, here are some more cases:
a)You'd like to reuse the commit message of a commit abc123
, but you don't have the commit yet. The commit is reachable from a branch master
of another remote repository.
git add .
git fetch <url_of_another_repository> master
git commit -c abc123 --reset-author
b)You'd like to have a quick look and find out what new commits you have made locally after you've made a mess.
git fetch origin master
git cherry FETCH_HEAD HEAD
c)You'd like to apply a commit from master
of another remote repository to your local branch.
git fetch <url_of_another_repository> master
git cherry-pick abc123
d)Your colleague has made a commit to a ref refs/reviews/333
and asks you to have a review but you don't have a web service to do it online. So you fetch it and checks it out.
git fetch origin refs/reviews/333
git checkout FETCH_HEAD