Home > Development Tools > git > body text

What should I do if git is submitted to master?

下次还敢
Release: 2024-04-09 11:48:20
Original
951 people have browsed it

When a git commit is accidentally committed to the master branch, the processing methods are: rollback the commit, create a new branch and merge, reset the master branch, delete and recreate the master branch, the branch selection method needs to consider code destructiveness, workload and history Record retention requirements.

What should I do if git is submitted to master?

What to do if git is submitted to master

When you accidentally submit code to the master branch, there are several Methods can be solved:

1. Roll back directly

  • Check whether the code is destructive or can be safely undone.
  • If it is safe to undo, use the following command to roll back the commit:

    <code>git revert COMMIT_HASH</code>
    Copy after login

##2. Create a new branch and merge

    Create a new branch and include unexpected commits.
  • Merge this branch to the master branch:

    <code>git checkout -b new-branch
    git merge master
    git push origin new-branch
    git checkout master
    git merge new-branch</code>
    Copy after login

3. Reset the master branch

    Reset the master branch to the state before the unexpected commit. Warning: This will delete all future commits for this commit.
  • Reset the master branch using the following command:

    <code>git reset --hard COMMIT_HASH_BEFORE_UNWANTED_COMMIT</code>
    Copy after login

4. Delete and recreate the master branch

    Delete the master branch and recreate it. Warning: This will delete all commit history on the branch.
  • Use the following command to delete the master branch:

    <code>git branch -D master</code>
    Copy after login
  • Re-create the master branch:

    <code>git checkout -b master</code>
    Copy after login

Which method to choose?

The method chosen depends on the following factors:

    Destructiveness of the code
  • Amount of work after submission
  • Whether it needs to be retained Commit History

Note:

    Before doing anything, make sure you have a backup of your code.
  • Please consider the implications carefully and choose the most appropriate solution based on your situation.

The above is the detailed content of What should I do if git is submitted to master?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
git
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template