Basic Git Commands You Need To Know
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 repositorycd [project_root] && git init
initializes in an existing project
Connect the repository to a remote machine or server:
git remote -v
lists the available remote repositoriesgit remote add origin [remote_url or host_name]
connects your repository to the server atremote_url
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/repository
copies from a remote hostcd [new_root] && git clone /path/to/repository
copies from another directory on the same machine
Add files to the staging area:
git add [file_name]
addsfile_name
to the repository (regular expression wildcards, such as *, can also be used to add multiple files at once)
Work with branches:
git branch
lists available branchesgit branch [branch_name]
creates a new local branch calledbranch_name
git checkout [branch_name]
switches tobranch_name
(called “checking out”)git checkout -b [branch_name]
creates and checks out the branch in one commandgit merge [other_branch]
mergesother_branch
into the checked out branchgit branch -d [branch_name]
deletesbranch_name
Commit changes:
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 thebranch_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 frombranch_name
and 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 you@email.com
sets your details for the current repositorygit config –global user.email you@email.com
sets your details for all repositories
Check on the current situation of the git repository:
git status
displays files that have been changed and are yet to be staged or committedgit diff
lists all conflicts between files in different branches, stages, or local repositoriesgit log
shows 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.