Git Basics

When I started getting involved with more complex front-end development work, I was staunchly against using the terminal for anything. I would seek out programs that would allow me to interact with services like GitHub through a graphical user interface rather than having to use the command line. It made no sense to me at the time why anyone would use the command line to carry out any task whatsoever. It seemed like it would be impossible to be more efficient by typing in commands and having to remember folder structures and filenames. However, once I got deeper and deeper into front-end work, it became apparent that in order to fully utilize tools such as GruntJS and Git, (which provided invaluable efficiencies in my overall workflow), I would need to practice my command line skills.

This post just outlines a few git commands I use within Terminal on a regular basis and is by no means a comprehensive overview of everything you can do with Git. The first thing to start off with is having an existing Git project on your local machine and syncing it up with a new repository on GitHub.

Adding an existing git project to GitHub

If you already have a project on your computer that has been initialized with git, then you're already halfway there. After you're done adding your files to staging and have created your first commit, go to GitHub and create a new repository. After navigating to your repository page on GitHub, you will need to copy the URL in the address bar.

After you've copied the URL, you can go back to Terminal and navigate to the root of your git project. Once there, put the following commands into the Terminal:

git remote add origin remote repository URL

After that, then just verify with the below command:

git remote -v

Lastly, push your first commit to the remote repository and then you should see the changes up on GitHub:

git push origin master

The last bit of code can also be used in future pushes to send your changes to GitHub when you have new commits.

Adding and committing

One of the most common tasks you will do with git is add altered documents to staging and then commit those changes. I haven't yet found a reason for myself to add only specific files to a commit, so I always just add all files to each commit. I used to do these in separate add and commit commands in the beginning, but there's a much easier way to go about this with both adding and committing all in the same command:

git commit -a -m "your title here" -m "your description here"

This line of code is essentially completing the two tasks in one command. You are automatically pushing all of your changes into the commit with the "-a" argument and then you are adding both the commit title and description with the two "-m" arguments.

Pushing your commits to GitHub

So once you've made your commit, you're happy with the current state of your files, and you're ready to push the changes to GitHub, you'll need a command in order to facilitate the transfer of those changes to your remote repository. This is actually quite simple and was mentioned earlier in this note:

git push origin master

And there you go, you're done! All of your changes will be pushed to GitHub and the commit will be synced with your remote repository. The only thing to keep in mind before doing this, (or before starting to edit your files in the first place), is to check the status of the repository using:

git status

This will enable you to make sure that you are either up to date with the remote repository on GitHub or that you are ahead of it and ready to push your changes to GitHub. If you find that your local repository is behind the remote repository you can use:

git pull

Once you execute that command in Terminal, it will pull all of the changes from the most up to date commit so you can be sure you are working on the most recent files.

That's it and I hope this post helps others as much as it helps to remind me of what to do when working with Git.