Version control depends on Git, which is super helpful for developers to manage their codebases, keep track of changes, and work together smoothly. A cheat sheet with common Git commands can really help you out, no matter how experienced you are with Git. It’ll save you time and make things a lot easier!
This post goes over some basic Git commands and explains how to use them effectively, so you can apply them with ease.
Make sure to set up your identification before you dive into Git. This allows Git to connect your commits with your email and name.
Choose your username:
git config --global user.name "Your Name"
This command sets your name for all the Git repositories on your machine.
Go ahead and set up your email:
git config --global user.email "your.email@example.com"
Similarly, this sets your email for commits.
Verify your configuration:
git config --list
This shows your Git configuration, letting you check if everything's set up just right.
Initializing and Cloning Repositories
Let's get started by initializing a new Git repository::
git init
Just run this in the root directory of your project in order to start tracking it with git
. It makes a .git
folder where Git keeps all its data.
To clone a repository that’s already out there:
git clone <repository_url>
This command lets you grab a remote repository and bring it over to your local machine. Like, for instance:
git clone https://github.com/user/repository.git
Essential Workflow Commands
Verify your repository's current status:
git status
This shows what’s going on in your working directory, covering staged, unstaged, and untracked files.
Add changes to the staging area:
git add <file_name>
Use this to stage a specific file for your commit. To stage all changes use this:
git add .
Save your changes:
git commit -m "Your commit message"
Use this to save the changes with a descriptive message. The message should summarize the changes made.
Check out your commit history:
git log
Shows a list of commits in the repository, including the author, date, and commit message.
Branches allow you to work on features or fixes separately from the main codebase.
Let's create a new branch:
git branch <branch_name>
This makes a new branch, but it doesn't switch over to it.
Let's switch to a branch:
git checkout <branch_name>
It takes you to the branch you want.
Let's create a new branch and switch to it:
git checkout -b <branch_name>
This is a quick way to create a new branch and switch to it right away.
Let's merge a branch into the current branch:
git merge <branch_name>
It brings in updates from the branch you specified into your current branch.
How to delete a branch:
git branch -d <branch_name>
It removes the branch you specified, but only if it’s already been merged.
Let's add a remote repository:
git remote add origin <repository_url>
Links your local repository with a remote one.
Let's push those changes to a remote repository:
git push origin <branch_name>
This uploads your local branch to the remote repository. If you want to push to the main branch, use:
git push origin main
Let's pull some changes from the remote repository:
git pull origin <branch_name>
It grabs the updates from the remote branch and combines them with your local branch.
View remote repositories:
git remote -v
This will show a list of the remote repositories that are linked to your project.
To unstage a file:
git reset <file_name>
It takes a file from the staging area and puts it back into the working directory.
So, let’s say you accidentally staged example.txt
but you really don’t want it to be part of the next commit:
git reset example.txt
This takes example.txt
out of the staging area but keeps its content in place.
Revert a commit:
git revert <commit_hash>
It makes a new commit that reverses the changes from the commit you specified.
So, let's say you've got a commit hash like abc123
and you're looking to revert it:
git revert abc123
Git will then open your editor so you can confirm the commit message for the revert. Just save and close to wrap things up!
Discard or forget the changes you made to a file:
git checkout -- <file_name>
Brings the file back to how it was the last time it was saved.
So, if you've edited example.txt
but decide you want to go back to how it was before:
git checkout -- example.txt
This will take example.txt
back to the last version that was committed, so any local changes will be lost.
Some Helpful Tips
Put your changes away.
git stash
It temporarily retains your modifications, allowing you to shift focus and address other tasks. Here's how to put them back on:
git stash pop
Show a graphical commit log:
git log --oneline --graph --all
Shows a clear, visual representation of the commit history.
Wrapping it up
This Git cheat sheet provides you with the most commonly used commands to help you manage your projects effectively. As you continue to practice, these commands will become intuitive to you. If you're dealing with more complex workflows, check out the official Git documentation.
If you think this guide is useful, feel free to share it with your developer buddies and save it for easy access later!