Hello, friends, you get to know what is git and you get introduced to GitHub? do you plan to start trying things out? great! In this tutorial, you will create your GitHub account and push your first project ever!๐ฅ
Introduction
To get started, you have to make sure that you have git installed
Linux (Debian):
sudo apt-get install git
- Linux (Fedora)
sudo yum install git
once the installation is done open your command prompt(CMD) on windows or the terminal on Linux and check the version of git that you have installed:
git --version
Now let's open the git bash and start working there: Firstly, you will need to set up your name and email address, to do so use these commands:
git config --global user.name "your name"
git config --global user.email "your email"
you can check if you write your name and email correctly either by this command:
git config --global user.name
or by using:
git config --list
don't freak out๐จ, you do not need to know what is all of this for now just look at your name and email in the list.
If you haven't used the cmd nor the terminal before, check the followed section if you already have some experience feel free to skip it.
Prerequisite ๐ด
There are some basic commands we will need to use frequently while using the bash I'll mention them briefly and you can check this for more details.
๐pwd: Print the current working directory, in other words in which folders you are working on the bash right now.
๐ls : list all the files and folders in the current directory
๐cd : change to different directory.
๐mkdir : create new directory.
๐touch : create new file.
This is good enough! Now let's move to GitHub๐
Visit GitHub to create your account. once you've done go to your repositories and click the new green button to create your first repository.
Enter your repository name, description, and click the check box of the README file -we will look at this magical file later.
Cool๐คธ๐ปโโ๏ธ You can imagen this repo as the folder that will contain your project files. Well, but your project is in your local machine and this repo is on the internet(remote repo) how can we do thisโ Here we go, we will get back to the git bash and do the following:
1) Change the working directory to the location where you want to save your project folder. ex:
cd Desktop/
2) Clone your remote repo into your local machine, you will need the link of your repo on GitHub you can copy this from here:
Then execute the following command:
git clone https://github.com/username/repo-name.git
Super! you can check your repo folder on your device.
All you need now is to have some code ๐บ 1) Navigate to your folder repo by adding:
cd yourRepoName/
2) Create a new file and write hello world program with your favorite programming language, mine is java ๐: on the same directory (your repo folder) create a new file:
touch hello.java
open the file and show the world your skills:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Now save and navigate back to the git bash. you can run this file:
java hello.java
Fine, Let's skip to the good part๐ง
git add
We have these changes locally and they are not tracked by the git, meaning if I change anything now and saved I can't go back to the previous versions later. To add these files under git monitoring (git staging area) we use:
git add theNameOfTheFile
or
git add .
to add all the files under this directory.
git commit
Now to record the changes on these files we need to commit them. we can get back to a specific commit by using the commit id. it is best practice to write a short descriptive message every single time you write a commit.
git commit -m "Here is the commit-msg"
Note: you can't commit files before adding them, so remember, add then commit๐ค
git status
This is very useful, it shows the state of the files in the current directory are they added or committed? what files have changed since the last commit? and more...
git status
git push
After you added and committed your changes, you can now push them to your remote repo in your GitHub account
git push origin main
when you push for the first time it will ask you for the authorization, enter your email and password, and wait until it finishes. Refresh your GitHub repo page, you can find the last commit you push there๐.
Congratulations!๐
Now let's have another scenario which is the most likely one, you already have the project folder and you want to connect it with a remote repo.
1) Navigate to your project directory.
2) We will turn this project folder into a git repository - note this repo is local repo not remote yet-. This can be done by using
git init
it initializes a new git repo.
3) add and commit the files under this directory as the previous example.
4) create a new repo in your Github account.
5) To connect your local repository with new created remote repository use
git remote add origin <yourRemoteRepoLink>
6) Push the files to the remote repo
git push origin main
There is a lot more to know about git and Github but for now, let's keep it simple.