How to undo commits in Git (PowerShell Git Guide)

Regardless of, write whether you are scripting yourself or working as part of a team, being able to keep track of code versions is very important. In this article, we will explain how to use the command Git resetto go to the previous commit in Git source control, and cover the following topics:

  • Using Git

  • What is Git and what are commits

  • Setting up a local repository in Git

  • Tracking and Committing File Changes in the Git System

  • Saving Additional Changes to Git

  • Managing commits in Git

  • Glossary of Git Commands

Prerequisites: How to Use Git

You need to install the Git client on your system. Client can be downloaded here and the installation guide is here… You will also need a file to edit and track changes. In this article, we use the script PowerShell, but you can take any other.

What is Git and what are commits

Git is a common version control tool that keeps track of changes by committing them (making a “commit”). Making a commit is like keeping a version of a file or group of files. Commit keeps track of file versions by establishing a relationship with a unique identifier (or SHA, or hash). This unique identifier allows you to revert to versions of files previously saved in the repository.

A typical workflow involves doing the following:

  1. Checking out files from a remote repository;

  2. Making changes to local files;

  3. Saving changes;

  4. Moving new files to the repository.

However, if you don’t have a main remote repository, or if you don’t like moving or merging code for some reason, you can use Git on your local system. You can save changes to your code and discard them so you have an idea of ​​the commits.

1. Setting up a local Git repository

You should start with the command Git init… This command will initialize the current folder as a Git repository. In this example, I show you how to initialize a project folder as a Git repository.

Git init

The command adds a hidden folder called .Git that keeps track of the current active branch, config files, aliases, and more. data

Then we need a file to track changes. We have PowerShell script file called myScript.ps1. The script contains a for loop that is repeated five times and printed to the screen. This first loop will be my outer loop. The actions that the code performs are not important – we just want to show how the code is edited in a simple script.

The next step is to view the status of the repository by running Git status… The status will show which branch we are currently on. For now, just keep in mind that we are on the “master” branch. All new or changed files since the last save of the changes will also be highlighted in red.

2. Tracking and committing file changes in the Git system

If you need to track files to commit changes, add them to the staging area with the command Git add… After the Git add command, you can specify one or more files to add to the staging area. In our case, there is only one file, so we add it by specifying the name. Then we check the status of the repository to view the script in the staging area.

Git add myScript.ps1
Git status

Now that we are tracking the file in the staging area, we need to issue a command to commit the state of the repository. We use the command Git commit with parameter -m to create explanatory text. The text should be short, but contain a description of the changes that were made to the code. After committing the changes, run again Git status to make sure there are no other files in the staging area and have not been modified since the last save.

Git commit -m “first version of script - outer loop only”
Git status

3. Committing additional changes

Now that we have our first save entry in the repository, we need to refine our code. Add another for loop to the outer loop, which we will refer to to add the inner loop.

On restart Git status the results will be the same as before. The file will not be tracked because changes have been made to the script file since it was last saved. We repeat the algorithm of using the commands to add a file and save it. In the text of our description, you can see that an inner loop has been added.

Git status
Git add myScript.ps1
Git commit -m “Added inner loop”

4. Managing commits in Git

Now that we have a few commits in the repository, we can move on to the command Git log… This command will show the previous commits and their explanations. Each commit has a unique hash that identifies it. In this example, the last commit is the one where we added the inner loop and it is marked as HEAD.

Now, suppose we want to undo our last changes and revert to an earlier version of the script. It would be possible to manually remove the inner loop as the script itself is rather small. However, in larger projects, this can be problematic.

Instead, we can use the first seven characters of the commit hash value to revert to a previous version. For this purpose, we execute the command Git reset with a commit hash:

Git reset a6dd1c2

This command resets the HEAD to a specific commit. The screenshot shows that myScript.ps1 returned to staging area with post-commit changes a6dd1c2… When you run the command again Git log HEAD will revert to the previous commit, and the commit where the inner loop code was added will be removed.

To navigate to a file, you need to check it out from the commit branch with the command Git checkout… After extracting the file, you can view its contents, which shows the presence of only the outer loop. Then we run the Git status command and thereby show that the current tree does not have the changes necessary to track.

Glossary of Git Commands

Here’s a quick rundown of each PowerShell Git command we’ve used and its purpose:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *