UNDER CONSTRUCTION!!!

Tech News

Keeping You Up To Date With The Latest Tech News & Virus Threats
Font size: +

10 Basic Git Commands to Get You Started

10 Basic Git Commands to Get You Started

Quick Links

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 more about installing Git on Windows.

1 To Clone an Existing Repo

The first command we can start with is Git 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 on GitHub or GitLab.

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:

Numbered arrows showing HTTPS repository cloning option on GitHub.

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

git clone https://name-of-the-repository-link
Git repo clone complete message on Git bash CLI.

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

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.

2 To Create a New Repo

If you want to create a new Git repository instead of cloning an existing one, run git 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 init
Git init command showing empty Git repo error message on CLI.

3 Creating 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 create a local branch, run the command:

git branch name-of-branch

To list all your branches, run:

git branch

To delete a branch:

git branch -d branch-name

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

4 Switch Between Branches

Git checkout is 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 checkout name-of-branch

For newer versions of git, you can run:

git switch name-of-branch

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

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.

git status

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

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

6 Commit 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 can use 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 -m "commit message"

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

7 Rolling Back Changes

Git revert allows you to remove all the changes a single commit made to 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 run git revert followed by the commit ID:

git revert commit-id

If you’ve made a lot of commits and aren’t sure where the commit ID is, you can identify the commit by running the command git log. Copy the commit ID and run the git log command with the commit ID.

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

Do not confuse git revert with git 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.

git push origin master

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). If master doesn't work, try main.

It’s recommended to run git status before pushing your changes.

9 Retrieve 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.

git pull origin main

Like earlier, if master doesn't work, try main. Since this command combines the functions of git fetch and git merge, it instantly applies the most recent modifications to your local repository (git merge) after retrieving updates from the remote repository (git fetch). You can learn more about pull requests on Git.

10 Merge it All Together

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

You can do this by running the git merge command. You should first run git fetch to update your local branch, and then do your merge:

git merge branch-name

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!

(Originally posted by Ray Malik)
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Cash App users have a week left to claim part of a...
This RTX 4050 gaming laptop is only $499 exclusive...
 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Thursday, 14 November 2024

Captcha Image

I Got A Virus and I Don't Know What To Do!

I Need Help!