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?
⚠ Warning:
Any uncommitted local change to tracked files will be lost, even if staged.
But any local file that's not tracked by Git will not be affected.
First, update all origin/<branch> refs to latest:
git fetch --all
Backup your current branch (e.g. main):
git branch backup-main
Jump to the latest commit on origin/main and checkout those files:
git reset --hard origin/main
Explanation:
git fetch downloads the latest from remote without trying to merge or rebase anything.
git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/main.
Maintain current local commits
[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from main before resetting:
git checkout main
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/main
After this, all of the old commits will be kept in new-branch-to-save-current-commits.
Uncommitted changes
Uncommitted changes, even if staged (with git add), will be lost. Make sure to stash or commit anything you need. For example, run the following:
git stash
And later (after git reset), reapply these uncommitted changes:
git stash pop
Which may create merge conflicts.
This will remove all uncommitted changes, even if staged,
and then pull:
git reset --hard HEAD
git pull
But any local file that's not tracked by Git will not be affected.
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?