Beginners Guide to Git

William Johansson
5 min readMar 16, 2023

--

When it comes to developing software, version control is a crucial component of the process. It allows you to keep track of changes made to your code over time, collaborate with others on a project and revert back to previous versions if something goes wrong. Git is a popular version control system that is widely used in the software development community.

The git logo

Making a git project.

The first step in making your project use git is to have a project to begin with, and the second step is to initialize it with git init. So, what exactly is git init? Well, when you start a new project and want to use Git for version control, you first need to create a Git repository. This is where Git will store all the information about your project's history, such as the changes made to files over time.

To create a Git repository for your project, you can use the git init command. This command initializes a new Git repository in your current working directory. When you run git init, Git creates a hidden .git directory in your project's root directory. This directory contains all the information Git needs to manage your project's version control.

Once you’ve run git init, you can start adding files to your project and making changes to them. You can then use Git commands like git add, git commit, and git push to track changes, save snapshots of your code and share your work with others.

The most popular website to share git projects and collaborate with others is GitHub. GitHub is a web-based platform for hosting and managing Git repositories. It is one of the most popular platforms for open source development and is used by individuals, teams and organizations to collaborate on projects and share code. A few other honourable mentions would also be: GitLab and BitBucket.

It’s worth noting that git init is just the first step in using Git for version control. There are many other Git commands and concepts to learn in order to use Git effectively, such as: branching, merging, pull requests and resolving conflicts. However, git init is a crucial first step in setting up a new project with Git.

Pushing your code to a codebase

The first step in getting your local code to the repository in the cloud is to use the git add command. This command adds your files to the staging area, which is a sort of "holding area" where you can review changes before you commit them. Here's how you use git add <file>: Replace <file> with the name of the file you want to add. You can also use . to add all files in the current directory: git add .. If you want to add all changed files to the staging area, you can do: git add -A. Once you’ve added your files to the staging area, you’re ready to commit your changes.

A commit is a snapshot of your code at a particular point in time. It’s like taking a picture of your project so you can remember what it looked like at a specific moment. To commit your changes, you use the git commit command. Here's how you use it:

git commit -m "commit message"

Replace "commit message" with a brief description of the changes you made. This message should be descriptive and explain what you changed. For example, if you fixed a bug, you might use a message like "Fixed bug in login form." Once you've committed your changes, they are saved to the git log.

The git log is where all the repositories commits are stored in chronological order. You can view the git log by using the command git log. It is a command that provides a lot of information about the commits made to a repository, including who made the commit, when it was made and the changes that were made. It also includes its own commit-hash which can be used by other git commands if we wish to change the git log, or add specific commits to other branches.

To push your committed changes to a platform like GitHub, there are a few things you need to do. The first thing is to make an account, and the second thing is to go to your profile page, and then press “create new repository” or go to this link: https://github.com/new .

A screenshot of the “Create a new repository” page on GitHub.

Once you have created your empty project on GitHub, you’re going to want to add the origin of your new repository to your local project by using this command: git remote add origin <origin>. You will need to replace <origin> with the origin of your project. If you’re following along, you’re going to see a blue banner at the top of your screen with the label “Quick setup — if you’ve done this king of thing before”, where you get the option to choose between “https” and “ssh”. If you haven’t setup an ssh key with GitHub, use the https option. It will look a bit like this: https://github.com/your-username/your-repository-name.git. After you have added the origin to your project, you can change your current branch name to anything you want. The general convention is to name it main, and you can change it with: git branch -M <branch-name>. Then, all you have to do is to push it to your origin, using this command:

git push -u origin <branch-name>

If you changed your branch name to “main”, the command will be: git push -u origin main. If it pushed and you see your code on GitHub, congrats, you’re done! In the future, to push more changes to your repository on GitHub, you only need to do git push.

If you encountered any issues pushing to your GitHub repository, check the name of your branch using this command:

git branch --show-current

And check the origin of your repository using this command:

git remote -v

Thank you for reading, remember to like this post and to follow me for more tech related guides.

--

--

William Johansson
0 Followers

I like writing about modern web development. Im a software engineer/full stack developer with an affinity for infosec and frontend design.