🎏 This is the 10th day of my participation in the Gwen Challenge. Check out the details: Gwen Challenge

0 x00 📢 preface

👇 Git getting started is linked below. Read the articles in that order at 👇.

Git goes from quitter to starter column

This series of articles will help you quickly learn the basics of Git through examples and practices.

This is the fourth article that covers undo operations at different stages.

0x01 Local changes are abandoned

📌 Sometimes I modify files in my local working directory (usually called workspace). How do I discard local changes (not yet provisioned)?

First, switch the project to the latest version of the Master branch.

$ git checkout master
Copy the code

Change helloWorld.html to add a comment (error content).

<html>

<head>
</head>

<body>
    <h1>Hello, World!</h1>
    <! -- Add a comment, error content. It will then reset.
</body>

</html>
Copy the code

Check the status of the workspace.

$ git st (git status)
Copy the code

Show that the file helloworld.html has been modified and has not been temporarily saved.

git checkout

Use the git checkout

command to checkout the version files in the repository and discard the changes in the working directory.

$ git checkout helloworld.html
$ git st
$ cat helloworld.html
Copy the code

Shows no changes in the working directory. The new comments in the file have also been restored.

git restore

You can also use git restore as prompted; the command is experimental and its functionality can change. Git – restore v2.32.0 doc

$ git st
$ git restore helloworld.html
$ git st
$ cat helloworld.html
Copy the code

After checking the status of the working directory, run git restore to discard the changes in the working directory as described above. The same effect as Git Checkout.

0x02 Cancel temporary changes

📌 Sometimes changes are provisioned but have not been committed to the repository. How do I cancel provisioned changes?

Change helloWorld.html to add a comment (error content).

<html>

<head>
</head>

<body>
    <h1>Hello, World!</h1>
    <! -- Add a comment, error content. It will then reset.
</body>

</html>
Copy the code

Use Git add to temporarily store changes.

$ git add helloworld.html
Copy the code

Check the status of the working directory to show that changes are staged and ready to commit.

git reset

Use the git reset command to reset the contents of the staging area in the HEAD and clear the changes that have been staging.

$ git reset HEAD helloworld.html
Copy the code

git restore –staged

Changes that have been staged can also be cleaned up using git restore –staged commands.

$ git restore --staged helloworld.html 
Copy the code

Abandonment of local changes

Git reset git restore — Staged commands do not change the working directory by default. So the files still exist in the working directory have changed. To remove these changes, use the checkout or restore command from the previous example.

$ git checkout helloworld.html  # git restore helloworld.html
$ git st
Copy the code

Now the working directory is clean again.

0x03 Undo committed changes

📌 Do you sometimes find that changes you have committed to your local repository are incorrect and want to undo the commit?

Change the helloWorld.html file and commit

<html>

<head>
</head>

<body>
    <h1>Hello, World!</h1>
    <! -- I don't want to commit this change.
</body>

</html>
Copy the code

Ephemeral changes and commit.

$ git add helloworld.html
$ git commit -m "I don't want to commit this change"
Copy the code

Git Revert Commit

The Git Revert operation is an option that Git creates for a new commit, which undoes all changes to an existing commit.

Because the last commit will be undone, HEAD is used as the restore parameter.

$ git revert HEAD
Copy the code

Running the command will bring you to the editor.

(HEAD, HEAD^, HEAD~2)

If the editor is not open, add –no-edit to the command.

$ git revert HEAD --no-edit
Copy the code

Viewing the log records, you can see the original commit record and the undo restore operation commit record.

0x04 Remove Branch Commit Record

📌 How do I remove submitted records?

When you do a restore commit with Git Revert, you remove the error and restore the code, but the undo commit and error commit records are still displayed in the history. If sensitive files are involved, the file contents can still be seen in the historical record even after the restoration operation, which involves data security issues.

Start by tagging the most recent submission to view the project submission history.

$ git tag unwanted 
$ git hist 
Copy the code

git reset –hard

Looking at the log records, commits after the label “v1” are error commits and original commits. You can use the git reset command to reset the current branch to make the commit, either using the tag name or the commit hash.

$ git reset --hard v1
$ git hist  
Copy the code

After executing the command, the master branch points to the commit labeled v1, and the error commit restore record is not seen.

🚨 — The hard flag is the only dangerous use of the reset command. It forces files in the working directory to be overwritten, literally destroying the data, and cannot be undone.

The faulty commit restore records are not gone, they are still in the repository and can only be referred to using hashes. Use git log-all to view all records.

Remove the label

The warehouse holds unreferenced referrals for a period of time until the garbage collection runs. Error commit restore commit records are recycled as garbage by removing label release references.

git tag -d unwanted
git hist --all     
Copy the code

The historical record is now clean and clean.

0 x05 📚 reference

“Git_reset Pro_git_v2_zh ebook

0x06 Attention column

This article has been included in the column 👇, you can directly follow.

Read more | The series continues to be updated