search
HomeDevelopment ToolsgitTake you step by step through the most commonly used git commands in 10 minutes

Essentially, Git can record changes to text, but its definition is a version control system. Chances are you've already used git in one way or another: due to its distributed nature, it's the de facto standard for code version control, as opposed to the centralized Apache Subversion (SVN).

Install git

To check if Git is installed, run in the terminal:

$ git version
git version 2.27.0.rc1.windows.1

If it is not installed, follow https://git- Instructions at scm.com/downloads. Mac users can install it using brew: brew install git.

Configure git

We only need to configure a few things

git config --global user.name "前端小智" && # 你的名字
git config --global user.email johndoe@example.com && # 你的邮箱
git config --global init.defaultbranch main # 默认分支名称,与GitHub兼容

You can use the following command to view the current global configuration

git config --global --list
# Type ":q" to close

git Store the configuration in plain text. If you want to modify it directly, you can edit the global configuration directly in ~/.gitconfig or ~/.config/git/config.

As the command suggests, removing --global will expand the scope of these commands to the current folder. But to test this we need a repository.

Create New Repository

A repository is just a folder that holds everything we want to track. Created by command:

mkdir gitexample && 
cd gitexample && 
git init
# gitexample git:(main)

This command creates a .git folder within the gitexample folder. This hidden .git folder is the repository: all local configurations and modifications are stored here.

Changes

Create something in the repository:

echo "Hello, Git " >> hello.txt

Run git status and we will see the newly created untracked files .

git status
# On branch main
# 
# No commits yet
# 
# Untracked files:
#  (use "git add <file>..." to include in what will be committed)
#   hello.txt
#
# nothing added to commit but untracked files present (use "git add" to track)

According to the prompt suggestions, we add files:

git add .

If we don’t want all files to be added, we can use

git add hello.txt

If you check the status of the repository now, you will I see that the file has been added (also called staged), but it has not been submitted yet.

git status
# On branch main
# 
# No commits yet
# 
# Changes to be committed:
#  (use "git rm --cached <file>..." to unstage)
#   new file:   hello.txt

To record these changes, let's commit it.

git commit -m "Add hello.txt"
# [main (root-commit) a07ee27] Adds hello.txt
# 1 file changed, 2 insertions(+)
# create mode 100644 hello.txt

git commit -m is a short command. You can use git commit to open an editor (mainly vim) and provide a detailed commit description.

Check commit records:

git log
# Author: qq449245884 <44924566884@qq.com>
# Date:   Sat Jul 17 14:57:24 2021 +0800
#
#    Add hello.txt
#

Create a branch

In many cases it is useful to have an independent version of the initial code: for example, Avoid code conflicts when testing functionality you're not sure about, or when working together. That's exactly what a git branch is: it grows from a specific point in history.

To create a branch, run git branch NAME. To switch branches, run git checkout NAME. Or simply

git checkout -b dev # 切换到一个名为“dev”的新分支
# Switched to a new branch &#39;dev&#39;
# gitexample git:(dev)

We change something in the Hello.txt file and commit the changes:

echo "\nHello, Git Branch" >> hello.txt &&
git commit -am "Change hello.txt"

Now, switch to the master branch:

git checkout main &&
cat hello.txt
# Switched to branch &#39;main&#39;
# Hello, Git

As you can see , the file content remains the same as before. To compare branches we can run.

git diff dev
# diff --git a/hello.txt b/hello.txt
# index 360c923..b7aec52 100644
# --- a/hello.txt
# +++ b/hello.txt
# @@ -1,3 +1 @@
# Hello, Git
# -
# -Hello, Git Branch
# (END)
# type ":q" to close

We make changes in the master branch:

echo "\nHi from Main Branch" >> hello.txt &&
git commit -am "Change hello.txt from main"
# [main 9b60c4b] Change hello.txt from main
# 1 file changed, 2 insertions(+)

Now let’s try to merge these changes.

git merge dev
# Auto-merging hello.txt
# CONFLICT (content): Merge conflict in hello.txt
# Automatic merge failed; fix conflicts and then commit the result.

