1

As you can see I'm a beginner and I have messed up.

$ git log
commit 1b33ea845ab32112dd36b835e54f136d3b8b8dbb (HEAD -> master, upstream/master, version1)
Author: Dhruv Gami <>
Date:   Wed Feb 5 14:55:00 2020 -0600

    Added Project modifications for units

    (cherry picked from commit b7b34e1b645dd43fcc78a7f2e639fa654d2f82fc)

commit 23c39347ba985969420bd7f5236efe43fdea1be7
Author: Dhruv Gami <>
Date:   Mon Feb 3 17:28:56 2020 -0600

    Changed num to label to write aux file

    (cherry picked from commit b7b34e1b645dd43fcc78a7f2e639fa654d2f82fc)

commit c5e8bf7bca100fb9f63eea6c7163072527357cd8
Author: Dhruv Gami <>
Date:   Fri Jan 31 14:22:11 2020 -0600

    Added output_date to download file to correct folder at the end of month

    (cherry picked from commit b7b34e1b645dd43fcc78a7f2e639fa654d2f82fc)

commit b7b34e1b645dd43fcc78a7f2e639fa654d2f82fc (origin/master, origin/HEAD)
Author: rip1217 <1000rip@noreply.com>
Date:   Mon Jan 27 10:44:28 2020 -0600

    Maintenance code addition

I have got this in my log and I would like to undo the last 3 changes in upstream and get back to commit b7b34e1b645dd43fcc78a7f2e639fa654d2f82fc (origin/master, origin/HEAD). Basically, start over again from Maintenance code addition in upstream repository. It is Ok if there is no way but to lose changes locally to revert to original in upstream. The changes are in python files if that information is required.

Thanks for your help!

dhruv gami
  • 37
  • 6
  • 1
    Does this answer your question? [How do I undo the most recent local commits in Git?](https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git) – Synoli Feb 06 '20 at 20:06
  • "It is Ok if there is no way but to lose changes locally" Both are possible, but what is the prefered outcome for your needs? Keeping your changes (to inspect/modify/recommit them) or get rid of them entirely? – Romain Valeri Feb 06 '20 at 20:49
  • Preferred to recommit them later. @Synoli - I looked at the answers in the post but somehow I'm unable to make changes to upstream repository on github. I was able to make changes locally. – dhruv gami Feb 06 '20 at 21:01
  • @dhruvgami If you run `git reset b7b34e1`, you should end up at the desired commit. Your changes will be preserved as unstaged modifications so you can recommit them later. – Synoli Feb 06 '20 at 21:25
  • Do you want to move your local `master` to commit b7b34e1? Or move `upstream/master` to b7b34e1? Or both? – Schwern Feb 06 '20 at 21:31
  • Preferably just move upstream/master to b7b34e1 – dhruv gami Feb 06 '20 at 21:37

2 Answers2

0

Push origin/master to upstream's master.

Before
A - B - C [origin/master]
         \
          D - E - F [master]
                    [upstream/master]

# git push --force-with-lease <remote> +<source branch>:<destination branch>
git push --force-with-lease upstream +origin/master:master

After
          [upstream/master]
A - B - C [origin/master]
         \
          D - E - F [master]

+origin/master:master says to push your origin/master to upstream's master. See <refspec> in git-push for more.

Because your branch is not a descendant of upstream/master Git will not normally let you do this. You will need to force it. Use --force-with-lease.

Schwern
  • 153,029
  • 25
  • 195
  • 336
0

To move the upstream/master to commit b7b34e1b645dd43fcc78a7f2e639fa654d2f82fc you need to do the following.

Reset your work to that very commit, using its hash.

git reset --soft b7b34

-- soft flag is needed to preserve changes from your previous commits.

At this point you may try git log and git status commands to look around: make sure reset is done and previous changes are staged.

Next step is of yours is to forcefully push to the upstream/master in order to reset it to the commit you are currently on locally.

git push --force

However, be warned not to destroy somebody's work, that would be possible if someone had already pushed to the upstream/master after the b7b34 commit.

Yevgen
  • 1,576
  • 1
  • 15
  • 17
  • 1
    `git push --force-with-lease` will prevent you from destroying somebody's work. There's no need for `--soft` in this case unless you have uncommitted work. It's messy to do Git surgery with uncommitted changes. Stash them or commit them as a WIP and amend them later. It's safer and simpler. – Schwern Feb 07 '20 at 17:45