10分钟一步步带你看完最常用的git命令

 3661

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


安装 git

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

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

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


配置 git

我们只需要配置一些东西

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

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

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

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

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


创建新存储库

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

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

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


改变

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

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

运行git status,我们会看到新创建的未被追踪的文件。

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

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

  1. git add .

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

  1. git add hello.txt

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

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

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

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

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

检查提交记录:

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


创建分支

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

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

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

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

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

现在,切换到主分支:

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

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

  1. git diff dev
  2. # diff --git a/hello.txt b/hello.txt
  3. # index 360c923..b7aec52 100644
  4. # --- a/hello.txt
  5. # +++ b/hello.txt
  6. # @@ -1,3 +1 @@
  7. # Hello, Git
  8. # -
  9. # -Hello, Git Branch
  10. # (END)
  11. # type ":q" to close

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

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

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

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

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

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

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

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

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

  1. git merge --abort

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

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

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

查看所有分支运行的列表

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

最后,删除分支运行:

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


重置分支

分支从 git 历史中的某一点开始 "生长(grow)",rebase 允许改变这个点。我们再创建一个分支,并在hello.txt上添加一些改动。

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

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

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

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

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

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

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

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


远程存储库

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

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

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

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

  1. git push --all origin

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

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

  1. git checkout main &&
  2. git pull


管理未提交的更改

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

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

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

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

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

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

  1. git restore .


管理提交的更改

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

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

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

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

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

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

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

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

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


别名

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

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

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

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

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

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

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


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



TAG标签:
本文网址:https://www.zztuku.com/index.php/detail-9093.html
站长图库 - 10分钟一步步带你看完最常用的git命令
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