Setting your branch to exactly match the remote branch can be done in two steps:
git fetch origin
git reset --hard origin/master
If you want to save your current branch's state before doing this (just in case), you can do:
git commit -a -m "Saving my work, just in case"
git branch my-saved-work
Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).
Note: the first example assumes that the remote repo's name is origin and that the branch named master in the remote repo matches the currently checked-out branch in your local repo, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.
BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).
Answer from Dan Moulding on Stack OverflowSetting your branch to exactly match the remote branch can be done in two steps:
git fetch origin
git reset --hard origin/master
If you want to save your current branch's state before doing this (just in case), you can do:
git commit -a -m "Saving my work, just in case"
git branch my-saved-work
Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).
Note: the first example assumes that the remote repo's name is origin and that the branch named master in the remote repo matches the currently checked-out branch in your local repo, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.
BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).
First, use git reset to reset to the previously fetched HEAD of the corresponding upstream branch:
git reset --hard @{u}
The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don't have to be explicitly specified. On Windows or with PowerShell, specify "@{u}" (with double quotes).
Next, as needed, use git clean to remove untracked files, optionally also with -x:
git clean -df
Finally, as needed, get the latest changes:
git pull
Hi everyone,
I’m a beginner with Git and I’ve gotten my local main branch a bit messed up. Here’s roughly what I did step by step:
-
Made git pull, git add file, made changes to the file. And then I made a commit locally on main.
-
Realized I shouldn’t have committed in this branch.
-
Tried to undo it using commands like git reset --hard HEAD~1. And I did it few times because I wasn't sure if it was even working. Everything was done locally. I haven’t pushed anything.
-
I also staged and unstaged some changes in my file.
-
Now my local branch is messy, and I’m worried that running commands that interact with GitHub could break things.
So, I just want to completely overwrite my local branch and make it exactly match the main branch on GitHub, discarding all Local commits and changes.
What’s the safest way to do this? I don’t care about losing any local work. I just want my branch to match the remote perfectly.
I did a hard reset on my local repo, now it won't sync with remote.
How to make my local Git branch exactly match GitHub?
[Git] How to 'git reset' a remote branch?
How to restore local repo to be the same as remote repo
What is the primary difference between git reset --hard HEAD and git reset --hard origin/main
Can I remove untracked files that are listed in my gitignore
Is resetting to origin/HEAD reliable
You are looking for git push -f origin branch-name.
See my other stack overflow answer on how git force pushing works here.
Well, the situation is pretty clear. You haven't created any new commit yet, so there's nothing to push. Look at that Nothing to commit, working directory clean.
If you really need to create a commit without any change, you may use git commit --allow-empty -m "empty commit". However I'd suggest to create a new file or change an existing one, and commit the changes. After that you may check if the commit was actually created, using git log. Then you may retry the push w/ git push origin master, I'm sure you'll succeed this time.
I might need some conceptual explanation here, because I think I made a mistake.
I have only some basic notions about how git works and I'm still learning programming, so getting good with it is not my priority atm.
So, I always thought that my REMOTE means == MAIN, but stored in the cloud. It appears I was wrong.
I made some test commits that I intended to revert and pushed them, but then I decided it would be bad to have those listed in the history and that just reverting them wouldn't do. So I followed this SO answer on how to remove commits from history, which basically tells you to hard reset the last commit and then push the changes to remote.
I understand this might mess things up if someone else is working on the repo, but this one is private.
Anyways, as I said, I assumed remote and main are the same thing, so I tried to push my changes and be done with it, but now it complains:
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Not only that, I noticed now that there are 2 branches on github: remote and main, and the remote branch is 1 commit behind. No idea what is wrong about my assumptions here, as I said, I always thought remote == main, but residing on the cloud. Not sure now.