This is what I use often:
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;
Note that it is good practice not to make changes to your local master/develop branch, but instead checkout to another branch for any change, with the branch name prepended by the type of change, e.g. feat/
, chore/
, fix/
, etc. Thus you only need to pull changes, not push any changes from master. Same thing for other branches that others contribute to. So the above should only be used if you have happened to commit changes to a branch that others have committed to, and need to reset. Otherwise in future avoid pushing to a branch that others push to, instead checkout and push to the said branch via the checked out branch.
If you want to reset your local branch to the latest commit in the upstream branch, what works for me so far is:
Check your remotes, make sure your upstream and origin are what you expect, if not as expected then use git remote add upstream <insert URL>
, e.g. of the original GitHub repo that you forked from, and/or git remote add origin <insert URL of the forked GitHub repo>
.
git remote --verbose
git checkout develop;
git commit -m "Saving work.";
git branch saved-work;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force
On GitHub, you can also checkout the branch with the same name as the local one, in order to save the work there, although this isn't necessary if origin develop has the same changes as the local saved-work branch. I'm using the develop branch as an example, but it can be any existing branch name.
git add .
git commit -m "Reset to upstream/develop"
git push --force origin develop
Then if you need to merge these changes with another branch while where there are any conflicts, preserving the changes in develop, use:
git merge -s recursive -X theirs develop
While use
git merge -s recursive -X ours develop
to preserve branch_name's conflicting changes. Otherwise use a mergetool with git mergetool
.
With all the changes together:
git commit -m "Saving work.";
git branch saved-work;
git checkout develop;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;
git add .;
git commit -m "Reset to upstream/develop";
git push --force origin develop;
git checkout branch_name;
git merge develop;
Note that instead of upstream/develop you could use a commit hash, other branch name, etc. Use a CLI tool such as Oh My Zsh to check that your branch is green indicating that there is nothing to commit and the working directory is clean (which is confirmed or also verifiable by git status
). Note that this may actually add commits compared to upstream develop if there is anything automatically added by a commit, e.g. UML diagrams, license headers, etc., so in that case, you could then pull the changes on origin develop
to upstream develop
, if needed.
git status
your second commandgit reset --hard HEAD
failed. You didn’t paste it’s output, though. → Incomplete question. – Selfconfessedgit status
saysnothing to commit, working directory clean
. – Please specify! – Selfconfessedrm -fr ./repo; git clone repo
. best way i've found – Hibernategit reset --hard origin/master
when it should have beengit reset --hard origin/main
. – Lexinegit reset --hard FETCH_HEAD
worked for me. I hadn't pushed after rebase. – Dabney