When I do git status on remote, I get "Your branch is ahead of 'origin/master' by 7 commits." I want to un-commit all these 7 commits while retaining the updates I made on these files on remote. I haven't pushed them, just committed in the past. It's not easy to see which commits these 7 are because I've been pulling in between and git log looks messy and I don't know how to extract this information... Can I just do git reset
? I would like to also undo git add
of any new files added in these commits if that is possible..
Asked
Active
Viewed 317 times
0

ru111
- 813
- 3
- 13
- 27
-
It's a bit confusing how I worded it as I meant "remote" as in local commits as opposed to origin/master. If by "match" do you mean just the commits, then yes, because I don't want to lose the changes I made on my local machine. I just want it un-added and un-commited. – ru111 Sep 05 '21 at 22:38
-
I am looking at https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-reset.html but I do not understand the difference between --mixed and --soft – ru111 Sep 05 '21 at 22:39
-
To make the commits match, you can use the same command without `--hard`. This will result in your local branch having the same commits while retaining any changes you’ve made. At this point you could create a new commit with the changes. Make sense? – JBallin Sep 05 '21 at 22:41
-
`git reset --mixed` seems to be what you want: undo the commits & undo the git-add (while `git reset --soft` would undo the commits only; and from the other side of the spectrum, `git reset --hard` would reset the commits, the index, *and* the working directory) – ErikMD Sep 05 '21 at 22:42
-
The difference between mixed and soft is whether or not the changes will be staged after the reset. I don’t think you need to worry about that. – JBallin Sep 05 '21 at 22:42
-
1Ah indeed `git reset --mixed` seems to be what I want. I was being cautious as I have lost files before by doing stupid git commands.... Thanks both!! (The visualisation in this helped https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard ) – ru111 Sep 05 '21 at 22:45
-
Sure. Btw you can use git reflog to undo most mistakes you make with git. Also, mixed is the default. – JBallin Sep 05 '21 at 22:49
-
Actually, it didn't work... I did `git reset --mixed` but when I do `git status` it still says "Your branch is ahead of 'origin/master' by 7 commits." I want this number to be 0, haha.. – ru111 Sep 05 '21 at 22:49
-
1BTW @ru111, some basic advice about the-risk-of-losing-changes and Git: for any Git repo and any kind of changes (a new file, some changes to an existing file, etc.), it is in practice impossible to lose changes **provided these changes have been committed at some point** (e.g., after doing `git reset --hard sth`, we can easily rollback thanks to the [git reflog](https://git-scm.com/docs/git-reflog) feature). But of course, if some changes to existing files have *not been committed*, `git reset --hard` would lose them. – ErikMD Sep 05 '21 at 22:50
-
@JBallin I use git log, but I'm not sure which commits I have to undo as git log has become pretty messy after git pull-ing in between my git commits that I want to undo.. hence looking for an easy way to just undo them all – ru111 Sep 05 '21 at 22:50
-
I meant reflog, typo. You didn’t include “origin/master” when you did your reset, so nothing happened. – JBallin Sep 05 '21 at 22:53
-
Thank you both, I understand better and `git reset origin/master` worked. But `git reset --hard origin/master` (first comment) would have deleted any committed changes from the repo right? Plus undo un-commited changes for good? If that is the case --hard would have been a disaster as my goal was to retain all changes I made, committed and uncommitted (I also had uncommitted updates), but just un-commit and un-add. So it might be better to edit the comment without the `--hard` for those coming to this page. – ru111 Sep 06 '21 at 20:38
-
Deleted the comment. I agree “hard” is a risky initial suggestion. You would’ve lost your local changes. Reflog would save you, but still. – JBallin Sep 06 '21 at 22:42
1 Answers
2
To have your local branch point to the same commit as your remote branch, while maintaining your changes - use git reset:
git reset origin/master
The result will be that git status
shows you are up to date with master and that you have unstaged changes.

JBallin
- 8,481
- 4
- 46
- 51
-
It seems there is a typo here: the command should read `git reset origin/master` – ErikMD Sep 05 '21 at 22:53
-
1Also, to remain branchname agnostic if needed in a script or an alias for example, one can use `git reset @{u}` (short for `@{upstream}`) which will substitute `@{u}` with whatever remote ref is set for the current branch to pull from. – Romain Valeri Sep 06 '21 at 06:05