CodeNewbie Community 🌱

Cover image for Six Git Commands Every Beginner Should Know
Jeremy Schuurmans
Jeremy Schuurmans

Posted on

Six Git Commands Every Beginner Should Know

For people new to Git, it can be confusing and intimidating. If that's you, here are six Git commands for your toolbox. With these, you can become productive with Git and lay a solid foundation for your future Git mastery. Code along with these in order and learn them all!

Just a quick side note: this post is intended to be a quick, accessible breakdown of some of the most common Git commands I use every day, which were also the ones I had to look up most often when I was learning Git. Since it’s geared toward people who are just getting into Git, all my examples are trying to help people get practice using Git locally. I’ll save any discussion about remote repositories like GitHub and commands that interact with it for another post.

My hope is that this will reduce the time it takes for someone new to Git to become productive with it.

It will be most useful to you if you already have a basic understanding of what Git is, a concept of how the Git workflow works (branching, merging, etc.), some command line knowledge, and have Git installed and configured on your system.

I use Ruby in all my examples.

1. git init

git init is how you add Git to your project.

  • open your terminal, and create a new directory. I keep all my code-related files in a code folder, so I'll put it there. You can put it wherever you like. mkdir code/practice

mkdir code/practice

  • change to your newly created directory. cd code/practice

cd code/practice

  • create a new project directory named 'git-practice'. mkdir git-practice

  • cd into it. cd git-practice

  • run git init

mkdir git-practice, then cd git-practice, then git init

this will create an empty Git repository, a .git directory inside your project directory.

2. git status

git status lets you see the current state of your files.

  • create a file called "git_practice.rb". touch git_practice.rb

  • run git status. Git will tell you that you are on the master branch, you have not made any commits, and it will list all untracked files you have, in this case, we only have one, git_practice.rb.

touch git_practice.rb, then git status

3. git add

git add is how you start tracking individual files.

  • run git add . adding the space + . tells Git to add all files. You can also add individual files by running git add <filename>. This can be useful if you're ever in a situation where you want to add certain files in one commit, and other files in a different commit.

git add .

If you run git status now, it'll show you changes ready to be committed, in this case, that we created a new file.

4. git commit

git commit creates a new commit.

  • run git commit -m (the -m stands for message), and write "first commit" in quotation marks. git commit -m "first commit"

git commit -m 'first commit'

5. git checkout

You use git checkout to switch between branches.

Note: For convenience, you can create aliases for any Git command to make it less tedious to type them repeatedly. A common alias I use is git co for git checkout. I'll use this example for the rest of this post, but you can just as easily skip this and stick with git checkout instead. To use git co you need to set that as an alias for checkout in your .gitconfig file. This file is responsible for your global Git configuration, and lives in your system's home directory, not in your project directory. You can easily set an alias using the command line. To set co as an alias for checkout run

git config --global checkout
Enter fullscreen mode Exit fullscreen mode

This will create a line in your .gitconfig that looks like this:

alias co = checkout

If you like, you can alias all these other commands too. For example, if you want to set git st as an alias for git status, you would alter the above command with status where st is your alias, and status is the command you're aliasing. Feel free to play around with it.

Now instead of git checkout you can run git co If you add the -b flag, it will both create a new branch and switch to it at the same time. You can name your branches whatever you like, but descriptive names are best, like so: git co -b add-example-code

  • running that command will create and switch to a branch called add-example-code.

git co -b add-example-code

  • open git_practice.rb in your text editor of choice.

  • add some code. Let's go with puts 'Hello World!' Save the file.

  • let's run git status again to see what happened. Again, it'll show you what branch you're on, and what changes you made that aren't staged for committing yet. In this case, that we modified git_practice.rb.

git status

  • add and commit this file. git add . then git commit -m "add hello world"

git add ., then git commit -m 'add hello world'

6. git merge

You use git merge to integrate your branch with the master branch. I’ll show you how to use it, but keep in mind that in situations where you are dealing with production code that is stored in a remote repository like GitHub, merging should be done by creating a pull request.

  • switch to master. git co master

  • run git merge add-example-code. This will integrate our add-example-code branch with master, so now all changes made in the branch are present in master.

git co master, then git merge add-example-code

With these commands you will be well on your way to productivity with Git.

We're barely scratching the surface, however. Many good, in-depth resources for learning Git can be found here.

Top comments (0)