Pulling yet another all-nighter to restore your project’s lost code changes? You’re not alone. That’s why millions of developers trust Git, the world’s leading version control system, to track every change and protect their work. Here’s a rundown of commands you’ll use the most.

If you’re new to Git, let’s start with a refresher. A Git repository (or repo for short) contains all the project files and the entire revision history. A repo has commits, which are used to record the changes in the repo, and every commit has a brief message, which the user types in to state what changes they’ve made. Git can also help manage any conflicts (e.g., if two people edit the same line of code) before merging. Learn Git on Windows.

Numbered arrows showing HTTPS repository cloning option on GitHub.

1To Clone an Existing Repo

The first command we can start with isGit clone, which is a command that connects to and downloads a copy of an existing repository to your local machine. Usually, the existing repository is remote onGitHuborGitLab.

First, go to a repo and click the green drop-down menu named “Code”, and then the copy to clipboard icon next to the GitHub repository URL, which will clone using the web URL. This is the easiest method, and it clones using HTTPS:

Git repo clone complete message on Git bash CLI.

Next, run the command below with the URL you just copied:

Once the repo is cloned, you should have a local copy of it on your machine.

Git init command showing empty Git repo error message on CLI.

If you get a “fatal: repository not found” error, double-check the URL. If it’s a private repo, you may need permissions to access it.

2To Create a New Repo

If you want to create a new Git repository instead of cloning an existing one, rungit init. It initializes the repository in the specific directory by giving it a path. So it’s best for new or untracked projects that you want to start tracking using Git.

First, make sure that you’re in the correct folder before you run the command:

Git status command on CLI with output saying nothing to commit, working tree clean.

3Creating a Branch for Collaboration

A branch in Git is a version of your repository, so multiple people can work on a repository simultaneously. In other words, it’s an independent line of development within a repo.There are usually various branches in a repo.

To list all your branches, run:

To delete a branch:

When deleting a branch, sometimes force deletion is needed. Just capitalize the-D, like this:git branch -D branch-name

4Switch Between Branches

Git checkoutis one of the most commonly used commands, mainly used for switching between branches, but can also be used for checking out files and commits.

To switch between branches and check it out in your local directory:

Git log command on CLI showing previous commits and commit IDs.

For newer versions of git, you can run:

For the above commands to work, the branch that you are switching to should exist locally, and any changes in your current branch must be committed or stashed first.

Shortcut command to create and switch a branch at the same time:git checkout -b name-of-branch

5Check Git Status

This is another common command, which can tell you different information about the current branch such as if the current branch is up-to-date or not, whether there is anything left to commit or push/pull, and whether there have been any files that were modified or deleted.

This is what the output should look like if there are no changes to be made:

6Commit Sets of Changes

This may be the most used Git command. When we are ready to save our work, maybe after a specific task or issue, we canuse Git commit. This essentially captures a snapshot of the project’s currently staged changes.

You also need to write a short and clear commit message with it to let you and other developers know about the changes. Do not forget to surround it with quotation marks.

Git commit only saves your changes locally. You still need to “push” them to a remote repo.

7Rolling Back Changes

Git revert allows you toremove all the changes a single commit madeto your local repo. For example, if a past commit added a file named ReadMe.md to the repo, a git revert on that commit will remove the readme.md from the repo. A new commit is also created to reflect this change.

All you need to do is rungit revertfollowed by the commit ID:

If you’ve made a lot of commits and aren’t sure where the commit ID is, you may identify the commit by running the commandgit log. Copy the commit ID and run thegit logcommand with the commit ID.

Do not confusegit revertwithgit reset. The latter will undo every change that has happened since a given commit occurred and change commit history. This is not ideal if other people are working on the same branch.

Once you are done making all the changes and committing them, you’ll want to upload your local changes to the remote repo. Pushing is an act of transferring these changes with your commits from your local machine to the remote repository. You can specify which branch you want to push the changes to.

The above command pushes the changes to the main branch (master is generally considered the main branch, but main is now also commonly being used). Ifmasterdoesn’t work, trymain.

9Retrieve All Changes

This is one I use when I’m coming back to a project and need to retrieve all the new changes that were made in the master branch (either with my merge or other developers’) that exist remotely. In other words, it’s a command you use when you want to get updates from the remote repo.

Like earlier, ifmasterdoesn’t work, trymain. Since this command combines the functions ofgit fetchandgit merge, it instantly applies the most recent modifications to your local repository (git merge) after retrieving updates from the remote repository (git fetch). You canlearn more about pull requests on Git.

10Merge it All Together

Finally, once you’ve completed working on your branch and everything is working correctly, the final step ismerging the branchwith the parent branch (usually dev or master, but double-check the repo).

You can do this by running thegit mergecommand. You should first rungit fetchto update your local branch, and then do your merge:

Ensure you’re on the branch you want to merge with your remote parent branch.

In the end, getting the hang of Git is like riding a bike—once you start, it only gets easier with each push!