DEV Community

Cover image for Get started with Git
Burak
Burak

Posted on

Get started with Git

Hi everyone! Have you ever found yourself in a situation where you broke your perfectly working code and didn't have a backup? If so, you might want to get started with Git. In this post, you will learn to initialize a Git repository, write some code, commit your code and push it to GitHub.

What is Git?

Git is a version control system, developed by Linus Torvalds in 2005. It's used to manage different versions of source code. As an example, you have a program and it has a bug. Someone can fork your code, thus creating a new version of your source code, then implement a fix for the bug, and ask you to merge their changes to your version of the program. During that period, you implement a new feature in another part of your program. How do you combine these two versions? This is what Git does for you. When you accept the other developer's request to merge their code, their changes get applied to the modified files, but your local changes are still in tact. Using Git, every version of your files in a repository are tracked. And the version that acts like a checkpoint, is called a "commit". You can look at the commit history of your repository and get the older version back if something goes wrong.

Installing Git

To use Git, you need to install it first.

On Windows, you can download from Git's website, or use winget to install Git.

On Linux, you can install Git from the repositories of your distribution. The package is usually called git.

On macOS, you can use Homebrew or install XCode.

Creating a repository

Now that you have git installed, it's time to create a repository. You should start by creating a folder for your project. Once you create the folder, open a terminal and set your current directory to the folder.
To initialize a repo, run the git init command. It will initialize the repository with a main/master branch.

Adding a GitHub remote

You created the repository, but you have nowhere to push it yet. For that reason, you should add a remote.

git remote add origin https://github.com/your-name/my-awesome-repo.git
Enter fullscreen mode Exit fullscreen mode

Replace your-name with your GitHub user name, and change the repository name to your own liking.

Creating a README and staging the change

You can start by creating a README.md. Create it in the folder and go back to the terminal.
README.md is currently untracked, and we want it to be tracked. To do this you can run git add ., which will tell Git to track all untracked files.

Creating a commit

Now you can commit your changes to the repository. To do that, run git commit. This will launch an editor (typically vi), in which you can write your commit message. If you want to cancel the commit and go back, you can leave the message empty and the commit will be aborted.

Pushing to GitHub

To push your repo to GitHub, you can do git push origin master, which will push your repository to the origin remote. You will need to authenticate in order to push the repository. You can use the GitHub extension in VSCode to take care of the authentication, or push using the GitHub Desktop app. If you need to push directly from the terminal, you will need to generate an access token on GitHub. You can see the instructions to do that here.

Top comments (0)