web-dev-qa-db-fra.com

Comment résoudre le fait de s’engager sur la mauvaise branche Git?

Je viens de faire un très bon engagement dans la mauvaise branche. Comment puis-je annuler le dernier commit dans ma branche maître puis prendre ces mêmes modifications pour les intégrer à ma branche de mise à niveau?

563
mikewilliamson

Si vous n'avez pas encore poussé vos modifications, vous pouvez également effectuer une réinitialisation logicielle:

git reset --soft HEAD^

Cela annulera la validation mais remettra les modifications validées dans votre index. En supposant que les branches soient relativement à jour les unes par rapport aux autres, git vous permettra d'effectuer une vérification dans l'autre branche, après quoi vous pourrez simplement vous engager:

git checkout branch
git commit

L'inconvénient est que vous devez entrer à nouveau votre message de validation.

869
Blair Holloway

4 ans de retard sur le sujet, mais cela pourrait être utile à quelqu'un.

Si vous avez oublié de créer une nouvelle branche avant de valider et d'engager tout sur le maître, quel que soit le nombre de commits que vous avez effectués, l'approche suivante est plus simple:

git stash                       # skip if all changes are committed
git branch my_feature
git reset --hard Origin/master
git checkout my_feature
git stash pop                   # skip if all changes were committed

Maintenant, votre branche maîtresse est égale à Origin/master et tous les nouveaux commits sont sur my_feature. Notez que my_feature est une branche locale, pas une branche distante.

112
fotanus

Si vous avez une copie de travail vierge (non modifiée)

Pour annuler une validation (assurez-vous de noter le hachage de la validation pour l'étape suivante):

git reset --hard HEAD^

Pour extraire ce commit dans une autre branche:

git checkout other-branch
git cherry-pick COMMIT-HASH

Si vous avez modifié ou non suivi des modifications

Notez également que git reset --hard va supprimer toutes les modifications non suivies et modifiées vous pourriez avoir, donc si vous avez celles que vous préférez:

git reset HEAD^
git checkout .
109
Michael Mrozek

Si vous avez déjà appliqué vos modifications, vous devrez forcer votre prochaine transmission après la réinitialisation de HEAD.

git reset --hard HEAD^
git merge COMMIT_SHA1
git Push --force

Attention: une réinitialisation matérielle annulera toute modification non validée dans votre copie de travail, alors qu'une force poussée effacera complètement l'état de la branche distante par l'état actuel de la branche locale.

Juste au cas où, sous Windows (en utilisant la ligne de commande Windows, pas Bash), il s’agit de quatre ^^^^ au lieu d’un.

git reset --hard HEAD^^^^
20
Igor Zevaka

J'ai récemment fait la même chose, j'ai accidentellement modifié un master, alors que j'aurais dû m'engager dans une autre branche. Mais je n'ai rien poussé.

Si vous vous êtes juste engagé dans la mauvaise branche et que vous n'avez rien changé depuis et que vous n'avez pas poussé vers le référentiel, vous pouvez alors procéder comme suit:

// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes. 
git reset HEAD~1 

// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash

// create other-branch (if the other branch doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// take the temporary commit you created, and apply all of those changes to the new branch. 
//This also deletes the temporary commit from the stack of temp commits.
git stash pop

// add the changes you want with git add...

// re-commit your changes onto other-branch
git commit -m "some message..."

NOTE: dans l'exemple ci-dessus, je rembobinais 1 commit avec git reset HEAD ~ 1. Mais si vous voulez rembobiner n commits, alors vous pouvez faire git reset HEAD ~ n.

De plus, si vous vous êtes engagé dans la mauvaise branche et que vous avez également écrit du code avant de vous rendre compte que vous vous êtes engagé dans la mauvaise branche, vous pouvez utiliser git stash pour enregistrer votre travail en cours:

// save the not-ready-to-commit work you're in the middle of
git stash 

// rewind n commits
git reset HEAD~n 

// stash the committed changes as a single temp commit onto the stack. 
git stash 

// create other-branch (if it doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// apply all the committed changes to the new branch
git stash pop

// add the changes you want with git add...

// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."

// pop the changes you were in the middle of and continue coding
git stash pop

NOTE: J'ai utilisé ce site Web comme référence https://www.clearvision-cm.com/blog/what-to-do-when-yen-commit-to-the-wrong-git-branch/

15
Ali Mizan

Donc, si votre scénario est que vous vous êtes engagé dans master mais que vous vouliez vous engager dans another-branch (qui peut ou non ne pas déjà exister) mais que vous n'avez pas encore poussé, c'est assez facile à corriger .

// if your branch doesn't exist, then add the -b argument 
git checkout -b another-branch
git branch --force master Origin/master

Maintenant, tous vos commits sur master seront sur another-branch.

Source d'amour de: http://haacked.com/archive/2015/06/29/git-migrate/

7
Lorcan O'Neill

Pour élaborer this answer, au cas où vous auriez plusieurs commits à partir desquels vous souhaitez vous déplacer, par exemple. develop à new_branch:

git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started
4
arsenius

Si vous rencontrez ce problème et que vous avez Visual Studio, vous pouvez procéder comme suit:

Faites un clic droit sur votre branche et sélectionnez View History:

enter image description here

Cliquez avec le bouton droit sur commit sur lequel vous souhaitez revenir. Et revenez ou réinitialisez au besoin.

enter image description here

2
Trevor

Si la branche à laquelle vous souhaitez appliquer vos modifications existe déjà (branche développez , par exemple), suivez les instructions fournies par fotanus ci-dessous, alors:

git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature

Et vous pouvez évidemment utiliser tempbranch ou tout autre nom de branche au lieu de my_feature si tu voulais.

De même, le cas échéant, retardez l'apparition de l'effet masqué (appliquez) jusqu'à ce que vous ayez fusionné au niveau de votre branche cible.

1
fbicknel