B
B
BogBel2015-11-29 21:50:41
git
BogBel, 2015-11-29 21:50:41

Will data be removed from branch if I do git reset --hard?

Good evening. I'm not very good at Git, so I ask you not to throw slippers.
There is a branch origin master , it has one commit during initialization.
There is a task1 branch, 5 commits were made in it, after which there was an unsuccessful merge into the main branch.
It is necessary to roll back the master branch to its initial state, so that the task1 branch remains intact and unharmed.
The commit to which you need to roll back was before all subsequent commits in the task1 branch.
I found the git reset --hard command, but as far as I understand, with such a command, the entire repository will be rolled back to its initial state and the task1 branch will go into oblivion.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Deyashkin, 2015-12-03
@Lobotomist

git reset only affects the current branch. The branch is what? A pointer to some commit. git reset moves that pointer. At the same time, depending on the options (--hard, --soft, --mixed), it works differently with files in the working directory. If --hard is used, the pointer is transferred and the state of the working directory is also changed and becomes what it is in the target commit.
So in your case you need:

# Откат коммита слияния в мастере

#перейти в мастер
git checkout master
# перенести указатель мастера на нужный коммит
git reset --hard <первый коммит мастера>
# запушить корректный мастер в origin
git push -f origin master

# Теперь откат ненужных коммитов в ветке task1

# перейти в ветку task1
git checkout task1
# перенести указатель ветки на нужный коммит
git reset --hard <good commit>

Since you actually removed some of the commits from the master when pushing, you need the --forced (-f) switch to confirm that you did it consciously.
For peace of mind, put a tag on the merge commit. So you can always return to it, even if you do something wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question