Git in just a few minutes
EstherWanjiru
Posted on December 13, 2021
Hey, here's a deep dive into Git.
Incase you missed the Introduction to git and GitHub, start here
Whether you are a long time programmer or are just getting started, you know that Git is quite pivotal in your coding journey.
Git is a distributed version control system that makes tracking changes to your code very easy. It's easy to swap across versions and well who doesn't like the idea that one can undo the changes in your code through Git.
Lastly, Git is so ideal when incorporated in Teams.
After installation, Configure it with your GitHub account.
Step 1: _(Option1)_Initialize Git in your existing project
To do this simply run the command in the terminal from the folder that contains all your project files. This will add a .git folder to your project that Git will use to track all the changes you make to your project.
Git init
_(Option2)_Cloning your repository(entire project being managed by Git) by creating a new repo or getting an existing repository and copying the link to clone the project. Ensure you pick the right url according to how you set up your Git, either ssh or http. Thereafter type the command below
Git clone ssh_url or Git clone http
in your terminal with the copied url at the end of the command. This copies all the code from the repo on to your computer and sets up Git to track all the changes. It adds a .git folder so that when you are ready to push your changes to GitHub, git will already know where to push them too.
Git structure
- Working Directory - All the files in the work repository in their current state. At this point none of the working repositories are being tracked by Git.
- Index/ staging - To track these changes in the work directory we need to add them to the Index. This can be done by using the command below.
git add
This tells Git we want to track all the changes that have been made in the working directory. This still doesn't make the changes in the official version in your code that you can change in between them. Which is why we commit these changes from the index to the final stage, the Head. Using the command below
git commit -m "message saved with the commit."
3.Head - This tells git that the changes are final and requires a message to be saved with the commit. After committing changes to the head, they are saved as a version on our computer but are not in our remote repository on GitHub.
In order to get these changes to our remote repository on GitHub, we need to push them to GitHub using the command below:
git push
If you set up your project using git clone
, then Git will already know where to push all the files to. But if you used git init
to initialize a new project, then it will need to be told where to push these changes to. This can be done by:
- Creating a repository on GitHub
- Copy the ssh_url for cloning.
- Thereafter, you can use the origin command below. This tells Git that we have a remote repository called origin at the copied url.
git remote add origin ssh_url
With the remote repo added, we can use the git push
command to push the changes to the remote repo on GitHub.
So far... we can π₯·π»π¦Έπ»ββοΈ
- Create a repository.
- Make changes to it.
- Save the changes.
- Push the changes to GitHub.
What if π€
One of your team members wants to pull down the changes that we have pushed to GitHub on to their local computer, this is where we use the command below in their terminal and all the changes from GitHub will be pulled onto their local computer and merges with all the changes that they have:
git pull
That's basically all you need to know about Git to get started!!!
Please make sure you leave a like π/loveheart β€οΈ on this one. It encourages one to write more and more π π
Posted on December 13, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.