Is there a Git command (or a short sequence of commands) that will safely and surely do the following?
- Get rid of any local changes.
- Fetch the given branch from origin if necessary
- Checkout the given branch?
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:
- It's a part of homebrewed deployment script for an application (the code is hosted on GitHub).
- There should be no difference if the branch was already fetched from origin or not (i.e. the first deployment of a new branch shouldn't ideally require any additional steps).
- The script is located on a remote machine that can be accessed by several people, hence no credentials are stored and user/password must be entered (but only once if possible).
- I don't care about any local changes; I always want a pristine copy of the given branch (the further part of deployment script produces local changes).
- I can't clone or export a fresh repository each time; it takes too much time.
git reset --hard
2.git checkout $branch
3.git pull origin $branch