10 Basic Git Commands to Get You Started
10 Basic Git Commands to Get You Started
Programming
Ad
QUICK LINKS
To Clone an Existing Repo
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.
Ad
Note
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.
git init
3
Creating a Branch for
Link copiedCollaboration
to clipboardAd
Ad
git branch
To delete a branch:
Tip
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.
Tip
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
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.
Warning
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.
Ad
Warning
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
Tip
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.
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).
Note
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
Reply / Post
Jacob
2024-11-12 02:10:32
Git add?
Richard
2024-11-12 05:34:06
2024-11-12 05:34:06
Good point.
1
Sign In / Log In Now
to Start or Join a Discussion
Ad
RECOMMENDED
Cybersecurity
Streaming
3 days ago
Amazon Alexa
5 days ago
21
Social Media
20 hours ago
1
19 hours ago
Carriers
22 hours ago
Ad
Advertising
Careers
Terms
Privacy
Policies