This article will explain:
- Primary Difference Between “git checkout <filename>” and “git checkout” in Git
- How to Utilize the “git checkout <filename>” Command in Git?
- How to Utilize the “git checkout” Command in Git?
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:
Step 2: Update Particular File
Then, make changes in a specific file by updating its content:
Step 3: Check Git Status
Next, utilize the below-provided command to view the repository’s current 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:
Step 5: Verify Changes
Lastly, view the repository’s status to verify changes:
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:
Utilize the “–” symbol with the “git checkout” command and switch to the previous branch:
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:
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.