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
Answer from VonC on Stack OverflowYou 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
Couple of points:
- I believe
git stash+git stash dropcould be replaced withgit reset --hard ... or, even shorter, add
-ftocheckoutcommand:git checkout -f -b $branchThat will discard any local changes, just as if
git reset --hardwas 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.
version control - How do I force "git pull" to overwrite local files? - Stack Overflow
how can I git checkout and overwirte branch by force? - Stack Overflow
Share your best git alias you made.
Does git pull not overwrite local code always?
Reset the index and the head to origin/master, but do not reset the working tree:
git reset origin/master
Despite the original question, the top answers can cause problems for people who have a similar problem, but don't want to lose their local files. For example, see Al-Punk and crizCraig's comments.
The following version commits your local changes to a temporary branch (tmp), checks out the original branch (which I'm assuming is master) and merges the updates. You could do this with stash, but I've found it's usually easier to simply use the branch / merge approach.
git checkout -b tmp
git add *; git commit -am "my temporary files"
git checkout master
git fetch origin master
git merge -s recursive -X theirs origin master
where we assume the other repository is origin master.