First off, I want to start out by saying that this is not meant to be a comprehensive guide to using git. This article is just meant to cover some of the most common commands used in everyday work with git. Git is a very powerful tool and you can learn more about it’s advanced uses by checking out the official handbook.
Also, git flows can be very different from person to person or company to company. So use this guide as a basic starting point, but keep in mind that you may want to look at some of the more advanced information if it doesn’t match your flow. My commands are coming from the point of a solo developer (although I did use these commands the same way when working with another developer before) where I commit and push to my remote repositories very often. This is so I can use generic commands a lot of times and not necessarily have to use, for example, more “complex” pull or push commands. So keep that in mind as you read along.
This will be a two part article. I will go over branching and merging in the second part.
With that being said, let’s get started!
Setting Up the Repository
The first thing you are going to want to do is setup a repository on your local machine. There are most likely two ways you will normally do this. The first way is to just initialize an empty git repository. We can do that with a very simple command inside of the directory where we want our git repo to be located.
This will setup an empty local repo that we can start to add files to and even start making commits even though it is not connected to a remote git repository yet.
The next option is to clone an existing repository that is located on a remote host and bring it onto your computer. You will find a URL that points to the remote URL and tell git to use that. For example, on GitHub it looks like this on the repository
You would then copy/past this URL (or type manually if you have to) as part of the clone command.
git clone https://github.com/nickyoung87/basic-plugin-template.git
There is also a way to use this same command to clone the repository into a specific folder. You can just add the directory name at the end of the line.
git clone https://github.com/nickyoung87/basic-plugin-template.git my-new-folder
This would put the contents of the remote repository on your local machine into /my-new-folder (from wherever your base path is – i.e. C:/my-new-folder if you were in the C:/ directory).
There are only a couple of commands that I use here. The most common one is to add a new origin so that you can push an existing local repository up to the remote repository. It will look like this
git remote add origin git-repo
Where <git-repo> is the link to the repository (as shown in the image above for clone). Once this is setup then you will be able to push, pull, merge, branch, etc. with the remote repository.
The second command I use, but definitely not nearly as much, is just to show some information about the remote repository.
git remote show origin
It will give you some information about the current status of branches, if they are old or new, what needs to be pruned, and some other information. Not always needed, but sometimes can be helpful.
Pulling, Adding, Committing, and Pushing
These commands are the most useful and the most common commands you will most likely use in your daily work with git. Depending on your workflow the flow of which you use them might be a little different, but for me (and I assume most) the common way to go about things is to first pull down from remote, make your changes, add your changes, commit them, and then finally push them back up to the remote repository.
Before I dig into the guts of using that workflow I want to share with you my most used command.
This is my most command because it let’s me know where I am at. It will give various messages about the current status of your local repository. So if there are new files added it will list those out, it will list out anything that is committed, or it will let you know how far ahead or behind you are of the remote branch. These are all very useful to know so you know what part of the flow you are in and can determine your next commands based on the information received here.
Alright let’s get on to learning about all of this now!
Pulling From Remote
Pulling down from the remote repository is important if you are working with a team of developers and will need to grab an updated copy of the branch before you make changes to it. By doing a pull you will pull down changes made to all branches synced with the remote origin.
Adding Files to Commit
Now that your local files are synced to the remote files you will likely make a bunch of changes and then when you are at a good point you will want to add those changes to the repository so you don’t lose them. The simplest way to do this is to add all of the changed files to be “staged” for commit.
git add .
The period (.) at the end let’s git know that you want to add ALL of the modified files to the staging area. I prefer to do it this way as I only adjust the files I need for each commit. This is why I am a believer in smaller and more frequent commits. You can however add the files one at a time if you want by putting the filename you want to add.
git add index.php
Depending on your workflow this could be better for you as it is a bit more precise, but if you are modifying lots of files that can all go into one commit then I definitely recommend just adding them all at once.
Committing Your Changes
Now you have added your files to be staged for committing. Committing the files will actually send these changes to the history of the repository. You will be able to add a message so that other users or developers will be able to see the changes you made along with a message that (hopefully) explains what you did for that commit. To commit the files you added and add the message at the same time you can do it like this
git commit -m "This is my commit message!"
The -m let’s git know that you want to add a message for this commit. You can wrap the commit message in quotes so that the editor doesn’t go crazy when you start to put the spaces in.
Pushing Your Changes to Remote
With everything committed and ready to go there is only one thing left to do – push it (real good). This is the step where we want to basically upload the changes we made to the remote repository so that everyone else will be able to pull them down and have them. It’s another very simple command.
That’s it! Now depending on your remote repository you may have to enter some credentials in for it to send successfully, but once you do the push then it will be live on the remote repository so that anyone can have access to it.
Wrap Up and What’s Next
So you have now learned some of the most basic commands that are used for getting started with git. These are some of the most essential commands you will use starting out. You have learned about setting up a repository (empty or clone), some basics about the remote origin commands, getting the status of the files, and finally walking through a basic workflow where you would pull, add, commit, and then push.
As mentioned in the beginning this is going to be part of a two part article – mostly so it doesn’t get too long and overwhelming. In the next part we will go over the rest of the useful commands that I use most often. These will be about merging and branching which are two extremely powerful tools when using git so you won’t want to miss it!
Let me know in the comments if you have any questions about getting started with git and I will be happy to help!