Because the file was modified twice in the same place, we had a conflict. Take a look at this file

cat hello.txt
<<<<<<< HEAD
Hello, Git
Hi from Main Branch
=======
Hello, Git
>>>>>>> dev

There is also a command to view the changes individually:

git diff --ours # :q to close 
git diff --theirs #:q to close

You can manually edit the file and commit the changes, but let's imagine that we only want one version of it. Let's start by aborting the merge.

git merge --abort

And restart the merge with the "theirs" strategy, which means in the event of a conflict, we will use what the incoming branch is holding on to.

git merge -X theirs dev
# Auto-merging hello.txt
# Merge made by the &#39;recursive&#39; strategy.
# hello.txt | 5 +----
# 1 file changed, 1 insertion(+), 4 deletions(-)

The opposite of this strategy is "ours". Merging these two changes together requires manual editing (or using git mergetool).

View a list of all branch runs

git branch # type :q to close
#  dev
# * main

Finally, to delete the branch run:

git branch -d dev
# Deleted branch dev (was 6259828).

Reset branch

Branch from git A certain point in history begins to "grow", and rebase allows this point to be changed. Let's create another branch and add some changes to hello.txt.

git checkout -b story &&
echo "Once upon a time there was a file">>story.txt &&
git add story.txt &&
git commit -m "Add story.txt"
# Switched to a new branch &#39;story&#39;
# [story eb996b8] Add story.txt
# 1 file changed, 1 insertion(+)
# create mode 100644 story.txt

Now, we go back to the main branch and add the changes:

git checkout main &&
echo "Other changes" >> changes.txt &&
git add changes.txt &&
git commit -m "Add changes.txt"

Reset the changes we made in the main to story branch:

git checkout story &&
git rebase main
# Successfully rebased and updated refs/heads/story.

You can see in the main New files created by the branch are added to the story branch.

ls
# changes.txt hello.txt   story.txt

Note: Do not rebase branches that others may have used, such as the main branch. Also, remember that every historical operation on the remote repository needs to force these modifications to take effect.

Remote Repository

If you don't have one yet, create a GitHub account, log in and create a new empty repository (private or public).

Assuming the name of the repository is "example", run the following command (change it to your username).

git remote add origin git@github.com:USERNAME/example.git &&
git push -u origin main

You can refresh the page and see the files in the main branch. To push all local branches to the remote repository, run.

git push --all origin

We edit something on GitHub: just click on any file and the pencil icon. Add a line of any text you want and press "Submit Changes."

Run this command locally to get remote changes. [Recommended: Git Tutorial]

git checkout main &&
git pull

Manage uncommitted changes

If you want to save your local modifications for later use, you can Use git stash.

echo "Changes" >> hello.txt &&
git stash

Now you can use the following commands to check, apply or discard these changes.

git stash list
# stash@{0}: WIP on main: 92354c8 Update changes.txt
git stash pop # 应用更改
git stash drop # 撤销修改

你可以使用 stash 编号,即git stash pop 0来应用一个特定的储藏库,或者git stash drop 0来撤销。

如果你想放弃所有的本地修改,只需恢复版本库到最后提交的修改,请运行。

git restore .

管理提交的更改

一旦你创建了一个提交,这个变化就会保存在本地的git历史中。如前所述,所有影响远程历史的修改都需要git push --force。以下所有命令都要记住这一点。

我们从编辑最后的提交信息开始。

git commit --amend # type :wq to save and close
# Press "i" to edit, "Esc" to stop editing

我们把一切重设到最开始怎么样?

要找到第一次提交的ID,请运行这个命令并滚动(向下箭头)到最后。

git log --abbrev-commit
# commit a07ee27
# Author: Your Name <your@email.address>
Date:   Sun Jul 11 11:47:16 2021 +0200
    Adds hello.txt
(END)
# type ":q" to close

现在运行这个来重置版本库,但保持所有的修改不被缓存。

