We have already covered git in advance, and now we are also familiar with GitHub and the features provided by GitHub. In this lesson, we will sync the local repository with GitHub; you will get a complete idea of how you can sync your local repository with the remote repository of GitHub.
Let's get started
We have covered Git & GitHub in detail. We learned that git is a Distributed Version Control System, and GitHub is a web-based git, version control repository and internet hosting service. Furthermore, we will learn how to synchronize the Local repository with GitHub in this lesson.
Creating local repository
First, we need to create an empty directory. Use the following command.
mkdir <gitandgithub>
Then, we need to Navigate to the Directory we created earlier with the following command.
cd <gitandgithub>
Git Init
Now we created the Directory; we can initialize the git repository in the empty repository using the following command.
git init
Remember that the command
ls -A
is used to list the .git/ folder on the terminal because .git/ is the hidden file in the working directory.
Git add
Adding File Using Terminal
We can create a file using the following command touch index. html
and edit the file through the terminal easily using the vim or nano editor.
vi index.html
You can also read the text inside the file using the following command.
cat index.html
Git status
So files are now added to the Directory. Before we add the files to the staged area, firstly, we need to check the status of the git repository, which files are modified or deleted in the Directory, which can be checked using the git status
command.
Adding file to the staging area
Now, we need to stage our files, which are added to the Directory, to track our files every time we modify, delete or add to the Directory. With the git add .
command, you can add your files to the staged area, and then with the help of the git status
command, you can check the status of your git local repository.
Git commit
When the files are in the staged area, you have committed on the files; when you make changes on the file, the commits history is created and can be accessed later using the command git log
. To Commit to the changes, you can use the following command.
git commit -m "Describe message."
main.css file is also added to the Directory, which means the changes have been tracked in the git repository. To stage the changes, we can use the
git add .
command, and to commit, we can use the commandgit commit -m "Adding main.css
file. Whenever you modify, delete or change the file or folder, you need to repeat this process and check the status of the git repository. You can use the commandgit status
.
Git branch
As we have already discussed the concepts of the branches in detail, here we need to rename the master branch to the main branch because Github's new repositories are pushed to the main branch. Use the following command to rename the master branch to the main branch.
git branch -M main
Creating a new GitHub repository
You'll need to find a "+" sign at the top right of your GitHub account to get this started. The profile tab is next to it. Click the profile, and you will see a dropdown with the first option to create a "New repository." Once clicked, you will be redirected to another page where you will have to set certain information regarding your project, such as providing your repository's name, adding a description of the project, and declaring the repository as public. Do not select the README file and License. We will create these files later on.
If you follow the above image, you will redirect to the manual, which might be useful for the beginner.
Git Remote
Now, let's go back to the local repository where we created the index.html file and main.css file; in the git bash terminal, we need to add the GitHub repository which we have created earlier; with the following command, you can add the remote repository to the local git repository.
git remote add origin <GitHub REPO URL>
To get the GitHub Repository URL, you need to go to your repository created earlier, and by clicking on the Code button, you will get the GitHub Repo URL.
The command will look like this.
git remote add origin https://github.com/gitlearning892/gitlearning892.github.io.git
With the git remote -v
command, you can check the remote origin is added to the local repository or not.
If you accidentally add the wrong repository to your local repository, you can change the existing remote repository with the following command.
git remote set-url origin <GitHUB REPO URL>
Git push
The Git push command is used to upload local repository content to the remote repository. Pushing is how you can transfer commits from your local repository to a remote repository. Pushing can overwrite changes; cautions should be taken when pushing to the remote repository.
git push -u origin main
Go to your repository and refresh it; you will see all of your local content now pushed to the Github repository.
A popup will come for the Github authentication. Click on Login using a browser and provide your credential to push all of your changes to the Github repository.
Git pull
The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match the content.
For example, If someone from your team made some changes in a file or created some file on a remote repository. A developer created a file named user.txt, but you don't know the changes made on the remote repository. Let's say you have created another file named team.txt. After you commit the file and try to push that file to your remote repository, git will not allow you to push your code because the remote repository contains work that you do not have locally. So, In this case, the git pull command is used to fetch and download content from a remote repo to a local repo.
Let's create a new file on GitHub Repository.
As users.txt file is created on the GitHub Repository but this file is not available on the Local repository.
You can pull your GitHub changes using the command
git pull origin main
Git Fetch
When we use git fetch command, we fetch the commits of a remote repository into our local repository. The fetched commits are saved as remote branches separate from the local branches. So, this helps in reviewing the commits before integrating them into the local working branches.
Lets create a new file news.txt in the new branch
Click on propose change and this will redirect us to the Pull Request.
So, To work on “newbranch” we need to switch from the main branch to the “newbranch”. The command to switch branch is
git checkout newbranch
Got an error, notifying newbranch doesn’t match any file doesn’t match any branch or this branch does not exist in the git repository, this does not mean to create a new branch and push it to the GitHub repository because the newbranch is already exiting, you need to fetch this branch before being able to work on it so in order to fetch the branch simply write git fetch origin
and then git checkout newbranch. If you want to fetch just a specific branch you can just specify the below command
git fetch origin [branch-name]
After reviewing the changes in the remote branch you can merge it into the local branch using git merge command.
git checkout main
git merge origin/newbranch
Then you can push the changes to the main branch and it will automatically merge the new branch changes to the main branch.
Git clone
git clone is a Git command-line utility that is used to target an existing repository and create a clone, or copy, of the target repository. The command for cloning the repository is.
git clone <REPO-LINK>
Summary
In this lesson, we have learned how to sync the local repository with GitHub and how to use git push, pull, and fetch commands with GitHub. We have also learned how to push changes to GitHub from a local repository.
If you have any question, please feel free to connect with me on LinkedIn.
Top comments (0)