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.
Why does doing 'force checkout' to some previous commit destroy the file that isn't commited?
version control - How do I force "git pull" to overwrite local files? - Stack Overflow
"The following untracked working tree files would be overwritten by checkout"
Share your best git alias you made.
Let's say we have a text file with some text: boo. We commit that change and say it has a hash 398d. Now we make changes to that text and add 'foo' but we don't commit it yet. Instead we do git checkout -f 398d and all the changes get destroyed. Why it makes sense? Wouldn't it make more sense if instead we keep the uncommited text in case I switch to master branch again?
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.