Understanding Git (Local Repo) and GitHub (Remote Repo)
In this tutorial, you are expected to know the basics of Git and how to use the terminal. If you are not familiar with what I am talking about here, I strongly recommend you to my blog posts about CLI: “How to use the Command Line Interface” and Understanding Version Control System(VCS) & Git (Local Repo).
Create a new repository on GitHub :
1. Go to the GitHub website(www.github.com) to create an account, it’s free.
2. Once you have created an account and signed in, go to the top-right and click on the plus arrow to a create a new repository.
3. After clicking on a new repository, a new tab will be opened and you need to name your repo. I maned mine my-git-project
. Next, scroll down and click on Create repository
(green button)
4. After creating your repo, the following image would be displayed on your screen. It would give the choice to create a new repository on the command line or push an existing repository from the command line. Since I already have a local repo, I am going to use the latter which is to push that local repo from the command line to my GitHub. For now, what you need to is just copy the second code snippet, we will get back to it shortly.
Illustration about what we have done so far: The following picture describes our move up until now. I have a local repo (.git) and just created an empty remote repo (GitHub). At this point, both repos are not connected to each other yet.
Pushing local to the remote repo
First things first, let’s head to my terminal and cd
into my local repo which is my-git-project
. I used ls
command to print files I have in my local directory and git log
to see how many commits I have done so far.
As you know, we are going to push an existing repo from the command line to GitHub. To do that, I would copy the snippet code below from my GitHub and paste it into my local directory.
Illustration about what we have just done: From the command line I just pushed my local repo(.git) to the remote repo (GitHub).
Back to GitHub
We could see that we have transferred all commits from my local repo to my GitHub repo.
By clicking on a file you could see how many commits you have made in that file. Let’s check it out chapter1.txt
Local and remote repo are linked now
Since both repos are linked, we can create a new file on the local repo, make a commit and then push right away to the remote repo. For the next example, I have created a new file, named it chapter4.txt
, and then wrote the following sentence: I teach python and JavaScript.
Next, I have used git add.
and git commit
commands
Now, let’s git push
the commit I have just made from my local repo to my GitHub repo.
Back to my GitHub repo, we could see that our remote repo has been updated.
Branching and merging
git branch:
Shows which branch you are working on
git branch <name of the branch you want to create>:
Will create a new branch. I am going to create a new branch and name working-branch
By using git branch
, it shows below that we have two branches main or master and working-branch. The asterisk sign let us know which branch you are working on.
git checkout:
it makes you switch from one branch to another. Now, let’s switch to my working-branch.
syntax: git checkout <name of the branch you would like to switch to>
We would see that with the asterisk sign we are now on the working branch
.
While on my working branch
, I have changed chapter4.txt
file by adding one more sentence.
Next, I am going to use respectively the following commands : git add.
, git status
and git commit
. At this point, you are familiar with those commands and I am not going to comment on them.
Merging
We need to switch to the main or master branch before we could initiate any merging.
git merge <name of the branch you would like to merge with>:
I am going to merge my working-branch with the main one.
After merging both branches, I git push
to transfer from my local repo to my GitHub repo the commit I have made.
Back to the GitHub repo, you can see that it has been updated.
That’s all for this tutorial. Thank you for reading me and I hope you enjoy it.