You have a scenario where you create a feature branch from the main branch. Then you work for a while on the feature branch and want to merge these changes into main.
But in the meantime changes have also been made to main and you are unsure if there are now conflicts.
With Git rebase you can take all the latest changes from main and sync them into your current feature branch. And all your current commits will be added on top of those changes from main (this keeps the commit history clean). It is similar to if you started over on a fresh feature branch from main and then added all your changes again.
It is also described here and on YouTube here.
To use git rebase, run the following commands:
Go to main branch first and get latest changes:
git switch main
git pull
Then switch to your existing feature branch:
git switch feature/my-feature-branch
If others have worked on the same feature branch, you can pull the latest changes with:
git pull --rebase (optional)
If only you have worked on the feature branch you can skip that (as you know you have the latest changes) and then run the rebase command:
git rebase main
This will add all the latest changes to your local feature branch.
Then you need to push those changes to the remote feature branch (typically, so you can create a pull request and merge everything into main):
git push --force-with-lease
You cannot run a regular git push since the local and remote branch history are no longer the same (since changes from main have been interjected into the feature branch). Using --force-with-lease is safe as it checks that remote and local are still the same before pushing and if not it will fail.
That's it.
If you run into a merge conflict you can try and open the file and update as needed and then run:
git add the-file-name-you-updated
git rebase --continue
Or if you want to cancel entirely, run:
git rebase --abort
To see more info on general Git usage and commands, see here.