If you want to overwrite only one file:
git fetch
git checkout origin/main <filepath>
If you want to overwrite all changed files:
git fetch
git reset --hard origin/main
(This assumes that you're working on main locally and you want the changes on the origin's main - if you're on a branch, or your project uses the old master main branch name rather than main, substitute that in instead.)
If you want to overwrite only one file:
git fetch
git checkout origin/main <filepath>
If you want to overwrite all changed files:
git fetch
git reset --hard origin/main
(This assumes that you're working on main locally and you want the changes on the origin's main - if you're on a branch, or your project uses the old master main branch name rather than main, substitute that in instead.)
Simplest version, assuming you're working on the same branch that the file you want is on:
git checkout path/to/file.
I do this so often that I've got an alias set to gc='git checkout'.
deployment - Git command to checkout any branch and overwrite local changes - Stack Overflow
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
Stashing and force checkouts
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
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.
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.
So I was checked out on a feature branch; I realized that I wanted to keep the progress but checkout to my development branch realy quickly. So I did git stash. Git says: Saved working directory and index state. So I assumed it got stashed. I thought all the modified files/new files would dissapear when 'stashed'; but guess not.
Then I forced checkout to development, thinking that all the changes I made would be removed. But it didn't. why is that? How would I accomplish my goal of removing every modified file I made in my feature branch?