Learn Git and GitHub With Examples
Git is an open-source tool used to manage the version control system. It is developed by Linus Torvalds.
Github provides a web-based graphical interface to create the remote repository for the version control system. It provides the facilities to host your Git repository and you can make this project as open-source so anyone can contribute to this project.
What is the version control system?
- Suppose many developers are working on a project. all are making changes to the source code. the version control system tracks these change history and provides more details about the change did by the developers.
- Git allows us to revert the current change and go back to the older version if any problem in the latest changes.
Follow the below step to setup git and create the Git Hub repository and push your local project to GitHub.
These commands are used in this tutorial further.
git init git add . git commit -m "Your message" git push -u origin master
Create a Repository on GitHub
Visit the Github website and signup for the GitHub account.
Click the " + " icon and select New Repository
Provide the repository name, write a small description, and click on the Create repository button.
Wow ..!! You have completed the GitHub set up and created the new repository on Github. Till this time, your repository is empty.
Git installation on the host system
Now Open your computer terminal and installed the git tool using the below command
- On Ubuntu:
sudo apt-get install git
- Another Linux distro please follow the link: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Set the default user name and email for your project.
This information is associated with your commits and you can change this any point of time.
git config --global user.name "user_name"
git config --global user.email "This email address is being protected from spambots. You need JavaScript enabled to view it. "
Now we have finished the basic git installation and configuration setup.
Git initialization on local directory
Create a new directory on your system (example - demo) and initialize the git with the below command.
mkdir demo
- This command creates the demo folder on your system
- Go to the demo directory and run below command
git init
- This command converts the current directory into the empty git repository and .git directory created inside the directory.
Git Operation on local directory
After Git initialization, You must do these four steps (status, add, commit, and push) to upload your local change to the GitHub repository.
Status
Git status command shows which files have modified or added files into the directory.
- Create the README.md file and write in it "This is a demo project".
echo "This is demo project" >> README.md
git status
Add
You need to add all files which are showing by the status command.
git add "filename"
- To add all files in one shot use below command.
git add .
Commit
We have already added files to the repository. Now, you have to write a message to git which explains your changes, and This message shows in the git log history so later you can check and know about the change.
git commit -m "This is my first git commit"
Push
Now you have completed your first git commit and just one step behind to upload your local change to GitHub repository.
- To connect your local system to GitHub. Run below command.
git remote add origin https://github.com/"Your User Name"/demo.git
- This command help to add the remote connection as origin so Git to knows where to send your files and also avoid to type full URL every time.
- Now push your change to the remote repository and verify this on Github.
git push -u origin master
- When you run this command it will ask your Github username and password so just provide it.
Go the GitHub and check your repository.
Wow... You have successfully uploaded your local files to the GitHub repository.
Note: You can run the git log command at the terminal and see your commit message and more info.