Git - git sync with Github (pull,fetch,merge,push)

Git - Sync Local Repo with Github Repo(clone,fetch,merge,pull)


 git clone command

This command downloads the copy of a Github repository to the local system.

Example:

There is a new developer who joined the project and he needs the latest code for the project. In this case, he can use the git clone command to download the latest repository.

        git clone <<HTTPS or SSH URL of Github Repo>>

After running the above command. there will be a directory get created with the same name as your Github repository name and you will find that this local repository has all the commits which are available on the Github repo. 

git fetch and merge command

fetch and merge commands are used to sync the data between local and remote repositories.

Example

Step1: Add a new file (github.java) to the Github repository.

Step 2: On-Command terminal run below command

            git fetch 

            git merge origin main

Step 3: Verify that the github.java file is available in the local Repo.

git pull command

This command is a combination git fetch and git merge command

git pull origin main --no-rebase

Example

Step 1: Let's create a file in the remote repo [Github](remote.java)

Step 2: Go to the local repository and add a file say main.java and commit this file to the local repo.

Step 3: Push commit changes to the remote repo.

           git push origin main

It will reject the push because the local repo is not in sync with the remote repo. remote.java file is missing in the local repo.

Step 4: To resolve this issue you first need to pull the changes and then push the changes to the remote repo.

              git pull --no-rebase

             git push origin main 

Comments