Translated 20210408 5 commands to level-up your Git game.md

This post is translated by lkxed.
This commit is contained in:
lkxed 2022-03-16 17:02:21 +08:00
parent 7a72c1e0f7
commit 703951621a
2 changed files with 67 additions and 67 deletions

View File

@ -1,67 +0,0 @@
[#]: subject: (5 commands to level-up your Git game)
[#]: via: (https://opensource.com/article/21/4/git-commands)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
[#]: collector: (lujun9972)
[#]: translator: (lkxed)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
5 commands to level-up your Git game
======
Get more use out of Git by adding these commands to your repertoire.
![Business woman on laptop sitting in front of window][1]
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][2] around to [keep track of changes][3] to files. It provides a [robust interface][4] to review and incorporate experimental changes into existing documents. It's well-known for its flexibility, thanks to [Git hooks][5]. 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][6] 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][7], 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][8] 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][9]. 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][10].
### 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.
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/4/git-commands
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-concentration-focus-windows-office.png?itok=-8E2ihcF (Woman using laptop concentrating)
[2]: https://opensource.com/article/19/10/how-gnome-uses-git
[3]: https://opensource.com/article/18/2/how-clone-modify-add-delete-git-files
[4]: https://opensource.com/article/18/5/git-branching
[5]: https://opensource.com/life/16/8/how-construct-your-own-git-server-part-6
[6]: https://opensource.com/article/21/3/git-whatchanged
[7]: https://opensource.com/article/21/3/git-stash
[8]: https://opensource.com/article/21/3/git-worktree
[9]: https://opensource.com/article/21/3/reasons-use-cherry-picking
[10]: https://opensource.com/article/21/3/git-your-home

View File

@ -0,0 +1,67 @@
[#]: subject: (5 commands to level-up your Git game)
[#]: via: (https://opensource.com/article/21/4/git-commands)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
[#]: collector: (lujun9972)
[#]: translator: (lkxed)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
5 个命令来升级你的 Git 操作
======
将这些命令加入到你的工作流中,使 Git 发挥更大的作用。
![Business woman on laptop sitting in front of window][1]
如果你经常使用 Git你可能会知道它非常有名。它可能是最受欢迎的版本控制方案被一些 [最大的软件项目][2] 用来 [跟踪文件变更][3]。Git 提供了 [鲁棒的接口][4] 来审阅代码、把实验性的变更合并到已经存在的文档中。得益于 [Git hooks][5],它以灵活性而闻名。同时,也因为它的强大,它给人们留下了一个「复杂」的印象。
Git 有诸多特性,你不必全部使用,但是如果你正在深入研究 Git 的 <ruby>子命令<rt>subcommands</rt></ruby>,我这里倒是有几个,或许你会觉得有用。
### 1\. 找到变更
如果你已经熟悉 Git 的基本指令(`fetch`、`add`、`commit`、`push`、`log` 等等),但是希望学习更多,那么从 Git 的检索子命令开始是一个简单安全的选择。检索你的 Git 仓库(你的 _工作树_)并不会做出任何更改,它只是一个报告机制。你不会像使用 git checkout 一样承担数据完整性的风险,你只是在向 Git 请求仓库的当前状态和历史记录而已。
[git whatchanged][6] 命令(几乎本身就是一个助记符)可以查看哪些文件在某个 commit 中有变更、分别做了什么变更。它是一个简单的、用户友好的命令,因为它把 `show`、`diff-tree` 和 `log` 这三个命令的最佳功能整合到了一个好记的命令中。
### 2\. 使用 git stash 管理变更
你越多地使用 Git你就会使用 Git 越多。这就是说,一旦你习惯了 Git 的强大,你就会更频繁地使用它。有时,你正在处理一大堆文件,忽然意识到了有更紧急的任务要做。这时,在 [git stash][7] 的帮助下,你就可以把所有正在进行的工作收集起来,然后安全地暂存它们。当你的工作空间变得整洁有序,你就可以把注意力放到别的任务上,晚些时候再把暂存的文件重新加载到工作树里,继续之前的工作。
### 3\. 使用 git worktree 来得到链接的副本
`git stash` 不够用的时候Git 还提供了强大的 [git worktree][8] 命令。有了它,你可以新建一个 _链接的_ 仓库副本,组成一个新分支,把 `HEAD` 设置到任意一个 commit 上,然后基于这个分支开始你的新工作。在这个链接的副本里,你可以进行和主副本完全不同的任务。这是一个避免意外的变更影响当前工作的好办法。当你完成了你的新工作,你可以把新分支推送到远程仓库;也可以把当前的变更归档,晚些时候再处理;还可以从别的工作树中获取它们的变更。无论选择哪一种,你的工作空间之间都会保持相互隔离,任一空间中的变更都不会影响其他空间中的变更,直到你准备好了要合并它们。
### 4\. 使用 cherry-pick 来选择合并
这可能听起来很反直觉,但是,你的 Git 水平越高,你可能遇到的合并冲突就会越多。这是因为合并冲突并不是错误的标志,而是活跃的标志。在学习 Git 中,适应合并时的冲突,并学会如何解决它们是非常重要的。通常的方式或许够用,但是有时候你会需要更加灵活地进行合并,这时候就该 [git cherry-pick][9] 出场了。Cherry-pick 允许你选择部分合并 commits这样一来你就不需要因为一些细微的不协调而拒绝整个合并请求了。
### 5\. 使用 Git 来管理 $HOME
使用 Git 来管理你的主目录从来没有这么简单过,这都要归功于 Git 可以自由选择管理对象的能力,这是一个在多台计算机之间保持同步的现实可行的选项。但是,想要让它工作顺利,你必须非常明智且谨慎才行。如果你想要了解更多,点击阅读我写的关于 [使用 Git 来管理 $HOME][10] 的小贴士。
### 更好地使用 Git
Git 是一个强大的版本控制系统,你使用得越熟练,就可以越轻松地借助它来完成复杂的任务。今天就尝试一些新的 Git 命令吧,欢迎在评论区分享你最喜欢的 Git 命令。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/4/git-commands
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[lkxed](https://github.com/lkxed)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-concentration-focus-windows-office.png?itok=-8E2ihcF (Woman using laptop concentrating)
[2]: https://opensource.com/article/19/10/how-gnome-uses-git
[3]: https://opensource.com/article/18/2/how-clone-modify-add-delete-git-files
[4]: https://opensource.com/article/18/5/git-branching
[5]: https://opensource.com/life/16/8/how-construct-your-own-git-server-part-6
[6]: https://opensource.com/article/21/3/git-whatchanged
[7]: https://opensource.com/article/21/3/git-stash
[8]: https://opensource.com/article/21/3/git-worktree
[9]: https://opensource.com/article/21/3/reasons-use-cherry-picking
[10]: https://opensource.com/article/21/3/git-your-home