Git

How to do ‘git checkout –theirs’ for Multiple Files

On Git, the developer creates files and modifies them multiple times. Therefore, sometimes it becomes difficult for developers to add the path of the multiple modified files simultaneously. The β€œgit checkout –theirs” command can be used along with the target repository name to perform the particular operation. In the mentioned command, the β€œ–theirs” option represents the version that holds the work changes to be reverted onto the current working Git branch.

This guide will provide the usage of the β€œgit checkout” command along with the β€œ–theirs” option for multiple project files.

How to β€œgit checkout –theirs” for Multiple Files?

To check out for multiple files, follow the provided steps:

  • Go to the Git repository.
  • List the existing content of the repository.
  • Select multiple files and update them one by one without pushing them to the staging index.
  • Show the status of the working repository.
  • Switch to the root directory.
  • Execute the β€œgit checkout –theirs <repo-name/*>” command.
  • Navigate to the target repository and view the status.

Step 1: Navigate to Git Repository

At first, execute the β€œcd” command along with the desired repository path:

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

Step 2: List Existing Content

Then, display the list of current repository content by running the β€œls” command:

$ ls

Here, we have selected three text files which are highlighted below:

Step 3: Modify Selected File

Next, modify the β€œfile.txt” file through the β€œecho” command:

$ echo "file" >> file.txt

Step 4: Update Selected File

Execute the β€œecho” command and add the changes into the specified file:

$ echo "text file" >> file1.txt

Step 5: Edit File

Similarly, edit another file with the help of the provided command:

$ echo "My file" >> file2.txt

Step 6: Check Status

Now, execute the below-given command to check the status of the current working Git repository:

$ git status .

As you can see, all modified files exist in the Git working directory:

Step 7: Checkout With β€œ–theirs”

Now, switch to the root directory and use the β€œgit checkout” command:

$ git checkout --theirs test1/*

In the above-stated command, the β€œ–theirs” option signifies that the current branch is being rebased. The β€œtest1/” is the name of the Git local repository, which contains modified untracked files and an asterisk β€œ*” symbol after the slash β€œ/” indicates all files.

According to the below-given output, the paths of all unstaged files are updated:

Step 8: Check Git Status

Lastly, move to the Git repository and view its status by running the β€œgit status .” command:

$ git status .

It can be seen that all unmerged files are added to the staging area:

That’s all! We have demonstrated the β€œgit checkout –theirs” process for more than one file.

Conclusion

To check out for multiple files, first, move to the Git repository and list the existing content of the repository. Then, choose multiple files and update them one by one without adding to the index. Next, check the status of the current working repository and switch to the root directory. Finally, execute the β€œgit checkout –theirs <repo-name/*>” command. Lastly, navigate to the target repository and view the status. This guide briefly illustrated the β€œgit checkout” command with the β€œ–theirs” option for multiple 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.