0% found this document useful (0 votes)
11 views

10 Basic Git Commands to Get You Started

The document provides a guide on 10 basic Git commands essential for beginners, including cloning repositories, creating new ones, managing branches, checking status, committing changes, and merging. It explains each command's purpose and usage, emphasizing the importance of version control in software development. The article aims to help new users effectively navigate Git and manage their projects.

Uploaded by

gecko6337
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

10 Basic Git Commands to Get You Started

The document provides a guide on 10 basic Git commands essential for beginners, including cloning repositories, creating new ones, managing branches, checking status, committing changes, and merging. It explains each command's purpose and usage, emphasizing the importance of version control in software development. The article aims to help new users effectively navigate Git and manage their projects.

Uploaded by

gecko6337
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

10 Basic Git

Commands to Get You


Started
By Ray Malik – 18 hours ago

Lucas Gouveia / How-To Geek

Programming

 Thread 2  Follow  Share

Ad

 QUICK LINKS
 To Clone an Existing Repo

 To Create a New Repo

 Creating a Branch for Collaboration

 Switch Between Branches

 Check Git Status

 Commit Sets of Changes

 Rolling Back Changes

 Upload All Your Local Changes

 Retrieve All Changes

 Merge it All Together

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.

Ad
If you're new to Git, let's start with a
refresher. A Git repository (or repo for
short) contains all the project Iles 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 conLicts (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 Irst 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:

Ad

Next, run the command below with the


URL you just copied:

git clone https://name-of-the-repository-link


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

Note

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.

Ad

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
speciIc
 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

3
Creating a Branch for
Link copiedCollaboration
to clipboardAd

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:

Ad

git branch name-of-branch

To list all your branches, run:

git branch

To delete a branch:

git branch -d branch-name

Tip

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 Iles 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 Irst.

Tip

Shortcut command to create and


switch a branch at the same time:
git checkout -b name-of-branch

Ad

5
Check 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 Iles that
were modiIed or deleted.
git status

This is what the output should look like if


there are no changes to be made:

6
Commit Sets of Changes
This may be the most used Git command.
When we are ready to save our work,
maybe after a speciIc task or issue, we
can use Git commit. This essentially
captures a snapshot of the project’s
currently staged changes.

Ad
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"

Warning

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 Ile 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 reLect 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.

Ad
Warning

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.

8
Upload All Your Local
Changes
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.
Ad

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.

Tip

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
modiIcations to your local repository (git
merge) after retrieving updates from the
remote repository (git fetch). You can
learn more about pull requests on Git.

Ad

10
Merge it All Together
Finally, once you’ve completed working on
your branch and everything is working
correctly, the Inal 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 Irst run git fetch to
update your local branch, and then do your
merge:

git merge branch-name

Note

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!

Ad
Programming GitHub

 Follow    

Readers like you help support How-To Geek. When you make a purchase
using links on our site, we may earn an ailiate commission. Read
More.

 THREAD 2

We want to hear from you! Share your opinions in the


thread below and remember to keep it respectful.

 Reply / Post

Sort by: Popular

Jacob

2024-11-12 02:10:32

Git add?

Richard

2024-11-12 05:34:06
2024-11-12 05:34:06

Good point.

Terms | Privacy | Feedback

 

 1
Sign In / Log In Now
to Start or Join a Discussion


Ad

 RECOMMENDED

Cybersecurity

9 Windows Features to Disable for


Better Security
Helpful yet risky settings to keep enabled!
3 days ago

Streaming

Your Smart TV Might Have a


Camera—Here's What You Can Do…
With but
Useful It concerning.

3 days ago

Amazon Alexa

Alexa Plus is the Upgrade We’re All


Waiting For
Amazon's Alexa Plus leak reveals game-changing AI
features—these are the ones I'm excited about.

5 days ago
 21
Social Media

Bluesky vs. X (Twitter): Just a Clone


or Something More?
The two microblogging apps have some key
differences.

20 hours ago
1

Google Play Store

Here's How I Find Great Android


Games on the Play Store
There are so many excellent games out there, but
good luck Inding them.

19 hours ago

Carriers

Boost Mobile Is a Real Carrier Now


Graduated from MVNO.

22 hours ago

Ad

Join Our Team


Our Audience
About Us
Press & Events
Contact Us
Follow Us

     

Advertising
Careers
Terms
Privacy
Policies

How-To Geek is part of the Valnet Publishing Group

Copyright © 2024 Valnet Inc.

You might also like