I am a git beginner and I ran into a problem with GIT REVERT. Here is the steps that I took:
mkdir new-repo
cd new-repo
git init
echo "first line" >> test.txt
git add .
git commit -m "first line added"
echo "second line" >> test.txt
git add .
git commit -m "second line added"
Then, to try out and learn how things work in git, I tried to give a git revert, to see if I could get back to the version of the file test.txt that only has the first line in the file, so I used:
git revert HEAD~1
but then, it gives the error:
CONFLICT (modify/delete): test.txt deleted in (empty tree) and modified in HEAD. Version HEAD of test.txt left in tree.
error: could not revert b88b72d... first line
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
And even if I git add test.txt and doesn't allow me to complete the revert, and I get stuck infinitely in this REVERTING operation status. If I do git rm test.txt, it allows me to complete the revert, but in this case I end up with no file test.txt in my workplace. That's not what I would like. I would like to have the test.txt with only the single line, after completing the revert. What am I doint wrong? and what should I do to get what I want in this case? I appreciate any help!
I tried git add test.txt but it doesn't work, and I remain stuck on the revert.
When you revert, you are basically applying a reverse diff, and you need to give Git something that identifies that diff.
In Git,
HEADrefers to the latest commit on the current branch, andHEAD~1refers to the second latest. So by doinggit revert HEAD~1you were trying to revert the first commit (that offirst line, which is the second-latest) rather than the second commit (that ofsecond line, which is the latest).