Create a repository on Github

Create a repository on Github

Generate a personal access token on GitHub to authenticate the access to the GitHub repo. Navigate to your Github account settings, then Developer Settings. Click the Personal access tokens menu, then click Generate new token.

Enter a name for the token under Token Name, choose the expiry time. Under the Repository access section choose only selected repositories. Click the "Select Repositories" button and choose the repository created. Under the "Permissions" section expand "Repository Permissions". Scroll down to "Contents" and select "Access: Read and Write". Scroll down to the bottom and click Generate token.

Generate a personal access token on GitHub

Then create a new local repository

mkdir fantastic-blog && cd fantastic-blog
git init --initial-branch=main

Create a new file home.html with contents

touch home.html
echo "<h1>Blog Header</h1>" > home.html

Add home.html to the repository staging area and create a commit

git add home.html
git commit -m "added home.html to the repo"

Create a remote branch and push the local repo to remote repo

git remote add origin https://github.com/chonkarexsaurus/fantastic-blog.git
git push origin main
create a local repository

Now both the local and remote repositories are in sync.

Update the home.html file in the Github repository by adding a <p>paragraph one</p> in the file.

update remote repository

Now the local repository has 1 commit but the remote repository already has 2 commits.

Download the latest changes by using git fetch command.

git fetch origin

Now that you've downloaded the latest changes, you can compare your local branch to remote branch

git diff main origin/main
fetch remote repository

To approve the changes and merge them into your local main branch use git merge command:

git checkout main
git merge origin/main
merge remote repository

The origin/main and main branches now point to the same commit, and you are synchronized with the upstream developments.

Let's add another line of code in our home.html file in the remote repository.

update remote repository

Now there are 3 commits in the remote repository, and 2 commits in the local repository

To synchronize your local repository with the central repository's main branch run git pull command.

git pull origin main
synchronize repository

Difference between Git Fetch and Git Pull

Git Fetch Git Pull
Used to fetch all changes from the remote repository to the local repository without merging into the current working directory Brings the copy of all the changes from a remote repository and merges them into the current working directory
Repository data is updated in the .git directory The working directory is updated directly
Review of commits and changes can be done Updates the changes to the local repository immediately
No possibility of merge conflicts Merge conflicts are possible if the remote and the local repositories have done changes at the same place
Git fetch basically imports the commits to local branches so as to keep up-to-date that what everybody is working on Git Pull basically brings the local branch up-to-date with the remote copy that will also updates the other remote tracking branches