五个提升你的 Git 水平的命令
Git

> 
> 将这些命令加入到你的工作流中，使 Git 发挥更大的作用。
> 
> 
> 


![](/data/attachment/album/202203/17/110616vti9hhsiiq4misra.jpg "Woman using laptop concentrating")


如果你经常使用 Git，你可能会知道它非常有名。它可能是最受欢迎的版本控制方案，它被一些 [最大的软件项目](https://opensource.com/article/19/10/how-gnome-uses-git) 用来 [跟踪文件变更](https://opensource.com/article/18/2/how-clone-modify-add-delete-git-files)。Git 提供了 [健壮的界面](https://opensource.com/article/18/5/git-branching) 来审阅代码、把实验性的变更合并到已经存在的文件中。得益于 [Git 钩子](https://opensource.com/life/16/8/how-construct-your-own-git-server-part-6)，它以灵活性而闻名。同时，也因为它的强大，它给人们留下了一个“复杂”的印象。


Git 有诸多特性，你不必全部使用，但是如果你正在深入研究 Git 的 <ruby> 子命令 <rt>  subcommands </rt></ruby>，我这里倒是有几个，或许你会觉得有用。


### 1、找到变更


如果你已经熟悉 Git 的基本指令（`fetch`、`add`、`commit`、`push`、`log` 等等），但是希望学习更多，那么从 Git 的检索子命令开始是一个简单安全的选择。检索你的 Git 仓库（你的 *工作树*）并不会做出任何更改，它只是一个报告机制。你不会像使用 `git checkout` 一样承担数据完整性的风险，你只是在向 Git 请求仓库的当前状态和历史记录而已。


[git whatchanged](https://opensource.com/article/21/3/git-whatchanged) 命令（几乎本身就是一个助记符）可以查看哪些文件在某个<ruby> 提交 <rt>  commit </rt></ruby>中有变更、分别做了什么变更。它是一个简单的、用户友好的命令，因为它把 `show`、`diff-tree` 和 `log` 这三个命令的最佳功能整合到了一个好记的命令中。


### 2、使用 git stash 管理变更


你越多地使用 Git，你就会使用 Git 越多。这就是说，一旦你习惯了 Git 的强大功能，你就会更频繁地使用它。有时，你正在处理一大堆文件，忽然意识到了有更紧急的任务要做。这时，在 [git stash](https://opensource.com/article/21/3/git-stash) 的帮助下，你就可以把所有正在进行的工作收集起来，然后安全地<ruby> 暂存 <rt>  stash </rt></ruby>它们。当你的工作空间变得整洁有序，你就可以把注意力放到别的任务上，晚些时候再把暂存的文件重新加载到工作树里，继续之前的工作。


### 3、使用 git worktree 来得到链接的副本


当 `git stash` 不够用的时候，Git 还提供了强大的 [git worktree](https://opensource.com/article/21/3/git-worktree) 命令。有了它，你可以新建一个 *链接的* 仓库<ruby> 副本 <rt>  clone </rt></ruby>，组成一个新分支，把 `HEAD` 设置到任意一个提交上，然后基于这个分支开始你的新工作。在这个链接的副本里，你可以进行和主副本完全不同的任务。这是一个避免意外的变更影响当前工作的好办法。当你完成了你的新工作，你可以把新分支推送到远程仓库；也可以把当前的变更归档，晚些时候再处理；还可以从别的工作树中获取它们的变更。无论选择哪一种，你的工作空间之间都会保持相互隔离，任一空间中的变更都不会影响其他空间中的变更，直到你准备好了要合并它们。


### 4、使用 git cherry-pick 来选择合并


这可能听起来很反直觉，但是，你的 Git 水平越高，你可能遇到的合并冲突就会越多。这是因为合并冲突不一定是错误的标志，而是活跃的标志。在学习 Git 中，适应合并时的冲突，并学会如何解决它们是非常重要的。通常的方式或许够用，但是有时候你会需要更加灵活地进行合并，这时候就该 [git cherry-pick](https://opensource.com/article/21/3/reasons-use-cherry-picking) 出场了。遴选操作允许你选择部分合并提交，这样一来你就不需要因为一些细微的不协调而拒绝整个合并请求了。


### 5、使用 Git 来管理 $HOME


使用 Git 来管理你的主目录从来没有这么简单过，这都要归功于 Git 可以自由选择管理对象的能力，这是一个在多台计算机之间保持同步的现实可行的选项。但是，想要让它工作顺利，你必须非常明智且谨慎才行。如果你想要了解更多，点击阅读我写的关于 [使用 Git 来管理 $HOME](https://opensource.com/article/21/3/git-your-home) 的小技巧。


### 更好地使用 Git


Git 是一个强大的版本控制系统，你使用得越熟练，就可以越轻松地借助它来完成复杂的任务。今天就尝试一些新的 Git 命令吧，欢迎在评论区分享你最喜欢的 Git 命令。




---


via: <https://opensource.com/article/21/4/git-commands>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[lkxed](https://github.com/lkxed) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
If you use Git regularly, you might be aware that it has several reputations. It's probably the most popular version-control solution and is used by some of the [biggest software projects](https://opensource.com/article/19/10/how-gnome-uses-git) around to [keep track of changes](https://opensource.com/article/18/2/how-clone-modify-add-delete-git-files) to files. It provides a [robust interface](https://opensource.com/article/18/5/git-branching) to review and incorporate experimental changes into existing documents. It's well-known for its flexibility, thanks to [Git hooks](https://opensource.com/life/16/8/how-construct-your-own-git-server-part-6). And partly because of its great power, it has earned its reputation for being complex.

You don't have to use all of Git's many features, but if you're looking to delve deeper into Git's subcommands, here are some that you might find useful.

## 1. Finding out what changed

If you're familiar with Git's basics (`fetch`

, `add`

, `commit`

, `push`

, `log`

, and so on) but you want to learn more, Git subcommands that query are a great, safe place to start. Querying your Git repository (your *work tree*) doesn't make any changes; it's only a reporting mechanism. You're not risking the integrity of your Git checkout; you're only asking Git about its status and history.

The [git whatchanged](https://opensource.com/article/21/3/git-whatchanged) command (almost a mnemonic itself) is an easy way to see what changed in a commit. A remarkably user-friendly command, it squashes the best features of `show`

and `diff-tree`

and `log`

into one easy-to-remember command.

## 2. Managing changes with git stash

The more you use Git, the more you use Git. That is, once you've become comfortable with the power of Git, the more often you use its powerful features. Sometimes, you may find yourself in the middle of working with a batch of files when you realize some other task is more urgent. With [git stash](https://opensource.com/article/21/3/git-stash), you can gather up all the pieces of your work in progress and stash them away for safekeeping. With your workspace decluttered, you can turn your attention to some other task and then reapply stashed files to your work tree later to resume work.

## 3. Making a linked copy with git worktree


When `git stash`

isn't enough, Git also provides the powerful [git worktree](https://opensource.com/article/21/3/git-worktree) command. With it, you can create a new but *linked* clone of your repository, forming a new branch and setting `HEAD`

to whatever commit you want to base your new work on. In this linked clone, you can work on a task unrelated to what your primary clone is focused on. It's a good way to keep your work in progress safe from unintended changes. When you're finished with your new work tree, you can push your new branch to a remote, bundle the changes into an archive for later, or just fetch the changes from your other tree. Whatever you decide, your workspaces are kept separate, and the changes in one don't have to affect changes in the other until you are ready to merge.

## 4. Selecting merges with git cherry-pick

It may seem counterintuitive, but the better at Git you get, the more merge conflicts you're likely to encounter. That's because merge conflicts aren't necessarily signs of errors but signs of activity. Getting comfortable with merge conflicts and how to resolve them is an important step in learning Git. The usual methods work well, but sometimes you need greater flexibility in how you merge, and for that, there's [git cherry-pick](https://opensource.com/article/21/3/reasons-use-cherry-picking). Cherry-picking merges allows you to be selective in what parts of commits you merge, so you never have to reject a merge request based on a trivial incongruity.

## 5. Managing $HOME with Git

Managing your home directory with Git has never been easier, and thanks to Git's ability to be selective in what it manages, it's a realistic option for keeping your computers in sync. To work well, though, you must do it judiciously. To get started, read my tips on [managing $HOME with Git](https://opensource.com/article/21/3/git-your-home).

## Getting better at Git

Git is a powerful version-control system, and the more comfortable you become with it, the easier it becomes to use it for complex tasks. Try some new Git commands today, and share your favorites in the comments.

## Comments are closed.