-1

I have been using git incorrectly I believe, I have been staging and committing then when I have made mistakes, I have been checking out a previous version and repeating the process, staging and committing, this has been working fine until I have tried to push my project to Github.

for example: commit #10 I made a mistake so I checked out to commit #6 and carried on now I am at say commit #20 and I want to push my repository to github

git status gives me

HEAD detached from 0ade9cbf
nothing to commit, working tree clean

I am asuming 0ade9cbf is the master I should have been on.

git log --oneline
d360cf5f (HEAD) App still works updated dependancies and fixed a few errors need to update database
0ade9cbf (origin/main, my-temporary-work) This was the last commit before moving to remote repo

my problem lies in when i want to push to github i get the following

git push
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

git push origin HEAD:<name-of-remote-branch>

Am i correct in thinking because I have been checking out to previous versions I have lost the master way back and i should

git checkout master
git merge d360cf5f

to bring the master up to the head ?

Then I can branch the repository work on it if i'm happy with it merge with the master ?

I have just read Ry's Git tutorial, probably should have read this first.

Andy Cass
  • 398
  • 6
  • 16

3 Answers3

2

You have indeed been using Git incorrectly. Let's talk about what the right way to work was. Your situation is a very reasonable one and comes up all the time:

  1. Work on a branch.

  2. Add-and-commit, Add-and-commit, Add-and-commit...

  3. Oh darn, that was a dead end, let's go back 3 commits and try a different way. But how?

After all, this is exactly why you're using Git, so you can do just this kind of thing! So how should you do it?

One very easy correct approach is:

  1. Do what you did: checkout the last "good" commit.

  2. Immediately, make a new branch and get onto it (git branch branch2, git checkout branch2).

  3. Resume working (step 2 above).

Alternatively, reverse 5 and 6; that's okay too!

  1. Do what you did: checkout the last "good" commit.

  2. Resume working (step 2 above).

  3. Come to another dead end? Or, ready to push? Either way, now we need a real branch. Make a new branch and get onto it (git branch branch2, git checkout branch2).

Just keep doing that sort of thing as you work. Commit early and often! When you are all done, you can "clean up" the unused dead ends and rename whatever branch you end up on, if you like. Then push. Cleaning up your local history before pushing is very reasonable and is good practice.

The point is, do not checkout the last the good commit (4) and fail sooner or later to make a branch and get on it (5/6), or you will still be in "detached head" state — as Git itself warns you!

humlet:testinggit matt$ git checkout 9dfa3
Note: checking out '9dfa3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

You have been ignoring what Git says, and that's not a good habit.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

You are in a state of detached HEAD. This means that your HEAD pointer is pointing at commit d360cf5f, but no branch is pointing at that commit. If you want your master branch to point to that commit, all you need to do is:

# Go to master
git checkout master

# Force master to point to commit d360cf5f
git reset --hard d360cf5f

Keep in mind that git reset --hard is a dangerous operation and should be used with care.

In your case, you dont have to merge masterwith yourd360cf5f` commit. Resetting would do the job.

Once you do this, you would be able to push to your remote.


Also, since you mentioned about your git workflow, yes what you are doing is somewhat unnecessary. You have the power of branches that can keep track of your changes.

Even if you need to go back in time and go in a different direction, you don't have to do it in a Detached HEAD state. You just create a branch from where you want to and then continue working. See here for this method.

mnestorov
  • 4,116
  • 2
  • 14
  • 24
-1

In this situation you cannot push 0ade9cbf, because currently it is only a specific commit on your history of commits in master, which you have accesed via git checkout and maybe modified.

You have two options to push your current version d360cf5f onto a remote:

a) Set it as newest commit on the master branch and push into master remote:

git branch <some name> 
git checkout master
git merge -X theirs <some name>
git commit -m "<some message>"
git push

b) Open up new Remote branch and push it there:

git branch <some name>
git checkout <some name>
git commit -m "<some message>"
git push --set upstream <some_remote> <some name>
CWebkas
  • 19
  • 1