Git/GitHub Cheat Sheet for Beginners

Learn the essential Git and GitHub commands in a concise and easy-to-understand cheat sheet. Perfect for beginners.

If you aren’t already familiar with version control and incorporating it into your daily workflow, now is the time to get started. This is a barebones basic guide to get you started with Git and give you a solid foundation to develop further. Git is almost certainly used in any professional environment and the more you familiarize yourself with it early on the more valuable you will be to employers. Also, this will make your personal experience better by being able to switch computers without having to worry about saving your project on a flash drive. Working on groups projects will become so much easier to manage. Ever messed up your code so bad you felt like it would just be easier to start from scratch? With version control, you can just revert back to a stable version free from all of those crazy ideas you wanted to implement at 2 am.

Git and GitHub

Git is the actual version control system. It is an open source DVCS (distributed version control system) that runs in the command line. This basically means it saves the entire history of the project. The history of your project and the history of the same project that’s being worked on by your peers will all have copies. This is the opposite of SVN (subversion) where the entire history is stored in only one place.

GitHub, commonly confused with Git, is actually a repository hosting service. You might not understand what a repository is right now but hang in there, you will by the end of this. GitHub actually has a lot to it but for now, we are going to keep it simple. This is where you will be uploading your history using Git. This allows you and your peers to retrieve the latest copy of your project while also allowing multiple people to work on it without hindering each other.

Getting Started

Git is complex and there are many things to learn but to get started you really only need to know a few key things to get started. The more you use Git, you will encounter situations where this will absolutely not be enough but there are many resources out there that can aid you when that occurs. So, use this to get you started but do keep expanding your knowledge.

The first thing you are going to want to do is to download Git. For Windows users I suggest also installing Git Bash, which is available when installing Git. For Mac users, using the Terminal will be completely fine. Once installed go ahead and head to GitHub create a free account. Now you have Git, the command line tool, and a GitHub account, where you will be uploading your repositories.

Cheat Sheet

Using Git Bash or the Terminal navigate to the actual project folder. If you are using Git Bash you can right-click the project folder and select “Git Bash Here” and it will start you in that working directory.

git init

This will create a .git repository in your project. A repository or “repo” is a collection of all the changes you’ve made to your project over time and will build a history of these changes. This is the first thing you want to do with a new project.

git config --global user.name "Your Name"

git config --global user.email "yourEmail@mail.com"

This sets up your information that is used every time you commit. This only needs to be done once when you first install Git.

git add filename.extension

Replace “filename.extension” to whatever file you are trying to add like “index.html”. This will add the file you specify to what is called a “staging area” or index. Think of the staging area like a section where things are getting set up to be moved to your repository.

git add .

If you want to add everything from the project folder to the staging area this command will do that instead of having to add each file one by one.

git add *.html

If you want to add all .html files to your staging area this would be the command to use. The extension can be changed to whatever you want.

git status

Shows what has already been added to the staging area and what files have been changed that need to be added to the staging area.

git reset filename.extension

Removes specified file from the staging area.

git rm --cached filename.extension

This will remove the file from the staging area and sets it to be untracked.

git commit -m "Description of the commit"

Takes the files from your staging area and commits them to your local repository. In quotes will be a brief description of what was changed with each commit. Try to describe the commit in brief detail such as “fixed bug where user name wasn’t updating” rather than a commit message like “some changes.”

touch .gitignore

This will create a file called .gitignore. You can open that file with a text editor and write the name of files or folders you want to be ignored from your repository. Ignored files won’t show up when you run git status to prevent you from committing files you’ve previously said you don’t want to commit or even know about their changes.

git branch branchName

Creates what is called a branch. A branch is a direct copy of your codebase from previous branch you were on (often the master branch).

git checkout “branchName”

Will allow you to checkout the branch you created and work within that branch. You can make any changes to your code that you want here. When it’s ready, you can commit your code and push the branch to GitHub (see below) or you can delete the branch if something goes wrong or you decide you don’t need that feature or bug fix any longer.

git merge branchName

While inside Master you can use this command to take the commits from the branch you were working in and merge them together with the main repository.

git remote add origin https://github.com/userName/project.git

This adds the location of your remote repository. Everything up until now has been on your local repository on your computer. You will need to go to your GitHub account and create a new remote repository where you will be able to push your local repository. After you created your remote repository you will be provided with a link and that link is the location you will want to use in the above command.

git remote

List of your remote repositories that have been associated with your project.

git push -u origin master

This will push your local repository to your remote repository. This command only needs to be written like this when you do it for the first time.

git push

This is what you will use to push your code to GitHub after your initial push.

git clone https://github.com/userName/project.git

If you don’t have your project on the computer you’re working with this will allow you to clone (or download) the entire project into the directory you are working.

git pull

If you are working on the same codebase with other people, this command will allow you to pull the latest version from the remote repository and update your local version so you can work with the latest updates as their changes enter the codebase.

Conclusion

Hopefully, this has given you enough information to get started and have a basic understanding of what’s going on. There is a lot more to Git but you can build on top of this information. Many people have no idea where to begin or think it’s daunting to understand but just take this information and get started. You will quickly see the benefits and also will have increased your value as a programmer.

#git #github #web-development #programming #developer

Git/GitHub Cheat Sheet for Beginners
2 Likes36.35 GEEK