7 分钟掌握 Git

2025-05-28

7 分钟掌握 Git

本质上,Git 会监控文本的更改,但它的定义是一个版本控制系统。你很可能已经以某种方式使用过 Git:由于其分布式特性,它是代码版本控制的事实标准,这与集中式的 Apache Subversion(SVN)截然不同。

安装 Git

要检查您是否安装了 Git,请在终端中运行:

git version
# git version 2.30.1 (Apple Git-130)
Enter fullscreen mode Exit fullscreen mode

如果你没有,请按照https://git-scm.com/downloads上的说明进行操作。Mac 用户可以使用 brew 安装:brew install git

配置 Git

我们只需要配置以下几项:

git config --global user.name "John Doe" && # your name
git config --global user.email johndoe@example.com && # your email
git config --global init.defaultbranch main # default branch name, to be compatible with GitHub
Enter fullscreen mode Exit fullscreen mode

您可以通过以下方式查看当前的全局配置:

git config --global --list
# Type ":q" to close
Enter fullscreen mode Exit fullscreen mode

Git 以纯文本形式存储配置,如果您愿意,您可以直接在~/.gitconfig或 中编辑全局配置~/.config/git/config

正如命令所示,删除后--global这些命令的作用域将限定在当前文件夹。但为了测试这一点,我们需要一个存储库。

创建新的存储库

存储库只是一个文件夹,里面存放着你想要追踪的所有内容。要创建一个存储库,请运行以下命令:

mkdir gitexample && 
cd gitexample && 
git init
# gitexample git:(main)
Enter fullscreen mode Exit fullscreen mode

.git此命令在文件夹中创建一个文件夹gitexample。这个隐藏.git文件夹构成了存储库:所有本地配置和更改都存储在那里。

做出改变

让我们在存储库中创建一些东西:

echo "Hello, Git" >> hello.txt
Enter fullscreen mode Exit fullscreen mode

如果我们运行git status,我们将看到新创建的未跟踪文件:

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)
Enter fullscreen mode Exit fullscreen mode

根据输出提示,让我们添加文件。可以直接使用以下命令完成:

git add . # Or `git add hello.txt`, if we don't want all files
Enter fullscreen mode Exit fullscreen mode

如果您现在检查存储库状态,您将看到文件已添加(又称为暂存),但尚未提交:

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

为了记录更改,让我们提交它们:

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

专业提示:git commit -m <MESSAGE>是一个简写命令,您可以用它git commit来打开编辑器(主要是 vim)并提供详细的提交描述。

让我们检查一下变化:

git log
# type :q to close
Enter fullscreen mode Exit fullscreen mode

它将显示类似以下内容:

commit a07ee270d6bd0419a50d1936ad89b9de0332f375 (HEAD -> main)
Author: Your Name <your@email.address>
Date:   Sun Jul 11 11:47:16 2021 +0200

    Adds hello.txt
(END)
Enter fullscreen mode Exit fullscreen mode

创建分支

在很多情况下,拥有初始代码的单独版本都很有用:例如,测试某个你不确定的功能,或者避免协作时出现代码冲突。这正是 git 分支的本质:它从历史的某个特定点开始发展。

创建分支运行git branch NAME并切换分支运行git checkout NAME。或者简单地:

git checkout -b dev # switches to a new branch called "dev"
# Switched to a new branch 'dev'
# gitexample git:(dev)
Enter fullscreen mode Exit fullscreen mode

让我们对hello.txt文件进行一些修改并提交修改:

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

现在让我们切换回主版本:

git checkout main &&
cat hello.txt
# Switched to branch 'main'
# Hello, Git
Enter fullscreen mode Exit fullscreen mode

如你所见,文件内容仍然与之前相同。要比较分支,我们可以运行:

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
Enter fullscreen mode Exit fullscreen mode

我们也对主分支进行一些修改:

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(+)
Enter fullscreen mode Exit fullscreen mode

现在让我们尝试合并这些变化:

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

由于文件在同一个地方被修改了两次,因此发生了冲突。查看文件:

cat hello.txt
<<<<<<< HEAD
Hello, Git

Hi from Main Branch
=======
Hello, Git
>>>>>>> dev
Enter fullscreen mode Exit fullscreen mode

还有一个工具可以单独查看更改:

git diff --ours # :q to close 
git diff --theirs #:q to close
Enter fullscreen mode Exit fullscreen mode

您可以手动编辑文件并提交更改,但假设我们只需要其中一个版本。我们将从中止合并开始:

git merge --abort
Enter fullscreen mode Exit fullscreen mode

并重新开始与“他们的”策略合并,这意味着如果发生冲突,我们将使用传入分支坚持的任何内容:

git merge -X theirs dev
# Auto-merging hello.txt
# Merge made by the 'recursive' strategy.
# hello.txt | 5 +----
# 1 file changed, 1 insertion(+), 4 deletions(-)
Enter fullscreen mode Exit fullscreen mode

与此策略相反的是“我们的”。将两个更改合并在一起需要手动编辑(或使用git mergetool)。

要查看所有分支的列表,请运行:

git branch # type :q to close
#  dev
# * main
Enter fullscreen mode Exit fullscreen mode

最后,要删除分支运行:

git branch -d dev
# Deleted branch dev (was 6259828).
Enter fullscreen mode Exit fullscreen mode

重新定基分支

分支从 git 历史记录中的特定点“增长”,rebase允许更改该点。让我们创建另一个分支,并再次对 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 'story'
# [story eb996b8] Add story.txt
# 1 file changed, 1 insertion(+)
# create mode 100644 story.txt
Enter fullscreen mode Exit fullscreen mode

现在,让我们回到主分支并在那里添加更改:

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

main要重播我们在分支运行中所做的更改story

git checkout story &&
git rebase main
# Successfully rebased and updated refs/heads/story.
Enter fullscreen mode Exit fullscreen mode

您可以看到在main分支中创建的新文件被添加到story分支中:

ls
# changes.txt hello.txt   story.txt
Enter fullscreen mode Exit fullscreen mode

需要注意的是:不要对其他人可能使用过的分支(例如主分支)进行 rebase。另外,请记住,远程仓库上的任何历史记录操作都需要强制这些更改生效。

远程存储库

如果您还没有,请创建一个GitHub帐户,登录并创建一个新的空存储库(私有或公共)。

假设存储库名称为“example”,运行以下命令(更改为您的用户名):

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

您可以刷新页面并查看主分支中的文件。要将所有本地分支推送到远程存储库,请运行:

git push --all origin
Enter fullscreen mode Exit fullscreen mode

让我们在 GitHub 上编辑一些内容:只需点击任意文件和铅笔图标。添加一行你想要的文本,然后点击“提交更改”。

现在在本地运行此命令以获取远程更改:

git checkout main &&
git pull
Enter fullscreen mode Exit fullscreen mode

管理未提交的变更

如果您想要保存本地更改以供以后使用,可以使用git stash

echo "Changes" >> hello.txt &&
git stash
Enter fullscreen mode Exit fullscreen mode

现在您可以使用以下命令来检查、应用或放弃这些更改:

git stash list
# stash@{0}: WIP on main: 92354c8 Update changes.txt
git stash pop # to apply changes
git stash drop # to drop changes
Enter fullscreen mode Exit fullscreen mode

专业提示:您可以使用存储编号,即git stash pop 0应用特定的存储或git stash drop 0删除它。

如果您想要放弃所有本地更改并简单地将存储库恢复到上次提交的更改,请运行:

git restore .
Enter fullscreen mode Exit fullscreen mode

管理已提交的变更

一旦创建提交,此更改将保存在本地 git 历史记录中。如前所述,所有影响远程历史记录的更改都需要一个git push --force。请记住,在执行所有后续命令时都要记住这一点。

让我们从编辑最后的提交消息开始:

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

我们把一切都重置到最开始怎么样?
要找到第一个提交的 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
Enter fullscreen mode Exit fullscreen mode

现在运行此命令来重置存储库,但保持所有更改未暂存:

git reset --soft COMMIT # e.g. a07ee27
Enter fullscreen mode Exit fullscreen mode

与之相反,你也可以使用 进行硬重置,删除所有更改。你可以从git 文档git reset --hard COMMIT中了解其他几种重置类型。

别名

大多数时候,您只会使用少量命令(主要是检出、添加、提交、拉取、推送和合并),但您可能希望有一些命令以防万一。

存储这些内容的一种方法是使用 git 别名。要配置别名,只需在配置中设置即可。例如,我经常使用的一个别名是git tree,它以树状结构打印出漂亮的历史记录日志:

git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit'
# Try it with `git tree`
Enter fullscreen mode Exit fullscreen mode

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

git config --global alias.clbr '!git branch --merged | grep -v \* | xargs git branch -D' 
Enter fullscreen mode Exit fullscreen mode

正如您所看到的,它的前缀是“!”,这允许我们使用任何命令,而不仅仅是 git 命令。

今天就到这里,希望对你的开发者之旅有所帮助。和往常一样,欢迎在评论区分享你的想法和反馈。下次再见!

文章来源:https://dev.to/valeriavg/master-git-in-7-minutes-gai
PREV
我和 React:15 分钟回顾 5 年
NEXT
TypeScript 初学者教程:缺失指南 - 第一部分