Movatterモバイル変換


[0]ホーム

URL:


How-To Geek logo

10 Basic Git Commands to Get You Started

A terminal with the Git logo and some code in the background.Credit: Lucas Gouveia / How-To Geek
4
By Ray Malik
Ray discovered her passion for technology and writing at a young age. This combination led her to pursue a career in cybersecurity. She earned a bachelor's degree in this field, building on her lifelong interest in web technologies. After graduation, Ray began her career as a support engineer for DNS security startups, where her blend of technical skills and creativity proved valuable. Later, she transitioned into the exciting areas of threat intelligence and dark web research. Ray is also skilled at writing about technical topics, creating both technical documentation and content for product marketing. This mix of abilities makes her a versatile and valuable team member in various roles. Outside of work, Ray enjoys coding in Python to keep her skills sharp and loves hiking and experimenting with new recipes in her free time.
Sign in to yourHow-To Geek account
Jump links

Jump Links

follow
Follow
followed
Followed
Thread2
Here is a fact-based summary of the story contents:
Try something different:

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 aboutinstalling Git on Windows.

1 To 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 onGitHub orGitLab.

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, 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 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

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 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 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 -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 toremove 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 rungit 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 commandgit log. Copy the commit ID and run thegit log command with the commit ID.

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

Do not confusegit revert withgit 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.

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). Ifmaster doesn't work, trymain.

It’s recommended to rungit 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, ifmaster doesn't work, trymain. Since this command combines the functions ofgit fetch andgit 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.

10 Merge it All Together

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

You can do this by running the git mergecommand. You should first rungit 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!

Follow
Followed
Share
FacebookXWhatsAppThreadsBlueskyLinkedInRedditFlipboardCopy linkEmail
Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission.Read More.
Stylized illustration featuring the Python logo surrounded by bar charts, a pie chart, and line graphs.
How stats made programming click for me
Safing Portmaster logo surrounded by red 'blocked' icons, shield symbols with checkmarks, and floating apps with internet access being blocked.
How I block specific apps from internet access with this free, open-source tool
A man sitting comfortably on an armchair using a laptop, with the Linux mascot Tux standing beside him and floating icons representing media, gaming, and music.
Yes, Linux can do 'real work'—here are 5 things I do on my Linux PC
See More
The back of the OnePlus 15 sitting in grass and leaves.
The OnePlus 15 can finally be sold in the U.S.
A replacement battery for a Kindle third generation eReader.
It’s time to admit you can swap out internal rechargeable batteries yourself
Several smartphones arranged diagonally on a blue geometric background, each displaying a simple home screen with a solid black wallpaper
Black is the new best wallpaper for your phone
See More

[8]ページ先頭

©2009-2025 Movatter.jp