A simple way to get started with Git/GitHub
Learning is the best way to understand what you want to know.
I want to share with you how world-class developers use Git and GitHub. I hope you will enjoy this article and I think it will help you in your journey of becoming someone who can work remotely and be able to understand version control.
I am going to work you through the way I use Git and GitHub and with that, you can learn a lot of other strategies to work with others.
The first thing we should know is that version control is going to help us track our code changes when we are developing our application. So here we are going to create a simple application that is going to help us understand how to use version control with Git and GitHub.
First of all, you should have a GitHub account and Git installed on the local machine. Then follow the following to understand the workflow of Git and GitHub. Happy coding…
- Create a new repository on GitHub
To start, you should go to your GitHub homepage and click the New button for creating a new repository.
The above picture shows the next page after clicking the New button. You can have a private repo or a public repo. So, it is your choice to decide what you want.
The following is the screenshot of the repository created. Where you can now start to work on your project.
After the creation of our repository, we should be able to clone the application from GitHub. To get a copy of what you have on GitHub in your local machine you have to click on Clone or download button and copy the link.
2. Clone the project
Write the following snippet in the terminal to get the project from GitHub.
> git clone https://github.com/j4l13n/learn-git-github.git
> cd learn-git-github
After cloning the project you will get the folder that has the same file as you have it on GitHub. If you have cloned well go into the create folder that has the same name as your GitHub repo.
3. How to commit your codes (Save the codes: git commit -m “message”
)
Since you have cloned the project you can add files and folders and save them in the repository. So, if you want to track the changes in codes. you have to know how to commit them.
Create a new file in the repo:
> touch index.html styles.css
> ls -a
Open the index.html file in your preferable editor. I would recommend using a vs-code editor. Add some codes into the file.
I have opened the index.html file. copy the codes above and paste them into the same file.
If you have saved the changes. go back to the terminal and run the following commands.
The first thing to know when you have changed your codes is the command called git status.
As you see, the files created are in red color which means that they are not tracked within our repo. So we have to save them into it. To do it, you should first add the files into staging where the repo will be ready to have the added files and code changes. This will let us know about the command.
The above screenshot shows after the command the status of our repo looks like. You can see that the files have changed the color from red to green. This means that the files are now into staging and we can now save(commit) them. This leads us to know about git commit -m "message"
.
4. Get the logs of commits (commit logs: git log
)
You have two commits in your repository and you can see the commits using the command called git log
. Run the command and you will see the logs of your commits. See the following.
So far so good, you have tracked your files and you are now ready to add your codes into your GitHub repo. The next thing is to push the added code to the GitHub. This will let us know about git push origin master
. As we have our repo connected with GitHub repo it will be easy to push our codes. See the following to push them.
You have successfully pushed your code to GitHub and to see it, you should go to the created repository on GitHub.
As you see it, you have added index.html and styles.css files. This means that anyone who wants to collaborate with you can have an opportunity of getting the same codes you have.
Let us think of a scenario where someone has added some code on your repo. He has added a file called script.js
. Your GitHub repo looks like this now.
Since we don’t have the file in our local machine. We should need to have the same file as our GitHub repo. So, this will introduce a new command called git pull origin master
which will help us to get all the new contents that are on the remote repository.
The response you are seeing is that you have received a new file called script.js which means now you have updated your local repository from GitHub.
I hope this was a good experience to get started with Git/GitHub. I am very happy to share this tutorial as you are learning something new. I am going to work on the advanced workflow of using Git/GitHub and I hope it will give you another experience of how to work with your team.
I think you enjoyed the tutorial.
Thank you for reading…