git branching – how to make current master a branch and then revert master back to previous version?
Suppose I currently have no branches other than
master, but now I’ve made some changes since my last commit that I’ve decided I don’t want to keep (note: the changes are not committed yet). I don’t want to get rid of these changes just yet though – I’d like to put them in their own branch (called e.g.
experimental_stuff) and then continue development from my previous commit. So I guess the steps are:
- Git branch diverged
- Can I delete old git branches
- Git HEAD referring to branch vs to commit
- Git, tell me when a remote branch that I am tracking is removed
- How to rename a git branch locally and remotely?
- Can I pull only certain files from another git repository?
- make current master a branch (
git branch experimental_stuff?)
- go back to previous commit (
git checkout <last_commit>?)
- make this my new master branch so that future commits continue from here (
Is this the right approach and what git command do I need for the last part (if any) ?
[Note: this is just a local git repository for my sole use, if that make any difference.]
2 Solutions collect form web for “git branching – how to make current master a branch and then revert master back to previous version?”
you are almost done.
Assume you have made a commit on your development files. Then..
git branch experimental_stuff
git reset –hard HEAD^ (go back one previous commit of your master branch to continue your development)
Assume you have not made a commit on your development files. Then..
you need to save your current changes to a temporary directory
git checkout -b experimental_stuff (create and change branch to experiental_stuff)
git stash pop (populate the temporary directory into experimental branch)
git checkout master (return back to master, and no need to go back the previous commit this time as you don’t have that commit)
Suppose I currently have no branches as such
Just to correct you: You always have at least one branch in Git. Even master is a branch, with the same features as any other branches. And Git doesn’t handle a branch named master different than another branch. It is just a convention, that most developers name their main branch “master”, but you don’t need to. You could even delete your master branch and create branches like development, release etc. For your question itself: The answer of Kit Ho is good.