In general, you can use the --strategy option to specify how conflicts should be handled. The strategy theirs blindly replaces files.
However, in your case, you've only got a single file. You really won't want to use the theirs strategy for all files. Instead, after you get the message to resolve the conflict, you can checkout that one specific file from the branch:
git checkout <branch> yourfile
(At that point, <branch> still refers to the branch as it was before the rebase started.)
In general, you can use the --strategy option to specify how conflicts should be handled. The strategy theirs blindly replaces files.
However, in your case, you've only got a single file. You really won't want to use the theirs strategy for all files. Instead, after you get the message to resolve the conflict, you can checkout that one specific file from the branch:
git checkout <branch> yourfile
(At that point, <branch> still refers to the branch as it was before the rebase started.)
Make sure the file contents are correct, then git add it and continue the rebase.
Newbie - does git pull --rebase branch overwrite changes without warnings?
Why git rebase overwrite my local changes? How to avoid overwrite? - Stack Overflow
git svn - Git rebase - force overwrite on merge conflict - Stack Overflow
version control - How do I force "git pull" to overwrite local files? - Stack Overflow
Say 2 people are working at the same branch name development.
If person A changes file A -line 1> commit and pusehd
person B changes file A line 1 -> commit -> git pull --rebase origin development.
Will the git pull --rebase overwrite person A's changes without warnings? I am on mobile right now & can't test but my curiosity is itching !
It sounds like your working directory was dirty at the time you did your rebase. You should probably be prepared for your working directory and stage to be wiped when you do a rebase. As a workaround, if you find yourself with a non empty working directory and/or stage, but you need to rebase to the latest other branch, you could do a stash, i.e.
git stash
Git will make 2 (or even 3) commits to preserve your working directory and stage as they currently are. After the rebase is complete, you can get these changes back by applying the stash:
git stash apply
I think in some cases Git would not even allow you to do a merge or rebase depending on the state of your working directory and stage. But in any case, it might be a good idea to make sure they are clean before rebasing.
Without much information about your current state, it's possible that some of your local commits might already be part of the branch you're rebasing onto.
For example, let's say you're trying to rebase develop on top of master. The develop branch contains a number of commits, but you've merged develop into master a few commits back.
In this case, git rebase master may appear to lose some of the commits, but git has detected they're already part of master so they don't need to be reapplied.
You can use git rebase --interactive to be presented with the list of commits that are about to be rebased. If you don't see some commits you expected, or you see more commits than you expected, you might need to look at using --onto to change the starting point for the rebased commits.
⚠ 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.