⚠ 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
Answer from RNA on Stack OverflowWhich may create merge conflicts.
⚠ 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.
git - Resolve merge conflicts: Force overwrite all files - Stack Overflow
git - How to pull files and only override conflicts - Stack Overflow
How to force pull to overwrite merge conflicts?
github - Git pull to overwrite conflict files only - Stack Overflow
There are three simple solutions to copy the last version that is in you remote repository, discarding all changes that you have made locally:
Discard your repository and clone again. This is the most simple solution, but if your repository is big, it can take a long time, and may require extra effort like re
configureing, etc.Discard the local changes with
git reset --hard <hash>and then do agit pull. The problem is you need to first find a commit that precedes whatever change history you are trying to avoid. After resetting to that commit hash, do agit pull.Do a
git fetchto bring the updates to your local reference of the remote branch (usually origin/master) and then do agit reset --hardpassing this reference, ie,git reset --hard origin/master.
git reset --hard {remote_repository_name}/{branch_name}
Example:
git reset --hard upstream/branch1
If you are working with a branch you can use the above code.But before that, you have to set (upstream or origin) to your local repository,
git remote add upstream https://github.com/lakini/example.git
here,https://github.com/lakini/example.git is the remote upstream repository.
Same as like this we can work in the remote repository(origin) as well.
git reset --hard origin/branch1
Doing a git pull should do the right thing, as long as you haven't done git add on the files you don't want in to have under git. I suggest putting the names of those files in a .gitignore.
If you are running into a specific problem with using git pull, you should ask about that.
I don't know much about DaftMonk, but if it generates a lot of boilerplate that shouldn't be committed, then it seems likely that is part of the build/development process you need to manage. Meaning, you would call DaftMonk after you clone your repo. Possibly with a Makefile, or whatever build tool is common in your language of choice.
The idea is that generated files that can change because of a change in configuration/other source should not be modified by hand, but instead be regenerated as needed. Therefore, if DraftMonkey is doing such a generation, you need to incorporate that into your process.
If I understand the question correctly, you do not yet have the git repository on your local machine, and you want to clone it, overwriting those files on your local machine that also exist in the repo. If so, the easiest way to do it would be to clone the repo into a new directory, then copy that directory to your existing one.
Sure you can overwrite the file with merge markers with content of new file. Just do git add <file> and git commit after that.
Edit the file in any editor you wish.
How to set the desired git editor?
# Set the default git editor
git config --global core.editor <your editor>
Once the conflict is resolved simply save the file then add it to the index and commit

Tip:
If you wish to add some of the changes and not all of them at once use the git add -p
These are the options you can do within add -p:
y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Once you use the s it will pick the chunk of code which can be considered as a standalone change. If you want to split it, even more, you will have to use the e to edit the hunk and then add it back to the stage area.