git reset --soft COMMIT # e.g. a07ee27

与之相反,你也可以进行硬重置,用git reset --hard COMMIT来删除所有修改。还有几种其他的重置方式,你可以从git文档中了解到。

别名

大多数时候,你只需要使用少数几个命令(主要是checkout、add、commit、pull、push和merge),但有些命令可能是你想要“以防万一”的。

存储这些信息的一种方法是git aliases。要配置一个别名,只需在配置中设置它。例如,我经常使用的一个别名是git tree,它以树的形式打印出一个漂亮的历史日志。

git config --global alias.tree &#39;log --graph --decorate --pretty=oneline --abbrev-commit&#39;
# Try it with `git tree`

另一个有用的别名是删除所有合并的分支。

git config --global alias.clbr &#39;!git branch --merged | grep -v \* | xargs git branch -D&#39;

你可以看到它的前缀是"!",这允许我们使用任何命令,而不仅仅是git命令。

~完,我是刷碗智,今天礼拜六写的,要准备去刷碗了,骨的白!

▎作者:Valeria 译者:前端小智 来源:dev 原文:https://dev.to/valeriavg/master-git-in-7-minutes-gai

The above is the detailed content of Take you step by step through the most commonly used git commands in 10 minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:前端小智. If there is any infringement, please contact admin@php.cn delete
Git vs. GitHub: Exploring Their RolesGit vs. GitHub: Exploring Their RolesApr 16, 2025 am 12:06 AM

Git and GitHub are different tools: Git is a distributed version control system for managing code versions and collaborative development; GitHub is an online platform based on Git, providing code hosting and collaboration tools. Git's main features include version management, branch management, and collaborative development, while GitHub provides code hosting, collaboration tools and social networking capabilities.

GitHub: A Hub for Open Source and Software DevelopmentGitHub: A Hub for Open Source and Software DevelopmentApr 15, 2025 am 12:10 AM

GitHub is a Git-based version control system hosting platform that provides version control, collaborative development and community communication functions. Using GitHub can improve development efficiency and code quality.

Git and GitHub: What's the Relationship?Git and GitHub: What's the Relationship?Apr 14, 2025 am 12:10 AM

Git and GitHub are different tools: Git is software for version control, and GitHub is an online platform based on Git. 1.Git allows you to track file changes and collaborative development. 2. GitHub provides code hosting and collaboration tools to enhance team development efficiency.

GitHub: The Platform for Developers and ProjectsGitHub: The Platform for Developers and ProjectsApr 13, 2025 am 12:01 AM

The core features of GitHub include version control, branch management, code review, issue tracking and project management. 1. Version control and branch management are based on Git, allowing tracking of code changes and experimental development. 2. Code review is implemented through PullRequest to improve code quality and team collaboration. 3. Issues tracking and project management are carried out through Issues and the project management board to improve project transparency and traceability.

GitHub in Action: Examples and Use CasesGitHub in Action: Examples and Use CasesApr 12, 2025 am 12:16 AM

GitHub is a powerful tool to improve the efficiency and quality of software development. 1) Version control: manage code changes through Git. 2) PullRequests: Conduct code review and improve code quality. 3) Issues: Track bugs and project progress. 4) GitHubActions: Automate the construction, testing and deployment process.

Git vs. GitHub: Version Control and Code HostingGit vs. GitHub: Version Control and Code HostingApr 11, 2025 am 11:33 AM

Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions and supports local operations; GitHub provides online collaboration tools such as Issue tracking and PullRequest.

What is Git in simple words?What is Git in simple words?Apr 09, 2025 am 12:12 AM

Git is an open source distributed version control system that helps developers track file changes, work together and manage code versions. Its core functions include: 1) record code modifications, 2) fallback to previous versions, 3) collaborative development, and 4) create and manage branches for parallel development.

Is Git the same as GitHub?Is Git the same as GitHub?Apr 08, 2025 am 12:13 AM

Git and GitHub are not the same thing. Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions, and GitHub provides an online collaboration environment.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor