Git

How to Use Git in PowerShell

Many system administrators and developers are already familiar with PowerShell, especially if they are working with the Windows environment. Working with Git using PowerShell, Git allows them for leveraging their previous skills and knowledge. PowerShell provides access to a wide range of commands and modules, making it easy to integrate Git operations with other tasks.

This tutorial will provide the process of using Git in PowerShell.

How to Use Git in PowerShell?

To use Git in PowerShell, check out the below-stated procedure:

  • Launch Windows PowerShell from the startup menu.
  • Navigate to the Git root directory.
  • Initialize a new Git repository and navigate to it.
  • Check status.
  • Generate a file and add content.
  • Track the created file and commit all changes.

Step 1: Launch Windows PowerShell

First of all, search for “Windows PowerShell” through the startup menu and launch it:

Step 2: Go Toward Git Root Directory

Provide the Git root directory path along with the “cd” command and redirect to it:

cd "C:\Users\user\Git

Step 3: Make a New Repository

Utilize the “git init” command for initializing a new Git repository:

git init demo2

The resultant image shows that an empty Git repository has been initialized successfully:

Step 4: Navigate to the Newly Created Git Repository

Move toward the newly created Git local repository using the “cd” command:

cd demo2

Step 5: Check Working Area

Execute the “git status” command for viewing the status of the current working repository:

git status

The below-stated output indicates that the working area is clean and there is nothing to commit:

Step 6: Generate a File

Use the “echo” command to create and edit the file simultaneously:

echo "This is my file" > abc.txt

Step 7: Verify the Created File

Check the Git status to ensure that whether the file has been created or not:

git status

The given output shows that the file has been created successfully:

Step 8: Track the File

Track the created file from the working area to the staging area with the help of the “git add” command:

git add abc.txt

Step 9: Commit Changes

Execute the “git commit” command along with the “-m” option to commit changes:

git commit -m "file tracked successfully"

It can be observed that all the changes have been committed successfully:

Step 10: View Git Log

Run the “git log” command to check the complete Git log history:

git log

As you can see, the recent commit has been saved in the Git log history:

You have learned about using Git in PowerShell.

Conclusion

To use Git in PowerShell, first, launch “Windows PowerShell” using the startup menu. Then, navigate to the Git root directory and initiate a new Git repository for moving to it. After that, generate a file and add content using the “echo” command. Next, track the created file and commit all changes. This tutorial has explained the complete method for using Git in PowerShell.

About the author

Hafsa Javed