Git distributed revision control system
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. This is an introduction to Git using visual guides to workflows and Git commands. Keep your current workflow style while enjoying the lightweight and feature-rich benefits that Git has to offer.
Step1. The git init Command
The git init command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new empty repository. Most of the other Git commands are not available outside of an initialized repository, so this is usually the first command you’ll run in a new project.
git init
Create an empty Git repository in the specified directory. Running this command will create a new folder called <directory> containing nothing but the .git subdirectory.
git init --bare <directory>
Step2. The git clone Command
The git clone command copies an existing Git repository. This is sort of like svn checkout, except the “working copy” is a full-fledged Git repository—it has its own history, manages its own files, and is a completely isolated environment from the original repository.
git clone <repo>
Clone the repository located at <repo> onto the local machine. The original repository can be located on the local filesystem or on a remote machine accessible via HTTP or SSH.
git clone <repo> <directory>
Step3. The git config Command
The git config command lets you configure your Git installation (or an individual repository) from the command line. This command can define everything from user info to preferences to the behavior of a repository. Several common configuration options are listed below.
git config user.name mycodinglab
Define the author name to be used for all commits in the current repository. Typically, you’ll want to use the –global flag to set configuration options for the current user.
git config --global user.name mycodinglab
Define the text editor used by commands like git commit for all users on the current machine. The <editor> argument should be the command that launches the desired editor (e.g., vi).
git config --global --edit
Step4. The git add Command
The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn’t really affect the repository in any significant way—changes are not actually recorded until you run git commit.
git add <file>
Stage all changes in <file> for the next commit.
git add <directory>
Stage all changes in <directory> for the next commit.
git add -p
Step5. The git commit Command
The git commit command commits the staged snapshot to the project history. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to. Along with git add, this is one of the most important Git commands.
git commit
Commit the staged snapshot. This will launch a text editor prompting you for a commit message. After you’ve entered a message, save the file and close the editor to create the actual commit.
git commit -m "<message>"
Commit the staged snapshot, but instead of launching a text editor, use <message> as the commit message.
git commit -a
Step6. The git status Command
git status
Step7. The git log Command
The git log command displays committed snapshots. It lets you list the project history, filter it, and search for specific changes. While git status lets you inspect the working directory and the staging area, git log only operates on the committed history.
git log
Display the entire commit history using the default formatting. If the output takes up more than one screen, you can use Space to scroll and q to exit.
git log -n <limit>
Condense each commit to a single line. This is useful for getting a high-level overview of the project history.
git log --stat
Step8. Add remote repository
To push our local repo to the GitHub server we’ll need to add a remote repository. Run git remote add with the options below
git remote add origin remote-repo-address
Step9. Push your changes
The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do. Go ahead and push it!
git push -u origin master
Let’s pretend some time has passed. We’ve invited other people to our github project who have pulled your changes, made their own commits, and pushed them. We can check for changes on our GitHub repository and pull down any new changes by running:
git pull origin master
If there has been some additions and changes. Let’s take a look at what is different from our last commit by using the git diff command.
git diff HEAD