DEV Community

Alex Hebert
Alex Hebert

Posted on

Intro to Git: Command Basics

Understanding the workflow using git is essential in modern software engineering. Git is an immensely useful tool whether working solo or collaboratively.

So, What is Git?

Git is a way to track changes and save a set of files inside a directory. Git creates a timeline of snapshots of the files it tracks. Each of these snapshots is called a commit, and commits are saved along a timeline called a branch. Git allows multiple of these branches to run in parallel and be merged later allowing for experimental changes and cooperative coding. Git is the backbone of cooperative coding because it allows multiple people to work on a project simultaneously without interfering with one another.

Setting up your first repository

To set up your first repository (or repo) is quite simple. To start, install git here:

For Windows users, this will give you access to a terminal called git bash. Open git bash and navigate to the directory you want to track with git. To do this use ls to get a list of files inside the current directory and cd <directory> to enter a child directory. To go back into the parent directory use cd ... If you want to make a new directory use mkdir <name> to create a directory inside the current directory. Once at the directory you want to track with git use the command git init to initiate a new git repository. Congratulations, you now have a git repo!

Making commits

To save changes to your repository you need to make a commit. First make sure your terminal is at your repo, using ls is a quick way to do this. Then use the git add <file> command to stage the changes you've made to that file. If you have multiple files that have modifications you can use git add . to add all of them in one command. Once all your changes are staged use git commit -m <message> to make the commit. Each commit needs a message that briefly explains the changes made since the last commit. This commit will be added to the branch, you can see the commits that make up the current branch with git log which will show you a list in reverse chronological order of the commits on the current branch.

Branching out

Git allows you to make branches separate from the main codebase allowing you to make changes without affecting the main branch. To see a list of all the branches you can access use git branch the current branch you're on should be highlighted. To make a new branch use git branch <name> and then to switch between these branches use git switch <branch>.

These are a few of the most common commands you'll use when working with git. Your journey into git shouldn't stop here. To read up more on git check out this free e-book that will walk you through everything git!

Top comments (0)