I know this has been discussed quite a lot here. I am absolutely new to git and its branching complexity. Although I went though several threads I am not comfortable with the terminologies used in them. So I am looking for some easy to understand step by step help on this. I have tried to explain the actual scenario I have and what I am looking for.
I have aa branch called css-merge-2016
where I was pushing my commits since last few days. Now I need to revert back to a specific commit. I don't want to keep any changes I made in my working directory either. If everything comes back to where I was before the specific commit that would serve my purpose.
I did git checkout <commit_number>
, which shows me I am on a DETACHED HEAD
. Then I entered the git checkout css-merge-2016
, which is the branch I am using to push my changes. But when I run git branch it is still showing the following:
subrara@subrara-desktop:/var/www/localsite.com/local.main.drupal$ git branch
* (detached from f996a78)
css-merge-2016
master
I checked again with git checkout <commitnumber>
which is not showing me the DETACHED HEAD
message any more but saying
HEAD is now at f996a78... Removed unused SCSS file from customtheme/sass/pages Deleted file: pages > _home.scss Rules are merged down to base/sass/all/pages/_home.scss
I need all commits I did after f996a78
get discarded and my local repo gets back to f996a78
. Is this at all possible? If yes, how? Please suggest. I am completely stuck!
UPDATE:
If I run git status
at this stage I can see
HEAD detached at f996a78
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
and then a long list of red colored file list of which some are marked as modified
and some as deleted
.
When you checked out commit f996a78
, you did so temporarily in a detached head state. This means that your css-merge-2016
branch remained unchanged, with only the pointer to the tip of the branch changing. If you really want to get rid of the commits occurring after commit f996a78
, then you can use git reset --hard
:
git reset --hard f996a78
Keep in mind that this will potentially cause the commits occurring after f996a78
to be deleted at some point, and these commits will immediately no longer be in your branch.
You should also take note that if you want to push your branch to the remote, you will now have to do a force push, eg
git push --force origin css-merge-2016
because you rewrote the history of that branch. If the css-merge-2016
branch be shared by anyone besides yourself, you might want to revert the commits in query instead using git revert
.
you may try
git reset --hard f996a78
This will restore your working directory to the desired state.
Part of your question is about the "branching complexity" of git
and about the fact that you are very new to git
.
The funny thing with git is that there basically is no "complexity". Sure, until you know the diverse commands, you don't really know what to do, but both the internal data structures as well as the actual commands are very "uncomplex" (read: dead simple) and straightforward, certainly in comparison to other systems.
If you read only one document about git
internals, then do read this one: http://gitready.com/beginner/2009/02/17/how-git-stores-your-data.html Try to really understand what it tells you (it is not hard, and not a lot of information at all). Get into the habit of drawing "commit trees" on whiteboards or paper. After this, all you need to know is how the individual git
commands modify the commit tree, and you're set for any future question.
If you have that page down, maybe add https://www.jayway.com/2013/03/03/git-is-a-purely-functional-data-structure/ .
...to your question. While the git reset ...
mentioned elsewhere certainly works, I prefer:
git branch css-merge-2016_backup css-merge-2016 # just in case...
git branch -f css-merge-2016 f996a78
git checkout css-merge-2016
Then you will let your branch (which is, literally nothing but a little "sticky note" pointing to a commit) point to that older commit. If you are 100% sure that you are done with your old stuff, you can delete your backup branch ( git branch -D
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.