-1

I have commited a lot of things i don't want to use, and have used

git checkout ""

to get back to the one i want to be at. How do I delete the other ones? And how do I get out of detached HEAD? I have pushed all the commits,and I guess that makes it harder regarding what I already have seen of solutions.

Lk97
  • 1
  • 1
  • You may "delete" the ones you don't need, by force pushing your current state (if its the one you want) ... or to let the git history unchanged revert all the unneeded commits. – YesThatIsMyName Oct 17 '19 at 07:11
  • 1
    Possible duplicate of [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – JP. Aulet Oct 17 '19 at 07:12
  • Has _anyone_ else pulled/seen your branch? We need to know this in order to suggest something to you. – Tim Biegeleisen Oct 17 '19 at 07:16

4 Answers4

0

You can push the all your submit, Then back to any version you want via head, And git branch a new branch, Finally you can push the version to new branch .

Lux
  • 124
  • 1
  • 9
  • Do I have to make a new branch? What happens to the other commits when I make a new branch? I commited and pushed at master – Lk97 Oct 17 '19 at 07:32
0

Of course it’s a optional way. You can merge the new branch code to master with your other commits when you done the version you want now.

Lux
  • 124
  • 1
  • 9
  • Please use the [edit](https://stackoverflow.com/posts/58426884/edit) link under your [first answer](https://stackoverflow.com/a/58426884/2745495) to update it, instead of posting an almost similar answer. If you are replying instead to a comment (like the OP's comment on your first answer), either post the reply as a comment or edit your answer. – Gino Mempin Oct 17 '19 at 09:31
0

If you pushed on master branch : You can revert your changes by adding a new commit, containing reverse diff of your changes, i.e using git revert <unwanted_commit_number>

If you pushed on a "work in progress branch" :

  • You can use revert as above (history-safe)
  • You can amend your last commit to add/remove some changes, using git commit --amend and force push (/!\ rewrite history)
  • You can remove your commit, i.e using interactive rebase git rebase -i <base_commit_number>^. Remove your unwanted commits in interactive window and force push (/!\ rewrite history)
  • You can move back to a previous state, using git reset ---hard <commit_number> and force push (/!\ rewrite history)

Get out of detached HEAD state :

  • If you come from a checkout like git checkout <commit_number> or git checkout remote/some_branch, you can cretae a new branch using git branch <branch_name>

  • If not, you may have a merge or a rebase, which is in progress. Using git merge --abort or git rebase --abort to move back to previous branch

Rey0bs
  • 1,192
  • 12
  • 19
0

Here's one option:

// tell those impacted not use name_of_old_branch 
git checkout -b feature/yeah1 // create a new branch from where you are
git push // push new branch
// tell those impacted to use new branch
// you can stop here and enjoy life
git push -d origin name_of_old_branch // delete remote branch
git branch -d name_of_old_branch  // delete local
//enjoy life

Please note that really deleting things in git takes more work.

tymtam
  • 31,798
  • 8
  • 86
  • 126