2

I have a branch in my repository with a new commit (say 'Commit A') regarding a new change. And I didn't push the commit yet to the remote repository.

Now, one fix is missing from this commit and I would like to:

  • git reset HEAD~1 (come back to the previous commit A-1, putting back the last committed changes in the working directory)
  • change the files for the missing fix
  • git add .
  • git commit (say 'Commit B')
  • git push

My question is mainly regarding the reset commit (A).

  • will it stay around as detached commit? Or will it still be in my branch?
  • will this push the 'Commit A' to the remote repository in any way?
  • will it mess my history? Or git push will just sync the commit into the branch, ignoring the detached ones? (that is 'Commit A')

And finally

  • what if instead I had 'Commit A' already pushed and try to do the same sequence of operations?
Kamafeather
  • 8,663
  • 14
  • 69
  • 99

3 Answers3

1

will it stay around as detached commit? Or will it still be in my branch?

If you were in a detached HEAD mode, then any reset would still be in a detached HEAD.
If you are not in a detached HEAD, then the git reset reset the HEAD of your branch to commitB, which means:

-commitA is either in another branch (see "How to list branches that contain a given commit?": git branch --contain commitA) - or it is visible for a time in the git reflog

will this push the 'Commit A' to the remote repository in any way?

No, unless commitA is accessible, part of another branch and if you are pushing that other branch.

will it mess my history?

Not your local history. It could mess the history of the upstream repo only if commitA was already pushed.

At one point, you need to:

  • either git checkout aBranch
  • or create a branch from where you are (or reset to) git checkout -b aNewBranch.

You need to be in a branch in order to push.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yes, but I **am** in a branch. From my branch I `git reset` and my *HEAD* will still point to the branch (even if it will point to one of its previous commits). The only detached thing is the *'Commit A'*, cause it is no more into my branch history (indeed I 'came back' and 'chose another path' (reset + commit). But maybe *'detached'* is not the right term for describing the commit state? – Kamafeather Sep 10 '14 at 10:35
1

If you don't do git push nothing will end up in a remote repository.

Git reset defaults to 'mixed' mode thus your command will be equivalent to 'git reset --mixed HEAD~1'. From git docs it says the following about mixed mode reset:

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

You can also, instead of resetting, amend you commit using 'git commit --amend' command. This will amend your last commit with the changes you would like to add.

markovuksanovic
  • 15,676
  • 13
  • 46
  • 57
  • It doesn't answer my questions about the *detached/limbo* state of the reset commits. And I know that I won't influence the remote repository if I don't push. But +1 for pointing me to `git amend`; I didn't think about it. – Kamafeather Sep 10 '14 at 10:45
  • @Kamafeather The commits that were made after the commit to which you reset are not removed. They will exits but will be orphaned. They can be found using 'git fsck --lost-found' command. Is that what you were wondering? – markovuksanovic Sep 10 '14 at 10:55
  • Yes, that's it. I wanted to be sure that resetting locally won't keep the reset commits also remotely. Thanks – Kamafeather Sep 10 '14 at 19:19
1

regarding the reset commit (A).

will it stay around as detached commit? Or will it still be in my branch?

It will stay around as detached commit (not reachable from your branch), you can reach it by doing

git reflog

That command will list the history of all commits pointed by HEAD, in this case you will see something like this:

af35532 HEAD@{0}: commit: Commit B
bae2395 HEAD@{1}: reset: moving to HEAD~
6685f32 HEAD@{2}: commit: Commit A

So, you can restore your branch to the previous state (pointing to Commit A), this way:

git reset HEAD@{2}

will this push the 'Commit A' to the remote repository in any way?

NO, since push only sends the heads (a.k.a. branches) and only commits reachables from these heads

Since Commit A is not reachable from your branch, it never would be sent. Note that if there is another different branch making Commit A reachable, then commit A will be sent along with that branch

will it mess my history? Or git push will just sync the commit into the branch, ignoring the detached ones? (that is 'Commit A')

No, it will not mess your history, git push will not sync the detached commit

what if instead I had 'Commit A' already pushed and try to do the same sequence of operations?

In this case, your push would be rejected as "non fast-forward", because you are trying to overwrite a remote head with divegent history

To https://github.com/user/repo.git
! [rejected]        master -> master (non-fast-forward)
dseminara
  • 11,665
  • 2
  • 20
  • 22