| by Arround The Web | No comments

Difference Between “git checkout ” and “git checkout”

While working on Git, developers create and work on multiple Git branches. Before merging them into the main codebase, they may need to switch between different branches for testing and debugging changes in a separate branch. Moreover, it is also required to revert specific file changes in a particular branch. For such corresponding purposes, the “git checkout” command can be used.

This article will explain:

Primary Difference Between “git checkout <filename>” and “git checkout” in Git

The “git checkout <filename>” is used to restore the specific file to a previous version. This command reverts or discards the untracked and uncommitted changes made to the specified file in the current repository. While the “git checkout” is utilized to switch/move between branches and to create a new branch in the Git directory.

How to Utilize the “git checkout <filename>” Command in Git?

To revert the changes of the specific files, first, redirect to the local repository. Then, make changes in a particular file and check the Git status. Next, execute the “git checkout <filename>” command to discard modified changes.

Step 1: Navigate to Desired Repository

First, switch to the local repository by typing out the “cd” command with desired repository’s path:

$ cd "C:\Git\ReposA

Step 2: Update Particular File

Then, make changes in a specific file by updating its content:

$ echo "This is new line" >> testFile.txt

Step 3: Check Git Status

Next, utilize the below-provided command to view the repository’s current status:

$ git status

In the given-below screenshot, it can be observed that the modified changes are untracked:

Step 4: Revert Changes

Now, revert or discard the modified changes of the particular file through the “git checkout” command along with the file name:

$ git checkout testFile.txt

Step 5: Verify Changes

Lastly, view the repository’s status to verify changes:

$ git status

It can be observed that the Git status is clear and file changes have been reverted:

How to Utilize the “git checkout” Command in Git?

The “git checkout” command is utilized for various purposes, such as:

  • To switch to a specific branch.
  • To switch to a previous branch.
  • To create/make a new branch and navigate to it at once.

In order to switch to a particular branch, type out the “git checkout” command and specify the target branch into which you want to navigate:

$ git checkout main

Utilize the “” symbol with the “git checkout” command and switch to the previous branch:

$ git checkout -

This command can also be utilized to create/make a new branch and switch to it at once by specifying the “-b” option and new branch name:

$ git checkout -b feature

That was all about the “git checkout <filename>” and “git checkout” commands in Git.

Conclusion

The “git checkout <filename>” command is used to revert or discard the untracked and uncommitted changes made to the specified file in the current repository. While the “git checkout” is used to move from one branch to another and to create a new branch in the Git repository. This write-up explained the difference between the “git checkout <filename>” and “git checkout” commands and how they can be utilized in Git.

Share Button

Source: linuxhint.com

Leave a Reply