Git

How to `git pull` While Ignoring Local Changes?

Sometimes, developers work on a local repository and perform several actions, such as creating files and branches for each module, updating files, committing changes, and many more. Moreover, they can ignore the added changes during the “git pull” operation. To do so, first, it is required to reset the HEAD position and then run the “$ git pull <remote-name> <branch-name>” command.

This write-up will discuss the pull operation method while ignoring the local changes.

How to `git pull` While Ignoring Local Changes?

To perform the “git pull” while ignoring local changes, check out the below-listed instructions:

  • Move to the Git local repository.
  • Check the list of the repository content.
  • Open the desired file and update it.
  • Track changes and check the log history.
  • Reset the HEAD position and run the “$ git pull <remote-name> <branch-name>” command.

Step 1: Go to Git Repository
First, navigate to the particular Git repository by running the “cd” command:

$ cd "C:\Users\nazma\Git\Test_13"

Step 2: Check List of Content
Then, execute the “ls” command to check the repository list of content:

$ ls

Step 3: Open Desired File
Next, open the particular file by running the provided command:

$ start file1.txt

Step 4: Track Added Changes
After that, execute the “git add .” command to push the added changes to the staging index:

$ git add .

Step 5: Commit Changes
Now, commit the added changes to the Git repository along with the desired commit message:

$ git commit -m "file2.txt second time updated"

Step 6: Check Reference Log History
View the list of the current repository log reference with the help of the “git log .” command:

$ git log .

Step 7: Reset HEAD Pointer
Finally, reset the HEAD position by executing the “git reset” command along with the “–hard” flag:

$ git reset --hard

As you can see, the HEAD pointer is set to the “6088a00” commit SHA hash:

Step 8: Check Added Remote URLs
To view the list of the remote URLs, run the “git remote” command along with the “-v” flag:

$ git remote -v

According to the given below output, the remote name “origin” exists in the remote URL list which contains the remote repository:

Step 9: Git Pull
Finally, pull all local content to the remote repository while ignoring the added changes by running the “git pull” command along with the remote and the branch name:

$ git pull origin dev

That’s all! We have efficiently discussed the pull operation method while ignoring the local changes.

Conclusion

To perform the “git pull” while ignoring local changes, first, move to the Git local repository and check the list of the repository content. Then, open the desired file and update it. After that, track changes and check the log history. Finally, reset the HEAD position and execute the “$ git pull <remote-name> <branch-name>” command. This write-up described the method of the pull operation method while ignoring the local changes.

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.