Git: Beginner CheatSheet
A reference for the git commands you'll most likely use every day.
Table of contents
As a newbie, you're likely to get lost in the numerous git commands available. Especially when trying to find how to complete a simple task. Once you come to understand Git and use it for a while, you'll realise that there certain basic commands that cut across or are almost always used. It is therefore essential to know them.
Version Control or Git can be defined as managing and tracking changes to code. It helps track different versions of a file as well as collaborate with others. If you're very new to the topic, read my article explaining Git and its commonly used terminologies, with a relatable analogy here.
This cheatsheet will be a compilation of the most popular everyday git commands and their purposes.
Installation
Git can be installed on computers that run any of the three most popular operating systems. For detailed setup tips, check out my Web Development Starterpack Tools setup here. Type the following command in your terminal or cmd. This checks if you have git installed. If you do, it’ll respond with the version number.
git --version
If you don't have git installed, Find your preferred installation links here:
Linux: git-scm.com/download/linux
Windows: git-scm.com/download/win If you encounter issues with any of these, see here for the official documentation on installation.
Configuration
After installation, it's ideal to customize your environment, especially your identity. This is a necessary step because you want your details to be tagged on all commits that you make. The main way is to configure your user details.
Name
To set your username to all future commits
git config --global user.name "[name]"
To set your email to all future commits
git config --global user.email "[email address]"
You only have to do this customization once if you use ---global
. However, if you have plans of changing the details later, you can leave the global keyword out. You can also customize your editor or the colour of your command line output.
Creation
There are two main things that fall under the Git create category:
Repository
To create a repository, you have two options:
Make a new project altogether and turn it into a repo
git init
You can also clone an existing project to be altered to your specification ``` git clone [url]
# Branching
After repos, branches are the next important items. Commits made are not only tagged with the user identity, but they're also tied to branches, specifically the branch you're on at the time of making the commit.
To check your current branch use:
git status
You can create a new branch with:
git branch [branch-name]
If you're not on your ideal branch, you can make a switch to your prefered branch by using
git checkout -b [branch-name]
If a branch is no more useful to you, delete it easily
git branch -d [branch-name]
# Changes
Git is all about changes, whether it is making the change, tracking it or synchronizing. Under changes, we'll break the types into two:
## Tracking
This is where changes that are made to the local branch are checked and reviewed for merging.
You can check the version history of your current branch
git log
If there have been changes made to a particular file within the project, you can see a list of that
git log --follow [file]
For any two branches, it is possible to check and compare their content differences
git diff [first-branch]...[second-branch]
Before pushing local changes to a GitHub repo, these changes have to be staged
git add [file]
Changes to a branch can be tagged with descriptive messages for easy identification. As well as to communicate to other contributors of the project.
git commit -m "[descriptive message]"
## Merging
This is the process of getting changes on your local branch into the repo on GitHub. Assuming there are others working on or contributing to the same repository. You can update your local branch with all the new commits from other branches.
git pull
After getting all changed from the remote tracking branch, you can easily add it to your local branch
git merge
Once you're ready to upload changes or commits made on local branches to GitHub use:
git push
# Correction
If at any point you realise that you've made mistakes, you can easily fix that by undoing or reverting to initial changes. This can be done by reverting all commits that were made after a specified commit
git reset [commit]
Another way is to discard entirely all changes made after a specific commit
git reset --hard [commit] ```
Conclusion
This list is only a fraction of the numerous git commands there are. However, as a git newbie, it's almost guaranteed that these are the commands you'll need at least for the first three to six months. Fortunately, you don't have to memorize them all at once. With constant use, some of these will stick with you. Till then, this cheatsheet can be your go-to for referencing simple Git commands.
Thanks for reading 👋🏾. I hope you found this helpful.