Git Commands for When You Are Starting Out
Git is one of the most ubiquitous version-control systems available right now. It works by creating “snapshots” of your files called commits and gives you the ability to rewind or branch off your work as needed.
To get started in Git, here are a few commands that you should become familiar with:
The Basics
git init
Once you’ve created your project directory, run the “git init” command in the project’s root folder to initialize Git. A new .git folder will be created and you’ll now be allowed to start versioning your files.
git add .
Once your initialization process is complete, you’ll need to stage your files. The “git add .” command will add all the files within your project folder to something called the “staging area.” The staging area is the place where your files will be kept until the next commit. Note: You can replace the “.” with filenames to stage just that specific file, however I rarely do this.
git commit -m “[Commit message]”
This command will commit the files and folders you currently have staged. The “-m” modifier will allow you to include a commit message (A small message that lets you, or your colleagues, know what changes were done for that commit.) Remember to place the commit message in quotes.
git status
This will show all staged and untracked files.
git log
This will show a print out of all the commits created on your current branch. Each commit will contain a commit ID, the name and e-mail of the author, and a time stamp.
git checkout [commit ID]
Let’s say you were working on your files and you did something wrong, something that the undo button couldn’t fix. In order to save yourself, you can run the “git checkout” command and pass in a commit ID from a previous commit and voilà, you’re back to where you were.
Branching
git branch [new branch name]
Branching is a key component of using Git. The best way to describe branching is to think of it as taking your entire project (all the files and folders you have currently active) and copying it into a new folder. All your old files are still safe so now you can do whatever you want with the new copy. Note: This does switch you to the new branch, it simply creates one. To switch over see below.
git checkout [branch name]
Once a new branch is created, you can use the “git checkout” command to switch to it. Once you’re inside a new branch, make any changes you want to your files, stage them, and then commit them. They will now be saved separately from your previous branch.
git merge [branch name]
Running the “git merge” command will take all commits from the branch you specified and merge them into the current branch (he branch that is currently checked out.)
Wrapping Up
Now Git is much more expansive than the few commands I’ve listed here, but if you’re a beginner to version control, these are going to be your most valuable tools. To learn more, I highly suggest you check out the Git Book.