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
git - Force overwrite of local file with what's in origin repo? - Stack Overflow
How do I overwrite one branch with another branch?
How to overwrite local changes from remote repository?
Does git checkout overwrite uncommitted changes in the working directory?
Yes. git checkout -- replaces the files at that path with the source-branch versions, discarding any uncommitted local edits there. Commit or stash your work first if you need to keep it.
How do I force git pull to overwrite local files?
Why does git checkout create new files in the target directory?
git checkout -- mirrors the source directory exactly, so files present in the source but missing in the destination are created. The commit output reports them, for example create mode 100744 scripts/publish/how_to.md.
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.
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'.