Version Control is an essential part of application development, and Git is a great Version Control System solution. This post is a list I’ve compiled of the most commonly used Git commands with their options and examples of how I use them. I also have a list of less commonly used GIT commands that are still very important to know. These are commands that I use on a daily basis to keep my local repositories clean and up-to-date. First, I’ll give the command form. Then, I’ll show an example of it being used. In these examples, I’ll mostly use a remote repository called “origin” and a branch called “master”. These Git commands are all meant to be executed in the command line inside the Git repository on your local computer.
git pull [<options>] [<repository> [<refspec>…]]
git pull origin master
This will pull all the changes to the “master” branch from the remote repository “origin” to your local copy of the repository.
git status [<options>…] [--] [<pathspec>…]
I usually use this command with no options or arguments to see which files are modified or are in the staging area (index).
git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p] [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]] [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize] [--chmod=(+|-)x] [--] [<pathspec>…]
git add assets/js/util.js dist/js/main.js single.php style.css
This will add assets/js/util.js dist/js/main.js, single.php, and style.css to the staging area (index). Any files that you modify should be moved to the staging area before being committed.
git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend] [--dry-run] [(-c | -C | --fixup | --squash) <commit>] [-F <file> | -m <msg>] [--reset-author] [--allow-empty] [--allow-empty-message] [--no-verify] [-e] [--author=<author>] [--date=<date>] [--cleanup=<mode>] [--[no-]status] [-i | -o] [-S[<keyid>]] [--] [<file>…]
git commit -m "Add form validation JS and error styles."
This will commit all the changes to the files that were added to the staging area (index). The “-m” is for adding the commit message at the end as part of the command.
Git checkout has several forms but I will show the main two that I use.
git checkout [-q] [-f] [-m] [<branch>]
git checkout test-branch
If you’re currently on the branch “master”. This will switch you to the branch “test-branch”.
git checkout [<tree-ish>] [--] <pathspec>…
git checkout -- assets/js/util.js
This will overwrite the changes in your working directory to the file “assets/js/util.js” with what is in the remote repository. You can also specify an entire directory or “.” to overwrite all local changes.
git reset [-q] [<tree-ish>] [--] <paths>…
git reset style.css
Git reset also has three forms, but the first two are similar. I use the first one the most. This example is removing the file “style.css” from the staging area (index). Technically speaking, the version of “style.css” in HEAD is being copied to the staging area (index). Practically speaking, you can use this to unstage files.
git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>] [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose] [-u | --set-upstream] [-o <string> | --push-option=<string>] [--[no-]signed|--signed=(true|false|if-asked)] [--force-with-lease[=<refname>[:<expect>]]] [--no-verify] [<repository> [<refspec>…]]
git push origin master
This will push all committed changes in branch “master” in a local repository to a remote repository (specified in the GIT config) called “origin”.