Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+14 votes

Hi yall, I'm finishing my Step 7, adding a new choice of color in the ComboBox, I'm the latest to do this in my group as they have already finished. So when I try to commit and push it, it fails, therefore I try to pull the changes from the Github, but then I gets to another window with three options(I don't remember the last one but there are "Rebase" and "Cancel", I chose Rebase and it asks me to Accept Yours or Accept My codes, I chose to Accept My codes, I messed up and then now my code is the latest version for the team repo. I cannot pull changes anymore as it appear to be upto date to all of them, I know it is kind of late but does anyone know how to redo some of my latest commits. Thank you

asked in CSC305 Fall 2023 by (617 points)

2 Answers

+7 votes

There are several different ways to go back in time with git.

1) git reset (Josh's answer) will allow you to rollback changes to go to a point in your git history, and pretend as if the later changes had never happened. Thus, it's dangerous, because if you're not careful, you can lose code/changes forever, and mess up your teammates and your team repo pretty badly.

This approach could work if you haven't already pushed your changes (rebased commits) to GitHub, and you're just trying to fix something you did wrong in your local repo.

2) The generally "safer" option is to use git revert to revert changes in a safe/logged way, where it creates a NEW commit that undoes the changes of a previous commit. That way the whole history of what happened is preserved.

To undo the most recent single commit, git revert HEAD is usually sufficient

To revert the three most-recent commits, you can do

 git revert HEAD HEAD~1 HEAD~2 

However, you might also want to use git log to find the identifiers (long string of unique characters) that identifies the commits that you want to "undo", and do:

 git revert id1 id2 

Note that you are undoing the changes represented by each commit, not rolling back to the state after that commit!

For more info, read: https://www.freecodecamp.org/news/git-revert-commit-how-to-undo-the-last-commit/

answered by (508 points)
+6 votes
  1. Use 'git log' to see the commit history, and find the commit hash of the last commit you are confident is correct.
  2. Reset to the correct commit using 'git reset --hard '
  3. Pull changes using 'git pull'
  4. Manually apply the changes you made to the now updated codebase
  5. Commit and push the code.
answered by (2.1k points)
+1

git reset will allow you to rollback changes to go to a point in your git history, and pretend as if the later changes had never happened. Thus, it's dangerous, because if you're not careful, you can lose code/changes forever, and mess up your teammates and your team repo pretty badly.

This approach could work if you haven't already pushed your changes (rebased commits) to GitHub, and you're just trying to fix things in your local repo.

However, once you've pushed your changes to GitHub, they are public, and your team members could have pulled them and be building their own changes based on that git history.

If you've already pushed, and then you git reset --hard, then git won't let you push your "rollback" changes up to GitHub unless you do a --force push, which is dangerous, because it overwrites the history up on GitHub, and could cause valuable code changes (including things your teammates just committed/pushed) to be lost forever.

Rule of thumb for this class: DO NOT use --force push unless you have Stonedahl's express permission!

Thus, it's safer to use git revert to revert changes in a safe/logged way, rather than trying to roll back with reset.

...