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.
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
codefolder, so I'll put it there. You can put it wherever you like.
- change to your newly created directory.
create a new project directory named 'git-practice'.
this will create an empty Git repository, a
.git directory inside your project directory.
git status lets you see the current state of your files.
create a file called "git_practice.rb".
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 add is how you start tracking individual files.
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.
If you run
git status now, it'll show you changes ready to be committed, in this case, that we created a new file.
git commit creates a new commit.
git commit -m(the -m stands for message), and write "first commit" in quotation marks.
git commit -m "first commit"
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
git config --global alias.co checkout
This will create a line in your
.gitconfig that looks like this:
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
alias.st 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
git_practice.rbin your text editor of choice.
add some code. Let's go with
puts 'Hello World!'Save the file.
git statusagain 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.
- add and commit this file.
git add .then
git commit -m "add hello world"
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
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.
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.