Home > Development Tools > git > body text

Take you step by step through the most commonly used git commands in 10 minutes

藏色散人
Release: 2021-08-27 14:04:15
forward
2724 people have browsed it

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
Copy after login

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兼容
Copy after login

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

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

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)
Copy after login

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
Copy after login

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)
Copy after login

According to the prompt suggestions, we add files:

git add .
Copy after login

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

git add hello.txt
Copy after login

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
Copy after login

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
Copy after login

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
#
Copy after login

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)
Copy after login

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

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

Now, switch to the master branch:

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

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
Copy after login

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(+)
Copy after login

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.
Copy after login

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
Copy after login

There is also a command to view the changes individually:

git diff --ours # :q to close 
git diff --theirs #:q to close
Copy after login

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
Copy after login

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(-)
Copy after login

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
Copy after login

Finally, to delete the branch run:

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

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
Copy after login

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"
Copy after login

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

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

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 # 撤销修改
Copy after login

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

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

git restore .
Copy after login

管理提交的更改

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

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

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

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

要找到第一次提交的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
Copy after login

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

git reset --soft COMMIT # e.g. a07ee27
Copy after login

与之相反,你也可以进行硬重置,用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`
Copy after login

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

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

你可以看到它的前缀是"!",这允许我们使用任何命令,而不仅仅是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!

Related labels:
source:前端小智
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!