Junior — Middle
How can you undo changes made in a remote repository?
sobes.tech AI
Answer from AI
To undo changes in a remote repository, the following approaches are usually used:
- Creating a new commit to undo changes — the safest way. For example, if you need to undo the last commit, you can do:
git revert <commit_hash>
git push origin <branch>
This will create a new commit that undoes the previous changes.
- Rewriting history with force push — a more risky method, used if you need to completely remove commits from history:
git reset --hard <commit_hash>
git push --force origin <branch>
This will delete all commits after the specified one from the remote repository. It is important to warn the team to avoid conflicts.
- Using pull request and rollback via hosting interface (GitHub, GitLab) — if the repository is hosted on a platform with a web interface, there is often an option to rollback changes through the UI.
The choice of method depends on team policy and the situation. In most cases, git revert is preferred as it preserves history and does not break the repository.