Git

How do I Pull Files from Remote Without Overwriting Local Files?

Sometimes, Git users want to download the updated version of the centralized server repository. On the other hand, they don’t want to merge or overwrite the local data with a remote for further modifications or use. In that situation, it is required to temporarily hold the local data to avoid overwriting by utilizing the “git stash” command.

This article will discuss the easiest way to pull files from the remote repository without overwriting local files.

How to Pull Files From Remote Without Overwriting Local Files?

To pull files from the remote repository without overwriting local files, check out the below-given instructions:

  • Switch to the desired local repository.
  • Create and move files into the staging index.
  • Update the repository and push files from the repository to temporary memory.
  • To download the GitHub data locally, use the “git pull” command.
  • Lastly, use the “git stash pop” command.

Step 1: Navigate to Required Repository
First, move to the particular Git repository through the provided command:

$ cd "C:\Users\LENOVO\Git\test-repo"

Step 2: Generate File
Then, run the following command to make a new file in the current repository:

$ touch file1.txt

Step 3: Move to Staging Index
Now, push the previously generated file into the Git index area:

$ git add file1.txt

Step 4: Commit Changes
Next, update the repository by executing the below-stated command along with the “-m” tag for the required commit message:

$ git commit -m "file1.txt added"

Step 5: Make Stash
Now, use the following command to temporarily keep all changes from the Git index:

$ git stash

As you can see, the previously committed file has been temporarily moved to the stash:

Step 6: Check Remote URL
Execute the “git remote” command to show the remote URL list:

$ git remote -v

Step 7: Perform Git Pull Operation
Finally, pull the remote content into the local repository through the “git pull” command:

$ git pull

Step 8: Pop Stash Data
Lastly, execute the “git stash“ command to pop the stashed data into the local repository:

$ git stash pop

It can be observed that the stash content is dropped successfully into the Git repository:

That’s all! We have compiled the easiest way to pull files from the remote repository without overwriting local files.

Conclusion

To pull files from the remote repository without overwriting local files, first, navigate to the Git local repository. Then, generate and add files into the staging index. After that, update the repository and move files from the repository to temporary memory. Finally, pull the GitHub data and execute the “git stash pop” command. This article demonstrated the method of pulling files from the remote repository without overwriting local files.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.