Beginners guide to Git using CLI.
Four years back during my first internship, I was asked to maintain my code on git. I freaked out given all those git jargons “commit, push request, pull request” and so on. I felt like I was being dumped in the ocean without knowing how to swim and nobody was there to help me. And then the difficult task of all: “using git through command line” felt even worse. I saw multiple videos, read few articles nothing made sense to me at that time. Over time, I learned while making a lot of mistakes. During this process, I have realized that all newbies almost feel the same.
In this article, we’ll cover how to get started with git and git-CLI. The only thing I’m assuming is that you have already created a Github account and installed git CLI on your local machine. If not you can create a Github account here: https://github.com and setup git CLI using this tutorial.
Step 1: Initialization of Git
Create a new folder/directory for this tutorial i.e “Git-test”, and go inside this folder using the given commands.
mkdir Git-testcd Git-test
Now we’ll initialize git in this folder with this command
git init
This command will initialize an empty git repository in our folder. A hidden .git folder will be created.
Step 2: Adding code to your initialized repo
We have created empty git repo. Now you can copy-paste your code files or create new files in this newly created folder i.e “Git-test”. Once the code and files are ready, check the status of your repository using the following command.
git status
In the output, you can see all the newly created files, but the color in which the list of files shown is red color. This means that these new files are not added in the git repo yet. To add all the newly created files in our repo we’ll use this command:
git add -A
Now check the status again using “git status” and this time you’ll see that the result is shown in green color. This means all our files are added to our git repo and we are good to go to our next step.
Git add command takes a snapshot of all the files in our folder and that snapshot is used later for version controlling.
Step 3: Commit
Git is a version control system where we can go back and forth between our different versions of code. Each version is like a snapshot of our code with a unique id. In our last step we created a snapshot of our code now we will create a unique id for that snapshot using git commit.
That unique-id is also called commit number.
Once we have added our code we’ll now commit it with a message attached to it. Commit your code using the given command:
git commit -m “your message”
Since no one can remember to commit numbers these commit messages will help you remember what changes you did in which version.
Step 4: Create a new repo on Github
Till now, we did everything in our local machine. Now let's open github.com and create a new repository there, set the name of the repository to “Git-test”, add a description if you want. And now click on the “Create repository”.
Step 5: Connect the local repository with Github
We have one repo present locally on our machine and one repo on Github. It’s time to connect these two. So we can push and maintain our code in it. Open your command line/terminal in the Git-test folder and use the following command (replace the link with your own GitHub repo link)
git remote add origin https://github.com/UsmanFazil/Git-test.git
Our local repo is now connected to our remote Github repo. And the name of that remote repo is “origin”. You can change this name if you want.
Step 6: Master branch
We have not created any branch yet. Before we push our code we need to create our first branch. We can create our master branch using the following command.
git branch -M master
Master branch will be created for our repository, now that we have covered all the prerequisites let's move toward the final step.
Final Step: Push code
Finally, the moment we all have been waiting for. Let’s push our code and see it on the GitHub website. For that, just the user command is given below. This will push our code from our local repo to the remote GitHub repo in the master branch.
git push -u origin master
If you have not configured your git config global variables in your local machine, git will ask for your GitHub user name and password, and then VOILA. You have successfully pushed your code in your remote GitHub repository.
Conclusion
The focus of this article was to simplify the process of creating a repo and using basic git CLI commands. In this article, we covered basics e.g how to create a new GitHub repository, how to connect local and remote repos. And how to push our code in the remote repo.
There were few technical details that I have not covered in this article just for the sake of simplicity. So, that newcomers don’t get overwhelmed. If you get stuck at some point or want the next part of this tutorial, where we go one step further and create multiply branches, pull and merge code, etc, just leave a comment.
Happy coding!!!