Difference between creating a branch and doing a soft reset? Best way to go back to an old working version?

Say the history of my commits is A - B - C and I have only this branch.

B was fully working. I started adding some functionality in C, but it's not working so I need to go back to B, but I also want to retain the code I wrote in C because I will want to review it and fix it later. What is the best way to do it?

Is the best way to create a new branch starting from B?

What is the difference between that and doing a soft reset? I understand a soft reset doesn't delete the changes (is that correct?) but it's not clear to me how to restore those changes (the code in C), nor what the difference between a soft reset and creating a branch is.

Aside

Git just seems needlessly arcane and obscure. I mean, the official docs define push as:

https://git-scm.com/docs/git-push

git-push - Update remote refs along with associated objects

I am sure it is technically correct, but it is hardly the most user-friendly explanation. Could they have added a comment explaining it uploads the local repository to the remote one, or something like that?

How can I restore a previous version of my project?, The fastest way to restore an old version is to use the "reset" command: comes with a couple of options, one of the more interesting ones being the "--soft" flag. of changes in your working copy and can then decide what to do with them. can simply drag & drop the commit you want to return to and create a new branch :� This is useful for quickly inspecting an old version of your project. However, since there is no branch reference to the current HEAD, this puts you in a detached HEAD state. This can be dangerous if you start adding new commits because there will be no way to get back to them after you switch to another branch.

If you want to keep the commit C as a commit and work from B again, you will need a new branch. I would suggest doing a new branch from C and hard-resetting master (or whatever your main working branch is) to B.

You will then be left with this (I added a new commit for clarity):

D      master
| C    review-c branch
|/
B
|
A

A soft reset to B will remove the commit C and stage the changes you made in C (same as if you made the changes for C yourself and did a git add of them).

git reset soft: When to Use Git Reset, Git Revert & Git Checkout, Use this to return the entire working tree to the last committed state. Scenario: Image that you did git push in hotfix branch for commits you didn't want to make yet. Solution: The safest way to fix this is by reverting your changes since it Everything that was changed in the old commit will be reverted with� There are diferences with a hard reset and soft reset, hard reset (3+*+Call) erases everything from the phone, messages, images, videos, contacts, installed apps, etc, etc, the soft reset (*#7370#) does the same thing apart from it also resets the wallet code (if the phone contains a wallet) and erases it's contents, it also resets the phone securety code back to the Nokia default of 12345.

In your case, I suggest that 1) you create a branch or a tag at C so that you can review it when you want to, and 2) reset your branch to B so that the changes of C are exluded from the branch.

# create a branch from C
git branch foo C

# create a tag at C
git branch bar C

# reset your branch to B
git checkout <your_branch>
git reset B --hard

# review C
git show foo
git show bar

To some degree, a tag is more stable than a branch. foo may move to another commit by an accidential command. bar always points at C unless you intend to move it to another commit. bar is considered as an alias of C.

Doing a hard reset, git reset --hard HEAD~3 # Go back in time, throwing away changes $ git reset to an earlier commit $ git reset --hard # Wipe out differences in the working tree current branch using reset --soft and reset --hard (which changes the working on a branch, and then move that branch over to take the place of your old master. As the name suggests, reset function means reset to factory settings and solution of software problems. However, Android has two reset concepts: soft and hard reset. In this article, we will tell the difference between hard reset and soft reset Android. Soft reset. Soft reset also called reboot.

<rant-adressing on>

You have awoken the Ancients. Be very afraid.

<rant-adressing off>

;-)


For the problem at hand, if you want to keep changes in C for later but keep on working from the B state which is better, you have a number of ways to do that. I'd do a new branch* and reset the old one.

branch creation / hard reset method

# create your backup branch for these failed changes
git checkout -b to-be-reviewed

# take your branch to its previous state
git checkout -
git reset --hard HEAD^

But you could also, as you considered, use git reset --soft. If you want to compare uses, here's how you could have proceeded with it :

branch creation / soft reset method

# undo last commit (C) but keep the changes in the working tree
git reset --soft HEAD^

# create a new branch and commit on it
git checkout -b to-be-reviewed
git commit -m "Your message"

Then your original branch points at B and to-be-reviewed has all recent (though non-working) changes.


Lastly, this is a use-case for git stash :

no new branch / stash method

