Git basics [Beginner Mode]
Git is an SCM tool and combined with GitHub it can do more than just tracking the version. In this article, I am gonna show you all the git commands that I used in my project. So let’s get into it…
A repository is a folder in a GitHub server that will contain your project files. And this repository has more functionality than your typical folder in your system.
A commit is a snapshot of your work at the moment.
A branch(except the main branch) is a sandbox for testing new code on the existing codebase. Any changes to your project in a separate branch will not affect your main branch.
Firstly to create a new repository for your project,
Open your project directory/ create a new project directory and open terminal and type:
git initto initiate a new repo.
git branch -m master mainto move the old master branch convention to main (optional)
git remote add origin <your remote URL>to connect local and remote repository
git remote -vto verify the new remote URL.
That’s it, you’ve just created a new repository 🥳. No lets start the work,
Create README.md and .gitignore files.
It’s the file that showcases your project in GitHub. This file will automatically be shown in your GitHub repo once you uploaded the files and this is not a normal text file but a special file called “md” (markdown), this is a text file but with extended functions.
Git will consult with this file every time you make a commit to checking which files should be tracked. Simply type the names of the file that you don’t want to be tracked like your IDE’s configurations and private keys and other secret files in a project.
So when you add files for commit you don’t have to worry about accidentally uploading secrets to the remote repository.
Now we have created these files so now we can commit these before we start the project. So,
git add <filename> <another filename>to add the files to track. You can also use “git add .” to add all the new files.
git commit -m <your commit message>to create a snapshot of your project. Add a nice message for future to check what has changed over time on each commits.
Now you have created a snapshot of your project but the span shot is present in the local machine, we want to take it to the remote repository (GitHub). To do that:
git push origin main
You can replace the “main” with any branch name but you will see how to do that in the next article.
That’s it, you have successfully created a repository and uploaded your code to the remote repository. Now what… Well, there is still a lot more to learn to make your coding life easier with git but we will learn about that in the next article. 👋