Are Git Commits that are not part of a branch pushed?

Viewed 107

Today I was working on my project, and I tried a new algorithm that didn't work well. Thus, I don't want to include it in the project, but I'd like to have a commit with the code in the repository just in case I want to refer back to it later. Here is what I did:

git add .

git commit -m "Lorem ipsum..."

Wrote down the commit hash in my notebook with some notes.

git reset HEAD^

So now, that commit is not part of the branch history, but if, in the future, I need this commit, I can simply check it out by using the commit hash. But here is my question:

Will this commit ever be pushed to the remote? Or will it stay only on my local machine forever? Obviously, this would be bad, because if I ever have to move machines, etc., I will lose this commit when I re-clone.

2 Answers

No, that commit will not be pushed. Only "active" commits on the branch you are pushing will make it to the remote repo.

My 2 suggestions are already in the comments:

  1. Create a separate branch. I prefer the naming convention user/first.last/some-branch which can be your personal backup of commits you (or someone else) may want to look at in the future. I typically have a bunch of these types of branches and I will purge out the garbage every few months.
  2. Commit the change, revert the change, and then when you push the history is preserved on your branch.

I nearly always prefer #1 over #2.

Note there are ways to push commits that aren't associated with a branch, but I don't recommend it because they are more difficult to find later, and may eventually be garbage collected if there is no branch or tag pointing to it. (See this comment from torek for more details.)

Related