Tired of creating Git repositories and using the commands that follow, without knowing what they do or why we need them? Me too. That's why today we're gonna clear them once and for all, so next time when someone will ask us how do you name our main branch, you'll know what to answer.
FIRST THINGS FIRST
A Git repo (or repository) is a storage space that holds your project's files and the entire history of every change made to them. Imagine that your project has a .git folder in it and its purpose is to track the changes you made to the code, allow you to collaborate with other people by adding to the same code base or revert the code to previous versions.
Why would we want that? Well, when you're the only contributor to a project, you don't really need version control (or do you)? But in general it's recommended to use some kind of VC, just in case something happens to your machine. No machine, no code. No code, all your work is gone. So you must keep it safe.
Coming back to the repositories, they generally fit into two categories:
remote repos- a copy of your project is stored on a sever (someone else's computer). We have platforms like GitHub, GitLab or BitBucket that are based on the Git version control system and on which anyone with an account can store their code and share it with other people. Super secure in case your machine makesboom. If machine makesboomand the servers also makeboom, the project is lost, but hey, who's so unlucky, right?local repos- a copy of your project is stored on your machine. Less secure than than a remote repository, because of the machine-makes-boom situation.
Remote repos are no better or worse than local repos; both categories have different risk profiles.
GITHUB REPOSITORIES
If you ever created an empty Github repo, you'll be presented with a few options.
- quick setup - for the one musy download Github Desktop, use a HTTPS or SSH.
- create a new repository on the command line.
- push an existing repository from the command line.
We will be focusing on the last two options.
Create a new repository on the command line
For this option, you have the following commands.
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/yourUsername/yourGithubRepoName
git push -u origin main
1.We usually a folder with the desired name for our project.
2.We open the terminal in that folder and run git init. This will create a brand-new, empty local Git repository inside our current folder. Nothing more (remember, we need that folder if we want Git to track our changes).
3.We run git add README.md. Not a mandatory step but nice to have. This file is used to communicate important information about our project (why it exists, how to use it, what's the setup, why it is useful etc). IMPORTANT: Git can only add files that already exist, so in order to add the README.md, we must first create it. We can do that manually in the folder we're in or run:
git >> README.md
followed by
git add README.md
4.We run git commit -m "first commit". As the name says, it creates the first commit. A commit is a snapshot of our Git repository at one point in time. Imagine you have three files in your project and you want to create a forever copy of the state of your project, at that exact point in time. You basically tell Git: these are my three files and this is what they contain. Take a snapshot of them and remember how they looked like in this exact moment.
HINT: remember we added the README.md file one step before. Now we commit it (make its existence permanent and official in Git's memory).
5.We run git branch -M main. This will overwrite the branch's name we are currently on to main. If we would run git branch -M shakira, the branch would be renamed to shakira. If you don't believe me, run git branch -M shakira and then git branch - this command will give you the name or the current branch you're on.
Why do we want to rename our branch to main? GitHub often suggests it as a standard setup step because it makes the default branch name consistent with modern repos. Why it’s used:
- Many projects now use main instead of master as the default branch name.
- It helps avoid old naming conventions and aligns with GitHub’s default branch setup.
Is this a good enough reason? I don't know, I'm probably still gonna name my branches superman and batman.
6.At this point, our repo is a local repo. Meaning that the copy is only kept on our machine. Our target is to upload it to Github so we need to connect the local repo with the remote one.
git remote add origin https://github.com/yourUsername/yourGithubRepoName
This basically tells Git: "hey, I have this repo that I want you to connect it with this other repo. Call it origin and this is the address https://github.com/yourUsername/yourGithubRepoName where you find this origin.
7.And lastly, we run git push -u origin main. This will push (upload) all the changes we have (files) of our project to the remote (origin) repo.
And voila, now the machine can make boom-boom but that won't matter, because we can sleep peacefully. Our to-to app code is safe.
Image source RealToughCandy.com on Pexels
Top comments (0)