-1

I'm new to Git and stuck in a situation whether to commit or not. So this might be a basic question. I tried to find a lot regarding this but did not find any answer specific to my concern. So asking here.

I have 15 commits for my branch. Now I have reset to my first commit because of one issue. Then I have made the changes which covered the changes of all commit.

Now my question is can I commit my changes directly? will it be saved on the top of the other commits? what will happen to the other commits and their history. Do I need to delete the history or not? Or I just have to commit the changes and my branch would be ready for merge? Or are there any extra step involved till I mark my final commit?

mkasberg
  • 16,022
  • 3
  • 42
  • 46
Amar Gadekar
  • 387
  • 4
  • 11
  • Please clarify how you reverted to your first commit. When talking about git, revert has special meaning because of `git revert`. But with 15 commits, I doubt that's what you actually did. – mkasberg Mar 03 '16 at 15:24
  • I use a sourcetree. In that I select the first commit. Right clicked on it and selected the option: reset current branch to this commit. – Amar Gadekar Mar 03 '16 at 15:25
  • You mean, `Reset current branch to this commit`? "reset" and "revert are two different things. – mkasberg Mar 03 '16 at 15:27
  • yeah edited the comment. Sorry for mistake. – Amar Gadekar Mar 03 '16 at 15:28
  • Read this out to learn how to fix your HEAD after `revert`: http://stackoverflow.com/questions/34519665/how-to-move-head-checkout-revert-reflog-reset/34519716#34519716 – CodeWizard Mar 03 '16 at 15:53
  • Hi codewizards, I have no head issue. Finally I got to know after reset to a secific commit. Now what I have to do is that, I have to save local changes to stash, then I have to revert all the commits till specific commit. Then I can commit the new one using stash changes. Then I would be able to merge. Thanks a lot for your effort codeWizard and mkasberg. Appreciate it. Finally my concern is resolved. – Amar Gadekar Mar 03 '16 at 16:24

2 Answers2

2

When you reset a branch it moves the branch pointer without changing your commits. You go from this:

first commit                       branch name
|                                   |
* -- * -- * -- * -- * ... * -- * -- *

to this:

branch name
first commit
|
* -- * -- * -- * -- * -- * -- * -- * ...

All your other commits are still there, but you're not based off of them anymore. You can commit your changes. It will make a new, separate commit based on your first commit. Like this:

first commit
|
* -- * -- * -- * -- * -- * -- * -- * ...
 \ branch name
  \|
   *
mkasberg
  • 16,022
  • 3
  • 42
  • 46
  • Thanks for the answer. It was helpful. So voted it. But this commit will be on the top of the other commits? and what about history of other commits? – Amar Gadekar Mar 03 '16 at 15:32
  • No, this commit will be on top of the first commit, the one that is currently in your working directory. The other commits are still there, but are not easily accessible since you don't have any branches or tags pointing to them. I'm not that familiar with source tree, but I know that you could find them using git-reflog. You might need to use git-cherry-pick to sort things out. It's still not clear exactly what you're trying to do. Do you want to "edit" your first commit while keeping the other 14? – mkasberg Mar 03 '16 at 15:35
  • I have reset to my first commit and made the changes which covers all of the 14 commits information. So ideally I will not require those 14 commits now. I have sorted out the issue. I want to commit this so that I can proceed with the merge. But before merge want to clarify these things first. So that I can proceed. – Amar Gadekar Mar 03 '16 at 15:41
  • If you don't need those 14 commits, then there's nothing to merge. Just run a normal git commit and you'll be in the situation I described in my answer above (at the bottom). – mkasberg Mar 03 '16 at 15:44
  • No. you dint get me correctly. Currently i'm working on my local branch this needs to be merged with develop branch now. So I think there was one wrong commit because of which I was not able to merge my branch with develop branch. As the issue has been sorted out now I'm in a situation where this commit will lead me directly to the merge activity. Or do I have to follow some other steps before committing changes? something like fetch pull etc. – Amar Gadekar Mar 03 '16 at 15:47
0

Based on your comments in mkasberg's answer, it sounds like the commits that you reset away from have already been pushed to a remote repository. If this is the case, then resetting to an earlier commit was not the right way to go about fixing your problem.

I would recommend saving a copy of all your files in their current state to a location outside of your working copy. Then, reset your working copy so all of those 15 commits are included again. Then overwrite your working copy with the backup you made, and commit that.

thelr
  • 1,134
  • 11
  • 30