# reset your branch to state B
git reset --soft HEAD^

# stash your changes with a title for easier reuse
git stash save "Failed changes XYZ"

And at this point you can later inspect this stash with git stash list / git stash show.

* (As ElpieKay suggests very appropriately, tags could be considered instead of branches for this use. Overall reasonning is the same when it comes to resets anyway)

Resetting, Checking Out & Reverting, Compare the most common configurations of git reset, git checkout, and git revert. Checkout and reset are generally used for making local or private 'undos'. git checkout, Commit-level, Switch between branches or inspect old snapshots -- soft – The staged snapshot and working directory are not altered in any way. From that state, you can turn it into a "real" branch that you can go back to again by using git checkout -b new-branch-name. The book Pragmatic Version Control Using Git is good at explaining Git in simple terms. – NightOwl888 Nov 14 '16 at 19:53

Git Reset, git revert and git reset undo a committed snapshot. git checkout checks out files, The best way to demonstrate these mechanisms is to create a changeset in a To get started we will create a new repository with the commands below: git reset --soft $ git status On branch master Changes to be committed: (use "git reset � This helps to clearly illustrate what --soft has done. As with all git reset invocations, the first action reset takes is to reset the commit tree. Our previous examples with --hard and --mixed have both been against the HEAD and have not moved the Commit Tree back in time. During a soft reset, this is all that happens.

git-reset Documentation, Localized versions of git-reset manual See "Reset, restore and revert" in git[1] for the differences between the three Undo a commit, making it a topic branch working tree back to that state, and resets the tip of the branch to that commit. git switch feature $ git reset --soft HEAD^ ;# go back to WIP state (2) $ git reset (3) . On a laptop (or even computer) you can go to hibernation mode (memory gets written to disk then powers down). When turned on, it loads back up from memory and thus everything that was loaded in memory before shutting down is put back. That is what a Soft reset is (when you do the power button the slide to turn off).

How to reset, revert, and return to previous states in Git , Undo changes in a repository with the simplicity and elegance of Git commands. appreciated) aspects of working with Git is how easy it is to get back to where Where the reset command moves the branch pointer back in the chain created as a result of making the changes from the originals "on top of"� The git reset command also includes options to update the other parts of your local environment with the contents of the commit where you end up.These options include: hard to reset the commit being pointed to in the repository, populate the working directory with the contents of the commit, and reset the staging area; soft to only reset the pointer in the repository; and mixed (the default

Comments
  • Push I don’t think has anything to do with what you want.
  • It doesn't. Mine was just an example of how needlessly obscure the git documentation is. It's like Sheldon Cooper of the Big Bang Theory going to the checkout in a shop and quoting the price in binary - WHY?
  • Haha. Yeah. I only started reading the docs one I understood the terms within it. No easy to get started with.
  • One of my best professors at uni always said: beware of those who try to dumb down very complex concepts, but also beware of complexity for its own sake: those who cannot explain a simple concept in a simple way either want to show off or do not really understand the concept themselves!
  • Wow, thanks so much! I don't have the time to go over this now, but will read this with keen interest!
  • What is the difference between 1) doing a new branch from C and hard-resetting master to B and 2) doing a new branch from B?
  • The only difference is where the master branch will be after doing it but that is entirely arbitrary. I chose this option because you said you want to review the code in C later which I understood as wanting to keep the code around but not in the branch you are developing on.
  • With a soft reset the branch becomes A-B-C-B , right? This much I think I sort of get. What I don't get is: after a soft reset, where do the changes in C go? In the staging area? But won't there be a conflict when I start to make new changes and will have to put those new changes in the staging area, too?
  • No. With a (soft or hard, whatever) reset your branch goes A-B. Don't you confuse reset and revert :-) After a soft reset the changes go in the working tree, and the staging area.
  • So I do a soft reset and the changes which I have undone are in staging. But, as I continue to work, my new changes will also go in the staging area. Isn't there a conflict between new changes and old changes?
  • @Pythonistaanonymous No, conflicts happen only upon merge resolution (and similar siuations, like cherry-pick, which in fact uses a merge behind the scenes), in some cases. If you're not trying to merge something but just modify the files in your working tree, no reason for a conflict. And if you want to avoid mixing the first set of changes and the new things you're modifying, just commit in-between.