92

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
szeryf
  • 3,197
  • 3
  • 27
  • 28

4 Answers4

134

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 abranch 

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.


If you do not want to switch branch, but only restore a folder from another branch, then git restore is the other command which replaces the old obsolete and confusing git checkout.
I presented git restore here.

git restore --source=anotherBranch --staged] [--worktree -- aFolder
# or, shorter:
git restore -s anotherBranch -SW -- aFolder
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I'm using this solution, but I added additional `git checkout $branch` at the end, because otherwise the current branch information returned by git was wrong. – szeryf Sep 17 '13 at 09:20
  • 1
    @VonC, Thank you. this really has overwritten master with branch. – psun Jun 20 '16 at 18:47
  • what if I'm using it to import an entire folder from another branch, and I want their files to overwrite mines? `git checkout ??? develop -- src/main/ ` – tuxErrante Jun 14 '21 at 13:46
  • 1
    @tuxErrante So you don't want to switch branch then? In that case, [`git restore`](https://git-scm.com/docs/git-restore) is for you. Nobody should use in 2021 `git checkout`. It is old, obsolete and confusing. `git restore -s experiment -SW -- aFolder`: that will restore `aFolder` content from the branch `experiment`, both working tree (W) and index (S). – VonC Jun 14 '21 at 13:52
  • 1
    @tuxErrante I have edited the answer to make the distinction (between switching branch and restoring content) clearer. – VonC Jun 14 '21 at 13:56
  • I was having some strange behavior where I couldnt switch from one branch to the next because it would overwrite the files, but I had already committed and pushed those files and merged them on github. So what i did was use the `git switch -f $branch` which worked for me while the first one didnt. Then when it switched to the branch, I did `git pull origin $branch` to pull down the changes, then I did `git reset --hard` and then everything was back to normal thank you – Uriahs Victor Oct 10 '21 at 20:25
  • It is not clear how `origin/abranch` and `$branch` are related. Do you mind elaborating, please? – MikeSchinkel Oct 26 '21 at 23:54
  • 1
    @MikeSchinkel That seems to be a typo. I have edited the answer to use the same name as the fetched branch. – VonC Oct 27 '21 at 05:23
  • @VonC — Thanks for the quick response! – MikeSchinkel Oct 28 '21 at 06:06
40

Couple of points:

  • I believe git stash + git stash drop could be replaced with git reset --hard
  • ... or, even shorter, add -f to checkout command:

    git checkout -f -b $branch
    

    That will discard any local changes, just as if git reset --hard was used prior to checkout.

As for the main question: instead of pulling in the last step, you could just merge the appropriate branch from the remote into your local branch: git merge $branch origin/$branch, I believe it does not hit the remote. If that is the case, it removes the need for credensials and hence, addresses your biggest concern.

Marcin Łoś
  • 3,226
  • 1
  • 19
  • 21
  • 5
    The -b may need to be capitalized. `git checkout -f -B $branch` resets the local branch if it already exists. – xer0x Nov 10 '17 at 18:12
  • 3
    Just `git checkout -B $existing_branch_name` worked for me, whereas `git checkout -f -b $existing_branch_name` didn't – so like @xer0x said, I think the answer is incorrect insofar as the capital `-B` is a requirement? – Suan Apr 09 '21 at 15:34
13

git reset and git clean can be overkill in some situations (and be a huge waste of time).

If you simply have a message like "The following untracked files would be overwritten..." and you want the remote/origin/upstream to overwrite those conflicting untracked files, then git checkout -f <branch> is the best option.

If you're like me, your other option was to clean and perform a --hard reset then recompile your project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron Hull
  • 422
  • 4
  • 16
3

The new git-switch command (starting in GIT 2.23) also has a flag --discard-changes which should help you. git pull might be necessary afterwards.

Warning: it's still considered to be experimental.

Jan
  • 310
  • 1
  • 7
  • Good point. I have promoted git switch in the past (https://stackoverflow.com/a/57066202/6309). I have updated my 2013 answer to include that new command. – VonC Jan 28 '20 at 15:04