How to Pull and Update One File in Git

John Wachira Feb 02, 2024
  1. Pull and Update One File in Git
  2. Conclusion
How to Pull and Update One File in Git

This article will discuss the steps to update a single file from the remote repository. This article is for those who want to pull changes from the remote but only apply changes for one file.

Pull and Update One File in Git

The example below shows a local repository linked to a remote GitHub repository. Our local repo has a config.py file that we need to update from the remote.

Our remote branch also has other files with changes. The git pull command will fetch and merge all the changes from the remote, and we are not ready for this.

How do we only update the config.py file? We know that the git pull command is a combination of two commands; the git fetch and git merge.

The git fetch command will fetch changes from the remote but will not update our local files.

$ git fetch

The changes from the remote repository should be accessible in our local repository.

We can then run the git checkout command while feeding the path to our file.

$ git checkout m- <revision> <path-to-file>

The <revision> part is a branch, and you can get the <path-to-file> by copying it from GitHub.

To get the path-to-file, go to your GitHub account and navigate to the file location. You can copy the path as shown below.

Copy the file location in your GitHub

We can run the git checkout command.

$ git checkout -m origin/master apps/config.py

Output:

$ git checkout -m origin/master apps/config.py
Updated 1 path from f8b3224

This will add the file to our index, and we can commit the changes.

update single file from the remote repository

We have successfully updated a single file from the remote repository.

Conclusion

Git allows us to fetch and update changes to a single file with the git checkout command.

When feeding in the path to the file, do not include the repository name. You copy the path on the GitHub webpage.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Fetch