Start Web Development By Learning Git and Github
What are Git and GitHub?
It’s important for any web developer to have the skills of using at least one versioning system like Git and another system to manage remote storage like Github. The learning of those tools and the relationship between them can be overwhelming at the beginning, that is why you do not have to worry if you do not have a clear visualization in your mind of them, it will be settled by the time.
Git is a tool that works on your local machine. It lets you save your files and directories locally on your machine by keeping a historical record of each save. This allows you to go back to it at any time. Git is commonly described as a version control system. Github is remote storage on the web. So, once you are done with your project on your local machine, you can push it on Github for sharing and collaborating with other developers.
Start correctly with Git and Github :
It’s trivial that we first need to install Git on our machine and create a Github account on the web.
Step 1:
Install Git
Reach the https://git-scm.com/ site and install the Git tool according to your operating system Linux, macOS, window. You can check your version by running :
git — version
Step 2 :
Set up git
In your terminal type the commands below :
git config –global user.name “ your name”
git config –global user.email “your name@example.com”
Be sure to enter your own information inside the quotes and verify that all is working properly by running these commands :
git config –get user.name
git config –get user.email
Step3 :
Create a Github account
If the output of the commands above matches your name and email, create a Github account by reaching the github.com link.
Step 3–1 : Create an SSH key :
If you like to push your project from your local machine to Github without having to type every time your username and password, you have to generate an SSH key, which is a really very long password used to identify your machine.
Let’s check if your SSH key is already installed by typing this command in the terminal :
ls ~/.ssh/id_rsa.pub
In the case of the message ‘No such file or directory’ appears at the console, then you have to generate it otherwise if no message appears this step is done.
To generate a new SSH key run the following command :
ssh-keygen -C <youremail>
Step 3–2 :
Link your SSH key with Github
In this important step, you have to log into your Github account, then click on your profile picture and click on the Settings option.
Next, on the left-hand side, click SSH and GPG keys. Then, click the green button in the top right corner that says New SSH Key. Name your key something. Leave this window open while you do the next steps.
In your console display your SSH key by entering this command :
cat ~/.ssh/id_rsa.pub
Highlight and copy the output, which starts with ssh-rsa and ends with your email address.
Now, go back to GitHub in your browser window and paste the key you copied into the key field. Then, click Add SSH key. You’re done!.
Step4: Getting git repository :
There are to ways you can follow to create a Git repository :
1- you can convert a local directory into a Git repository :
2- you can clone an existing Github repository.
For simplicity let’s only choose the second way. So, reach your Github account and create a new repository by clicking the button shown in the screenshot below.
Then name your repository “git-test”. check the “Add README file” option and create the repository by clicking the button “Create repository”.
Your repository is created and you are redirected into, now you are ready to copy it into your local machine. Click the button “Clone”, select the SSH option and copy the line below to it.
In the command line on your local machine, open your terminal to where you want to store this project, and then clone the git-test repository by typing the command git clone followed by the URL copied in the last step.
The full command should look similar to :
git clone git@github:USER-Name/repository-Name.git
Now, you have successfully connected your Github repository to your local machine. You can test this by moving into this folder :
cd git_test then type the command git remote -v, this will display the URL of the repository you created in Github.
screenshot
Step5 :
Use The Git Workflow :
- Let’s add a new file to our repository “git_test” by typing in the command line :
touch hello_world.txt
screenshot
- With the command git status you can check that your file is not staged because it is displayed in red.
- Type git add hello_world.txt this command will add this file in the staged area in git.
- Type again git status and you will notice that your file is displayed in green color, which means your file is in the staged area.
screenshot
before we push our project to Github I want to show you the command that allows you to undo a staged file to an unstaged file :
git restore — staged file name
Step6 :
Push your work to Github
git push origin main