Is there a Git command (or a short sequence of commands) that will safely and surely do the following?
Currently I'm stuck with:
git fetch -p
git stash
git stash drop
git checkout $branch
git pull
but it's bothering me because I'm asked for password two times (by fetch
and pull
). Generally I would be happy with any solution as long as the password is needed only once.
A couple of notes:
You could follow a solution similar to "How do I force “git pull” to overwrite local files?":
git fetch --all
git reset --hard origin/abranch
git checkout $branch
That would involve only one fetch.
With Git 2.23+, git checkout
is replaced here with git switch
(presented here) (still experimental).
git switch -f $branch
(with -f
being an alias for --discard-changes
, as noted in Jan's answer)
Proceed even if the index or the working tree differs from HEAD.
Both the index and working tree are restored to match the switching target.