To use Git well, you need two things: an understanding of what Git is and knowledge of which commands to use in various circumstances. In this post, we’ll review both.
An Understanding of Git
So first up, what is Git?
Git is a form of version control software. It’s designed to help you keep track of changes in code, documents, images, and other files related to a project. When you use Git, you create a repository and take “snapshots” of the content. Because they’re stored in a central location with a full history that can be used to roll back through changes, check differences, and recover deleted files, these snapshots can then be safely shared between different contributors and physical systems.
A Git repository consists of three primary sections. The first is the main Git directory, which stores the collection of snapshots you “commit” into it. Then there’s the staging area, which temporarily holds files prior to them being committed. Finally, the working directory holds the latest copy of your files— it’s where you create, edit, and delete files.
Basic Git Commands
Here’s a list of basic Git commands that can get you started.
Initialize a project repository:
git init [repository name]creates a blank repository
cd [project_root] && git initinitializes in an existing project
Connect the repository to a remote machine or server:
git remote -vlists the available remote repositories
git remote add origin [remote_url or host_name]connects your repository to the server at
git remote rm [remote_url or host_name]deletes the connection to the remote machine or server
Copy an existing project repository to a new machine:
git clone http://username@host/path/to/repositorycopies from a remote host
cd [new_root] && git clone /path/to/repositorycopies from another directory on the same machine
Add files to the staging area:
git add [file_name]adds
file_nameto the repository (regular expression wildcards, such as *, can also be used to add multiple files at once)
Work with branches:
git branchlists available branches
git branch [branch_name]creates a new local branch called
git checkout [branch_name]switches to
branch_name(called “checking out”)
git checkout -b [branch_name]creates and checks out the branch in one command
git merge [other_branch]merges
other_branchinto the checked out branch
git branch -d [branch_name]deletes
git commit -m “[Message describing the changes]”creates a snapshot and saves it to the Git repository
Send and receive changes in different branches:
git push origin [branch_name]sends your changes to the
branch_name(note that “master” is commonly used as a default name for the main repository, but it is being changed on multiple platforms that implement Git.)
git pull [branch_name]gets all the changes from
branch_nameand applies them to your local repository
Some Extended Commands
Here are some additional Git commands.
Add your details to the Git configuration:
git config –local user.email firstname.lastname@example.org your details for the current repository
git config –global user.email email@example.com your details for all repositories
Check on the current situation of the git repository:
git statusdisplays files that have been changed and are yet to be staged or committed
git difflists all conflicts between files in different branches, stages, or local repositories
git logshows the repository’s history
Want To Learn More?
Git is an incredibly useful tool. However, it’s also quite complex and has a steep learning curve. As I said at the beginning, using it well requires an understanding of what Git is and knowledge of which commands to use in various circumstances. Cheat sheets like this are fantastic for refreshing your command knowledge, but to get the most out of them, you need a more complete knowledge of Git. Cprime has a Git and GitHub course that runs you through everything you need to know. As a result, you can become the resident Git expert in your office.