Reverting a Commit
Which Git command creates a new commit that undoes the changes made in a specified commit, preserving the history?
- git reset --hard
- git revert
- git undo
- git checkout
- git restore
Understanding `git reset --hard`
What does the command `git reset --hard HEAD~1` do?
- It moves the HEAD pointer to the previous commit and stages the changes.
- It moves the HEAD pointer to the previous commit, discarding the changes in the working directory and staging area.
- It reverts the last commit while preserving the changes in the working directory.
- It creates a new branch pointing to the previous commit.
- It undos the most recent change made to the staging area
Reverting a Merge Commit
When reverting a merge commit, which option is required to specify which parent the revert should follow?
- -m
- -p
- -f
- -r
- --mainline
Safety of `git revert`
Why is `git revert` generally considered safer than `git reset --hard` in shared repositories?
- Because it's faster
- Because it doesn't alter the commit history, avoiding potential issues for collaborators.
- Because it automatically pushes changes to the remote repository.
- Because it completely deletes the commit, freeing up space.
- Because it is the only way to undo changes.
Undoing a Staged File
Which command unstages a file but keeps the changes in the working directory?
- git reset HEAD file.txt
- git restore --staged file.txt
- git checkout file.txt
- git revert file.txt
- git unstage file.txt
Restoring a File
What is the main purpose of `git restore file.txt`?
- Discard changes in the working directory and revert to the last committed version of the file.
- Stage the file for the next commit.
- Rename the file in the repository.
- Delete the file from the repository.
- Copy the file into a different directory
Recovering a Lost Commit
If you accidentally deleted a branch with commits that weren't merged, how can you potentially recover those commits?
- Use `git fsck --full --no-reflogs --unreachable --lost-found` to find dangling commits.
- Use `git revert --continue` to continue reverting commits that were lost.
- The commits are permanently lost; there is no way to recover them.
- Use `git merge --abort` to undo the deletion of the branch.
- Use `git clean -df` to remove unnecessary files.
Reverting Multiple Commits
How can you revert a range of commits, for example, the last three commits?
- git revert HEAD~3..HEAD
- git revert HEAD~2..HEAD
- git reset HEAD~3
- git checkout HEAD~3..HEAD
- git undo HEAD~3..HEAD
Conflict Resolution After Revert
What should you do if a `git revert` operation results in a merge conflict?
- Abort the revert operation immediately.
- Resolve the conflicts, stage the changes, and then run `git revert --continue`.
- Use `git reset --hard` to discard the conflicting changes.
- Ignore the conflicts and push the changes to the remote repository.
- Automatically merge the changes by running `git revert -a`.
Undoing a Commit Before Pushing
You made a commit locally that you want to completely remove *before* pushing to a remote repository. Which command is most appropriate?
- git revert HEAD
- git push --force
- git reset --hard HEAD~1
- git clean -df
- git checkout HEAD~1