深圳幻海软件技术有限公司 欢迎您!

七分钟,用事例带你看完工作中常用的 Git 命令

2023-02-27

从本质上讲,Git可以记录文本的变化,但其定义是一个版本控制系统。你有可能已经以这种或那种方式使用了git:由于它的分布式性质,它是代码版本控制的事实标准,与集中式的ApacheSubversion(SVN)相对。安装git要检查是否安装了Git,在终端运行:复制$ git ve

从本质上讲,Git可以记录文本的变化,但其定义是一个版本控制系统。你有可能已经以这种或那种方式使用了git:由于它的分布式性质,它是代码版本控制的事实标准,与集中式的Apache Subversion(SVN)相对。

安装 git

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

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

如果没有安装,请按照 https://git-scm.com/downloads 上的说明。Mac用户可以用brew来安装它:brew install git。

配置 git

我们只需要配置一些东西

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

可以用下面命令查看当前的全局配置

git config --global --list 
# Type ":q" to close 
  • 1.
  • 2.

git在纯文本中存储配置,如果你想直接修改,可以直接在~/.gitconfig或~/.config/git/config中编辑全局配置。

正如命令所建议的那样,去掉--global会使这些命令的适用范围扩大到当前文件夹。但要测试这一点,我们需要一个存储库。

创建新存储库

存储库只是一个文件夹,里面有我们想跟踪的所有东西。通过命令创建:

mkdir gitexample &&  
cd gitexample &&  
git init 
# gitexample git:(main) 
  • 1.
  • 2.
  • 3.
  • 4.

这个命令在gitexample文件夹内创建了一个.git文件夹。这个隐藏的.git文件夹就是版本库:所有的本地配置和修改都存储在这里。

改变

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

echo "Hello, Git " >> hello.txt 
  • 1.

运行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) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

根据提示建议,我们添加文件:

git addd .  
  • 1.

如果我们不想要所有文件添加可以使用。

git add hello.txt  
  • 1.

如果你现在检查版本库的状态,你会看到文件已经被添加了(又称staged),但还没有提交。

git status 
On branch main 
#  
No commits yet 
#  
# Changes to be committed
#  (use "git rm --cached <file>..." to unstage) 
#   new file:   hello.txt 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

为了记录这些变化,我们来提交它。

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

git commit -m 是一个简短的命令,你可以用git commit打开编辑器(主要是vim),提供详细的提交描述。

检查提交记录:

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

#    Add hello.txt 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

创建分支

在很多情况下,拥有一个独立的初始代码版本是很有用的:例如,在测试你不确定的功能时,或者在一起工作时避免代码冲突。这正是git分支的意义所在:它从历史上的一个特定点开始生长。

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

git checkout -b dev # 切换到一个名为“dev”的新分支 
# Switched to a new branch 'dev' 
# gitexample git:(dev) 
  • 1.
  • 2.
  • 3.

我们在Hello.txt文件中更改一些内容并提交更改:

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

现在,切换到主分支:

git checkout main && 
cat hello.txt 
# Switched to branch 'main' 
# Hello, Git 
  • 1.
  • 2.
  • 3.
  • 4.

正如你所看到的,文件内容仍然和原来一样。为了比较分支,我们可以运行。

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

我们在主分支中进行更改:

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(+) 
  • 1.
  • 2.
  • 3.
  • 4.

现在让我们试着把这些变化合并起来。

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

因为文件在同一个地方被修改了两次,我们就产生了冲突。看看这个文件:

cat hello.txt 
<<<<<<< HEAD 
Hello, Git 
 
Hi from Main Branch 
======= 
Hello, Git 
>>>>>>> dev 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

还有一个命令可以单独查看更改:

git diff --ours # :q to close  
git diff --theirs #:q to close 
  • 1.
  • 2.

你可以手动编辑文件并提交修改,但我们设想一下,我们只想要其中一个版本。我们就从中止合并开始。

git merge --abort 
  • 1.

并以 "theirs"策略重新启动合并,这意味着在发生冲突时,我们将使用传入的分支所坚持的东西。

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(-) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

与此策略相反的是 "ours"。将这两个改动合并在一起,需要手动编辑(或使用git mergetool)。

查看所有分支运行的列表

git branch # type :q to close 
#  dev 
# * main 
  • 1.
  • 2.
  • 3.

最后,删除分支运行:

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

重置分支

分支从 git 历史中的某一点开始 "生长(grow)",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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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

git checkout main && 
echo "Other changes" >> changes.txt && 
git add changes.txt && 
git commit -m "Add changes.txt" 
  • 1.
  • 2.
  • 3.
  • 4.

重置我们在main到story分支所做的更改:

git checkout story && 
git rebase main 
# Successfully rebased and updated refs/heads/story. 
  • 1.
  • 2.
  • 3.

可以看到在主分支创建的新文件被添加到story 分支。

ls 
# changes.txt hello.txt   story.txt 
  • 1.
  • 2.

注意:不要rebase 别人可能使用过的分支,例如主分支。另外,请记住,在远程版本库上进行的每一次历史操作都需要强制这些修改生效。

远程存储库

如果你还没有,请创建一个GitHub账户,登录并创建一个新的空仓库(私有或公共)。

假设版本库的名字是 "example",运行以下命令(改成你的用户名)。

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

你可以刷新页面,看到主分支的文件。要把所有本地分支推送到远程仓库,请运行。

git push --all origin 
  • 1.

我们在GitHub上编辑一些东西:只要点击任何文件和铅笔图标。添加一行你想要的任何文字,然后按 "提交修改"。

在本地运行这个命令,以获得远程的变化。

git checkout main && 
git pull 
  • 1.
  • 2.

管理未提交的更改

如果你想保存你的本地修改以便以后使用,你可以使用git stash。

echo "Changes" >> hello.txt && 
git stash 
  • 1.
  • 2.

现在你可以使用以下命令来检查、应用或放弃这些变化。

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

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

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

git restore . 
  • 1.

管理提交的更改

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

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

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

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

要找到第一次提交的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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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

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

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

别名

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

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

git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit' 
# Try it with `git tree` 
  • 1.
  • 2.

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

git config --global alias.clbr '!git branch --merged | grep -v \* | xargs git branch -D'  
  • 1.

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

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