尝试使用git switch、git restore、git grep和git blame

 

我每天像呼吸一样使用git,但我意识到我从未阅读过git的文档,所以我决定实际去读一下。我发现一些很有用但不太常用的git命令,所以我想分享一下。

    • git switch

 

    • git restore

 

    • git grep

 

    • git blame

 

    • git log -p

 

    git commit –amend –no-edit

git 切换

这是一个切换分支的命令。虽然可以使用git checkout来实现,但看到这篇文章后我明白为什么要使用git switch了。

 

基本上,git checkout是一个可以让你在工作目录中切换到指定分支或撤销修改的命令。

git checkout ブランチ・コミットを切り替える

git checkout — ファイルの変更を廃棄する(直近のcommitに戻す)

由于有两个功能,所以有点令人困惑。我记得几年前学习Git的时候,我曾经陷得很深。
Git 2.23版本发布了一个只具有切换分支和提交的功能——Git Switch。

# git checkout your_branchと同じ
git switch your_branch

然而,在创建新分支时选项不是-b,而是-c。

# git checkout -b your_branchと同じ
git switch -c new_branch

回退版本

git restore具备git checkout剩余一半的功能,可以废弃文件的更改并回到最近的提交。

# git checkout -- file.txtと同じ
git restore -- file.txt

我认为使用git switch和git restore代替git checkout会更容易理解,特别是对于初学者,请务必尝试一下。

git grep – 通过git搜索

我借用keras的存储库来进行解释。
https://github.com/keras-team/keras

这是一个用于搜索包含特定关键字的文件的命令。它的行为与VSCode的搜索功能(Cmd/Ctrl + Shift + F)几乎相同,但可以从终端执行,我个人认为使用起来更方便。

git grep "bash"
.devcontainer/Dockerfile:    && apt-get install -y sudo bash \
.devcontainer/Dockerfile:CMD ["/bin/bash"]
.devcontainer/devcontainer.json:        "terminal.integrated.defaultProfile.linux": "bash"
ISSUE_TEMPLATE.md:```bash
keras/distribute/sidecar_evaluator.py:    ```bash
keras/kokoro/github/ubuntu/cpu/build.sh:#!/bin/bash
keras/kokoro/github/ubuntu/gpu/build.sh:#!/bin/bash
keras/tools/gpu_build/parallel_gpu_execute.sh:#!/usr/bin/env bash
keras/tools/pip_package/build_pip_package.sh:#!/usr/bin/env bash
shell/format.sh:#!/bin/bash
shell/lint.sh:#!/bin/bash

git 光荣地承担 <文件名>

这个命令可以查看每行的更改历史记录。您可以一次确认提交ID、提交者和提交时间。

git blame README.md
e1a17d32f (Francois Chollet      2021-06-15 13:29:07 -0700   1) # Keras: Deep Learning for humans
37a1db225 (Francois              2015-03-27 17:59:42 -0700   2) 
5b3cf890c (fchollet              2017-11-05 13:00:03 -0800   3) ![Keras logo](https://s3.amazonaws.com/keras.io/img/keras-logo-2018-large-1200.png)
059caec89 (Francois Chollet      2017-09-18 12:39:52 -0700   4) 
e1a17d32f (Francois Chollet      2021-06-15 13:29:07 -0700   5) This repository hosts the development of the Keras library.
e1a17d32f (Francois Chollet      2021-06-15 13:29:07 -0700   6) Read the documentation at [keras.io](https://keras.io/).
78691340e (Gabriel de Marmiesse  2019-09-15 07:40:35 +0200   7) 
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700   8) ## About Keras
e1a17d32f (Francois Chollet      2021-06-15 13:29:07 -0700   9) 
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  10) Keras is a deep learning API written in Python,
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  11) running on top of the machine learning platform [TensorFlow](https://github.com/tensorflow/tensorflow).
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  12) It was developed with a focus on enabling fast experimentation.
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  13) *Being able to go from idea to result as fast as possible is key to doing good research.*
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  14) 
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  15) Keras is:
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  16) 
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  17) -   **Simple** -- but not simplistic. Keras reduces developer *cognitive load*
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  18)     to free you to focus on the parts of the problem that really matter.
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  19) -   **Flexible** -- Keras adopts the principle of *progressive disclosure of
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  20)     complexity*: simple workflows should be quick and easy, while arbitrarily
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  21)     advanced workflows should be *possible* via a clear path that builds upon
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  22)     what you've already learned.
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  23) -   **Powerful** -- Keras provides industry-strength performance and
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  24)     scalability: it is used by organizations and companies including NASA,
b6e8b9921 (TensorFlower Gardener 2022-03-10 14:27:15 -0800  25)     YouTube, and Waymo.
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  26) 
88373a906 (Francois Chollet      2021-06-18 10:12:46 -0700  27) ---

查看 <文件名> 的 git 提交历史,并显示每次提交的差异。

这是一个用于查看文件修改记录的命令。

git log -p README.md

不仅可以确认提交的历史记录,还可以同时查看差异。

Author: Ikko Ashimine <eltociear@gmail.com>
Date:   Mon May 23 18:28:11 2022 +0900

    Fix typos

diff --git a/README.md b/README.md
index 37675e0a..09d1b7ce 100644
--- a/README.md
+++ b/README.md
@@ -174,7 +174,7 @@ version maps to a specific stable version of TensorFlow.
 The table below shows the compatibility version mapping
 between TensorFlow versions and Keras versions.
 
-All the release branches can be found on [Github](https://github.com/keras-team/keras/releases).
+All the release branches can be found on [GitHub](https://github.com/keras-team/keras/releases).
 
 All the release binaries can be found on [Pypi](https://pypi.org/project/keras/#history).
 

commit 5b6df0f0e67463ce9109c3f9799a26356f8cc3e5
Merge: 1601e591 4a7e71b7
Author: TensorFlower Gardener <gardener@tensorflow.org>
Date:   Tue Mar 22 09:20:44 2022 -0700

    Merge pull request #16259 from ahmedopolis:patch-1
    
    PiperOrigin-RevId: 436492033

commit 4a7e71b7502f455e2ad6b4c8e3cf26de75728a7c
Author: François Chollet <francois.chollet@gmail.com>
Date:   Mon Mar 21 13:41:49 2022 -0700

    Update README.md

提交 git commit 并修改最新的 commit 记录,不编辑

如果已经提交了,但想要增加一些轻微的修正,再创建一个提交(考虑提交信息)就有点麻烦了吧。

git add file.txt
git commit --amend --no-edit

使用该功能,之前进行的小型修正会被整合到最近的提交中,非常方便。

如果你还没有使用过的话,强烈建议尝试一下。

广告
将在 10 秒后关闭
bannerAds