Small hints for myself regarding GIT.
Imagine we added 1.txt to some project and committed this file:
git add 1.txt
git commit -m 'test 1'
Then we added another file 2.txt and also committed:
git add 2.txt
git commit -m 'test 2'
After manipulations above we have the following situation:
$ git log --graph
* commit 53ec09a2cee2444877233ceca32c15779bb1d02d (HEAD -> master)
| Author: Yurii Petrovskyi
| Date: Fri Aug 9 10:35:32 2019 +0300
|
| test 2
|
* commit afb41e2d1fbab67a8cfaf316d41f81b73138b730
| Author: Yurii Petrovskyi
| Date: Fri Aug 9 10:35:07 2019 +0300
|
| test 1
|
* commit 0796e3cd0b010b3e84f565fd777811bfc3b225c4 (origin/master)
Then we need to undo the last commit (where we added 2.txt). In this case, it depends on do we want to save 2.txt file or we want to delete it. If we want to save the file and reset the last commit we need to use the next command:
git reset --soft HEAD~1
if we do not need this file at all:
git reset --hard HEAD~1
There is another scenario. We don`t want to push 1.txt from the first commit, so we should remove the file from repository's history:
git rm --cached 1.txt
git commit --amend -CHEAD
After commands above our "test 2" commit will contain removed 1.txt file and if we push it to the remote only 2.txt will be sent.
Also if we want to revert the whole specific commit we can use:
git revert commit_id
For example, I reverted "test 1" commit and this is a result:
git revert afb41e2d1fbab67a8cfaf316d41f81b73138b730
git log
commit 3ff659b964ca741a8c71dfa9ba45bcc74b66aa90 (HEAD -> master)
Author: Yurii Petrovskyi
Date: Fri Aug 9 12:45:32 2019 +0300
Revert "test 1"
This reverts commit afb41e2d1fbab67a8cfaf316d41f81b73138b730.
commit 2d006836dda2d5558b4e1d2701b291c5187abfd1
Author: Yurii Petrovskyi
Date: Fri Aug 9 12:43:44 2019 +0300
test 2
commit afb41e2d1fbab67a8cfaf316d41f81b73138b730
Author: Yurii Petrovskyi
Date: Fri Aug 9 10:35:07 2019 +0300
test 1
commit 0796e3cd0b010b3e84f565fd777811bfc3b225c4 (origin/master)
If you pushed some changes and it was a mistake, in that case I used the following commands:
git reset --soft HEAD~1
git push origin -f
Imagine we added 1.txt to some project and committed this file:
git add 1.txt
git commit -m 'test 1'
Then we added another file 2.txt and also committed:
git add 2.txt
git commit -m 'test 2'
After manipulations above we have the following situation:
$ git log --graph
* commit 53ec09a2cee2444877233ceca32c15779bb1d02d (HEAD -> master)
| Author: Yurii Petrovskyi
| Date: Fri Aug 9 10:35:32 2019 +0300
|
| test 2
|
* commit afb41e2d1fbab67a8cfaf316d41f81b73138b730
| Author: Yurii Petrovskyi
| Date: Fri Aug 9 10:35:07 2019 +0300
|
| test 1
|
* commit 0796e3cd0b010b3e84f565fd777811bfc3b225c4 (origin/master)
Then we need to undo the last commit (where we added 2.txt). In this case, it depends on do we want to save 2.txt file or we want to delete it. If we want to save the file and reset the last commit we need to use the next command:
git reset --soft HEAD~1
if we do not need this file at all:
git reset --hard HEAD~1
There is another scenario. We don`t want to push 1.txt from the first commit, so we should remove the file from repository's history:
git rm --cached 1.txt
git commit --amend -CHEAD
After commands above our "test 2" commit will contain removed 1.txt file and if we push it to the remote only 2.txt will be sent.
Also if we want to revert the whole specific commit we can use:
git revert commit_id
For example, I reverted "test 1" commit and this is a result:
git revert afb41e2d1fbab67a8cfaf316d41f81b73138b730
git log
commit 3ff659b964ca741a8c71dfa9ba45bcc74b66aa90 (HEAD -> master)
Author: Yurii Petrovskyi
Date: Fri Aug 9 12:45:32 2019 +0300
Revert "test 1"
This reverts commit afb41e2d1fbab67a8cfaf316d41f81b73138b730.
commit 2d006836dda2d5558b4e1d2701b291c5187abfd1
Author: Yurii Petrovskyi
Date: Fri Aug 9 12:43:44 2019 +0300
test 2
commit afb41e2d1fbab67a8cfaf316d41f81b73138b730
Author: Yurii Petrovskyi
Date: Fri Aug 9 10:35:07 2019 +0300
test 1
commit 0796e3cd0b010b3e84f565fd777811bfc3b225c4 (origin/master)
If you pushed some changes and it was a mistake, in that case I used the following commands:
git reset --soft HEAD~1
git push origin -f