1

So I am using git and github. I made some poor commits and pushed them to github. I then reverted back to the most recent good commit, and started working from that branch. I did this using a command like

git checkout last_good_commit_hash

I checked out to a commit, not to a branch that I had made myself.

I then changed a lot of code, added a lot of good commits, and decided it was time to push to github. However, the github repo was already up to date, because the master branch is not my current branch.

When I run git branch I get this output:

* (detached from 0c42801)
  master

I'd love to use a git command like the one from this stackoverflow question

git merge --strategy=ours master    # keep the content of this branch, but record a merge
git checkout master
git merge better_branch             # fast-forward master up to the merge

However, I don't know the name of the better branch, because it seems to be named (detached from 0c42801)

How can I get my correct branch to become the master/head and push it to github?

Community
  • 1
  • 1
johncorser
  • 9,262
  • 17
  • 57
  • 102

1 Answers1

2

I think you should be able to name your detached branch and then merge it

git checkout -b my-new-awesome-branch git checkout master git merge my-new-awesome-branch

You might also want to consider, in the future, not just doing everything in the master branch and only merging things to master when you feel they've reached some kind of good state. A typical workflow is to branch off of master to work on some new elements/features and then to merge back to master only after you feel things have gotten to a good state or after they have been reviewed by someone else.

If you want to take the route suggested in the previous paragraph, you can keep your work in this separate branch until you're ready to integrate it into master and keep the branch in your remote repository as well by doing the following:

git checkout -b my-new-awesome-branch to get that branch named, and then

git push origin my-new-awesome-branch to keep the history on this new branch in the remote as well.

wf-akgill
  • 121
  • 1
  • 6