| by Arround The Web | No comments

The Difference Between git pull vs git fetch

While working on Git, when developers make changes to their Git remote repository, they may want to integrate them into the local repository. In this situation, Git offers two most widely used commands to fetch modifications or changes from the Git remote, such as the “$ git fetch origin” command and the “$ git pull origin” command.

This manual will differentiate the git fetch and git pull commands.

What is the Difference Between git pull vs git fetch Command?

The git pull command enables users to fetch and merge with another Git repository as well as Git local branch. In contrast, the git fetch command allows developers to download objects from another Git repository.

Now, let’s head toward the implementation side!

How to git pull and git fetch?

To perform git fetch and git pull, first, move to the directory and initialize it using. Next, list out the content of the current repository and check the repository status. After that, track the untracked file and add the remote. Lastly, run the “$ git fetch origin” command and execute the “$ git pull origin <branch-name> –allow-unrelated-histories” command to fetch the remote repository data and integrate it with the local repository.

Let’s try out the above-discussed instructions!

Step 1: Navigate to Local Directory

First, execute the provided command to navigate to the Git repository:

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

 

Step 2: Initialize Git Repository

Next, initialize the Git local repository using the “git init” command:

$ git init

 

Step 3: List Repository Content

Run the “ls” command and list out the content of the repository:

$ ls

 

Step 4: Check Status

Check the current repository status by utilizing the following command:

$ git status

 
According to the below output, the “file12.txt” is untracked and present in the Git working directory:


Step 5: Track File

Now, run the “git add” command to track the untracked file:

$ git add file12.txt

 

Step 6: Commit Changes

To store the added changes, execute the “git commit” command with commit message:

$ git commit -m "first commit"

 

Step 7: Add Remote Repository

After that, add the remote repository into the current Git directory with the remote repository URL which you want to pull and fetch the data:

$ git remote add origin https://github.com/GitUser0422/Temp_repo.git

 

Step 8: git fetch

First, fetch the whole up-to-date remote repository data using the “git fetch” command:

$ git fetch origin

 
Here, the “origin” is the name of our added remote repository URL:


Step 9: git pull

Lastly, execute the “git pull” command with the remote branch name and “–allow-unrelated-histories” option to fetch and integrate it with the local branch:

$ git pull origin master --allow-unrelated-histories

 
As you can see, the “master” remote branch is merged successfully with the similar local branch:


We have explained the use of git pull and git fetch commands in Git.

Conclusion

git pull command is used to fetch from the remote branch and merge data with another repository or local branch. On the other hand, the git fetch command enables users to download objects from another repository. To perform git fetch and git pull, first, move to the directory and initialize it. After that, track the untracked file and add the remote repository. Finally, execute the “$ git fetch origin” command and run the “$ git pull origin <branch-name> –allow-unrelated-histories” command. This manual illustrated the git pull and git fetch commands working.

Share Button

Source: linuxhint.com

Leave a Reply