Learn the Basic of Git and GitHub
If you want to get started on learning about Git technology, you’ve come to the right place. This is a comprehensive beginner’s guide to Git. There are many clients for Git. The technology is all the same no matter the client. But in this guide we’ll be using GitHub to understand Git.
What is Version Control?
- Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. So ideally, we can place any file in the computer on version control.
Creating a new Github repository!
Create new project
- When creating a new project on your local machine using git, you'll first need to create a new repository(or often "repo"). Now, to begin with, open up a terminal and move to where you want to place your project on your local machine using the cd(change directory) command.
Initialize the git
- To initialize the git repository to the root folder run the git init command.
Add new file to repository
- Now, add a new file to the repository, using any text editor you like. I'll be using vscode for this. (On the terminal you can run "code ." and it will open up the vscode with the directory you are working on)
Git status command
After creating a new file you can run the git status command to know which file exists in your repository.
In the command prompt, it basically tells you that it has noticed you have created a new mytext.txt file but unless you use the "git add" command we can't stage any changes.
Git add command
Now to stage our changes to Github, run the git add. You can now run the git status command again to check if your changes are staged or not.
Here the "dot(.)" means it will stage of all the changes you have made, for instance, if you have multiple files then it will stage all the changes of each file you have made up till now, so it is recommended in a development environment to give the relative path of the file you want to add.
Git commit
It's time to commit your message now using git commit -m "Your message about to commit".
The message in the commit should be something related to what you have done maybe it's a new feature or a bug fix or any refactoring of something.
Create new repo
Now create a new repository on Github.
After clicking the button, GitHub will ask you to name your repo and provide a brief description.
Git remote add origin
Now, open up your terminal and run git remote add origin to add your project from your local machine to GitHub.
I have used "git branch -M main" here for demo purposes since I am having only one file, but when you are working in a development environment it is recommended that you create a new branch for your work and push all the changes to that branch by using git checkout command.
Push your project
- Now we'll push the commit on our new GitHub repo. This will allow other programmers to see the changes we've made or the work we have done. To push changes we'll run the git push command.