15 Eylül 2022 Perşembe

git reset seçeneği - Staging Area İçin Undo

Giriş
Şeklen şöyle


Şu anki branch'in daha önceki hallerinden birisine geri gelmek içindir. Açıklaması şöyle
git reset —mixed is used to undo changes made in the working directory and staging area.
git merge —abort helps stop the merge process and return back to the state before the merging began.
head seçeneği
-- file veya ~N seçeneği ile kullanılır

Örnek
Commit'lenmiş bir dosyayı silmek için şöyle yaparız.
$ git reset HEAD -- <file>
Örnek
Açıklaması şöyle
It also very common to accidentally push a few changes/files that were not supposed to be committed. Let’s say you had some config files or credentials files like AWS secrets or .pem files. These should not be pushed in the repository. One thing that many people might try in order to remove the pushed .pem from the remote repo is following:

Pull the latest code from the repo.
Delete the .pem file
Create another commit with the deleted .pem file
Push the changes back to remote repo
However in the above approach we haven’t completely removed the .pem from git. You see git keeps a track of all the files that you commit. So anyone can dig into the history of git commits and access the .pem file.

But git allows you to actually modify history. So the solution would be to move back to your safe commit and force push.

git reset HEAD~n is a command that lets you move back to your previous n commits. So git reset HEAD~2 will take you back two commits and will show you the changes of the last two commits.

git reset HEAD --hard will clean all those changes. So for example lets say you have the following commits


Now let’s say you want to remove the last two commits i.e (commit ‘Add aws secrets’ and ‘Add .pem file’)

First you will go back two commits by running the following command:

git reset HEAD~2

Now you can remove those two commits by using the following command:

git reset HEAD -- hard

Now for push the code to remote and your .pem files will be deleted from history as well.

--hard seçeneği
Örnek
Commitlenmiş her şeyi silmek için şöyle yaparız.
git reset --hard HEAD
Örnek - undo commit
Şöyle yaparız
$ git reset HEAD~ //Bir önceki commite gel
$ nano <file> //Dosyayı düzelt
$ git add <file> //Dosyayı tekrar ekle
$ git commit -c ORIG_HEAD //Commitle

Hiç yorum yok:

Yorum Gönder