Git Demystified
adikejre
Posted on April 5, 2023
Git can seem to be an intimidating tool to use and especially for those starting out, it is easy to get lost in the complexities and the vast use cases which might lead you to wonder- Is it worth the headache?
In this blog, I will attempt to show a simple workflow on how to use git in a simplified way and cover basic commands and use cases. This will help in getting comfortable with using git and github and cover basic commands to get started with.
What is Git?
Git is a version control system which lets you to track all the changes made to a file system.
A repository is created and initialized which contains all the files. Git maintains a history of all changes made to these files over time. This allows you to view the changes made to the code, collaborate with other developers, revert to previous versions and essentially time travel between the different versions of the code base.
Creating a Github Repository
We can easily create a repository as shown below.
Once it is created, we can clone the repository on our local machine so that we can easily make changes in our files. For this we use the command: git clone
We copy the https key and do git clone as shown below using powershell
Great! so now we have created an empty repository and cloned that on our local system. You can see the .git
folder present in the project's root directory.
Now we can start adding files and making changes.
Making changes and pushing the code
Now in the directory where we have cloned, we can create a sample html file and add content.
Now we need to accomplish the following:
- Add all our changes to the working tree so that we can push it
- Commit our changes to record all the changes we have made
- Push our changes to github
For this we use the following commands:
git add -A
: This adds all our changes made in our directorygit commit -m "commit message"
: This records the changes and adds a comment to the commit.git status
: To see all the changes which are staged but not yet pushedgit push
: This uploads the changes to the remote repository which is in github
These commands are seen below in action:
And here instantly we can see the file we created and all the changes reflected on our github repository.
Git Branches
Creating separate branches to work on is very useful especially when working on different features which you want to keep separated. It is good practice to work on a branch instead of the main so that the main or master branch is protected and changes are only made to the required branch.
The following commands are used:
git branch <branch_name>
: Creates a new branch with given namegit branch
: Gives the name of the current branchgit checkout <branch_name>
: Shifts to the given branch
Thus it is easy to switch between branches and using different branches for different features is often useful.
We create a branch test1
and push the changes to it.
Note that when pushing to a new branch which does not have an upstream branch in github yet, we use git push --set-upstream origin test1
Now a friend wants to contribute to your project. This is easy to do as they can clone the repository in their system and create a branch and start working on their own.
As shown above, the repository is cloned and test2
branch is created to work on.
This covers the basics of setting up a git project, and a workflow on the basics of git you need as a developer working on your personal project and wants to start with simple version control to make life easier. In the next posts, I will go into more detail on how to work collaboratively using git, make pull requests and cover other useful commands.
Posted on April 5, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.