git使用指南_如何使用Git:参考指南

git使用指南

Git备忘单 (Git Cheat Sheet)

介绍 (Introduction)

Teams of developers and open-source software maintainers typically manage their projects through Git, a distributed version control system that supports collaboration.

开发人员和开源软件维护人员的团队通常通过Git(一个支持协作的分布式版本控制系统)来管理他们的项目。

This cheat sheet-style guide provides a quick reference to commands that are useful for working and collaborating in a Git repository. To install and configure Git, be sure to read “How To Contribute to Open Source: Getting Started with Git.”

该备忘单样式指南提供了对命令的快速参考,这些命令对于在Git存储库中进行工作和协作非常有用。 要安装和配置Git,请务必阅读“ 如何为开源做贡献:Git入门 ”。

How to Use This Guide:

如何使用本指南:

  • This guide is in cheat sheet format with self-contained command-line snippets.

    本指南采用备忘单格式,包含独立的命令行摘要。
  • Jump to any section that is relevant to the task you are trying to complete.

    跳至与您要完成的任务有关的任何部分。
  • When you see highlighted text in this guide’s commands, keep in mind that this text should refer to the commits and files in your own repository.

    当您在本指南的命令中看到highlighted text时,请记住,该文本应引用您自己的存储库中的提交和文件。

设置和初始化 (Set Up and Initialization)

Check your Git version with the following command, which will also confirm that Git is installed.

使用以下命令检查您的Git版本,该命令还将确认已安装Git。

  • git --version

    git --version

You can initialize your current working directory as a Git repository with init.

您可以使用init将当前工作目录初始化为Git存储库。

  • git init

    git init

To copy an existing Git repository hosted remotely, you’ll use git clone with the repo’s URL or server location (in the latter case you will use ssh).

要复制远程托管的现有Git存储库,将使用git clone和仓库的URL或服务器位置(在后一种情况下,您将使用ssh )。

  • git clone https://www.github.com/username/repo-name

    git clone https://www.github.com/username/repo-name

Show your current Git directory’s remote repository.

显示您当前的Git目录的远程存储库。

  • git remote

    git远程

For a more verbose output, use the -v flag.

要获得更详细的输出,请使用-v标志。

  • git remote -v

    git remote -v

Add the Git upstream, which can be a URL or can be hosted on a server (in the latter case, connect with ssh).

在上游添加Git,它可以是URL或可以托管在服务器上(在后一种情况下,请使用ssh连接)。

  • git remote add upstream https://www.github.com/username/repo-name

    git remote添加上游https://www.github.com/username/repo-name

分期 (Staging)

When you’ve modified a file and have marked it to go in your next commit, it is considered to be a staged file.

修改文件并将其标记为下次提交时,该文件将被视为暂存文件。

Check the status of your Git repository, including files added that are not staged, and files that are staged.

检查您的Git存储库的状态,包括未暂存的已添加文件和已暂存的文件。

  • git status

    git状态

To stage modified files, use the add command, which you can run multiple times before a commit. If you make subsequent changes that you want included in the next commit, you must run add again.

要暂存修改的文件,请使用add命令,您可以在提交之前多次运行该命令。 如果您要在下一次提交中进行后续更改,则必须再次运行add

You can specify the specific file with add.

您可以使用add指定特定文件。

  • git add my_script.py

    git添加my_script.py

With . you can add all files in the current directory including files that begin with a ..

. 您可以添加当前目录中的所有文件,包括以开头的文件.

  • git add .

    git添加

You can remove a file from staging while retaining changes within your working directory with reset.

您可以从暂存中删除文件,同时使用reset保留工作目录中的更改。

  • git reset my_script.py

    git reset my_script.py

提交中 (Committing)

Once you have staged your updates, you are ready to commit them, which will record changes you have made to the repository.

暂存更新后,就可以提交更新了,这将记录您对存储库所做的更改。

To commit staged files, you’ll run the commit command with your meaningful commit message so that you can track commits.

要提交暂存的文件,您将运行带有有意义的提交消息的commit命令,以便您可以跟踪提交。

  • git commit -m "Commit message"

    git commit -m“提交消息”

You can condense staging all tracked files with committing them in one step.

您可以通过一步提交来压缩登台所有跟踪的文件。

  • git commit -am "Commit message"

    git commit -am“提交消息”

If you need to modify your commit message, you can do so with the --amend flag.

如果您需要修改提交消息,则可以使用--amend标志进行修改。

  • git commit --amend -m "New commit message"

    git commit --amend -m“新提交消息”

分行 (Branches)

A branch in Git is a movable pointer to one of the commits in the repository, it allows you to isolate work and manage feature development and integrations. You can learn more about branches by reading the Git documentation.

Git中的分支是指向存储库中提交之一的可移动指针,它使您能够隔离工作并管理功能开发和集成。 您可以通过阅读Git文档来了解有关分支的更多信息。

List all current branches with the branch command. An asterisk (*) will appear next to your currently active branch.

使用branch命令列出所有当前分支。 当前活动分支旁边将出现一个星号( * )。

  • git branch

    git分支

Create a new branch. You will remain on your currently active branch until you switch to the new one.

创建一个新分支。 在切换到新分支之前,您将保留在当前活动的分支上。

  • git branch new-branch

    git分支新分支

Switch to any existing branch and check it out into your current working directory.

切换到任何现有分支,然后将其检出到当前工作目录中。

  • git checkout another-branch

    git checkout 另一个分支

You can consolidate the creation and checkout of a new branch by using the -b flag.

您可以使用-b标志来合并新分支的创建和签出。

  • git checkout -b new-branch

    git checkout -b 新分支

Rename your branch name.

重命名您的分支名称。

  • git branch -m current-branch-name new-branch-name

    git branch -m 当前分支名称 new-branch-name

Merge the specified branch’s history into the one you’re currently working in.

将指定分支的历史合并到当前正在使用的分支中。

  • git merge branch-name

    git merge 分支名称

Abort the merge, in case there are conflicts.

如果存在冲突,则中止合并。

  • git merge --abort

    git merge --abort

You can also select a particular commit to merge with cherry-pick with the string that references the specific commit.

您还可以选择一个特定的提交,使其与cherry-pick合并,并带有引用该特定提交的字符串。

  • git cherry-pick f7649d0

    吉特樱桃挑选f7649d0

When you have merged a branch and no longer need the branch, you can delete it.

合并分支后,不再需要该分支时,可以将其删除。

  • git branch -d branch-name

    git branch -d 分支名称

If you have not merged a branch to main, but are sure you want to delete it, you can force delete a branch.

如果尚未将分支合并到main,但是确定要删除它,则可以强制删除分支。

  • git branch -D branch-name

    git branch -D 分支名称

协作与更新 (Collaborate and Update)

To download changes from another repository, such as the remote upstream, you’ll use fetch.

要从另一个存储库(例如远程上游)下载更改,您将使用fetch

  • git fetch upstream

    git 上游获取

Merge the fetched commits. Note that some repositories may use master instead of main.

合并获取的提交。 请注意,某些存储库可能使用master而不是main

  • git merge upstream/main

    git merge上游/主

Push or transmit your local branch commits to the remote repository branch.

将本地分支提交推送或传输到远程存储库分支。

  • git push origin main

    git push origin主

Fetch and merge any commits from the tracking remote branch.

从跟踪远程分支中获取并合并所有提交。

  • git pull

    git pull

检查中 (Inspecting)

Display the commit history for the currently active branch.

显示当前活动分支的提交历史记录。

  • git log

    git日志

Show the commits that changed a particular file. This follows the file regardless of file renaming.

显示更改了特定文件的提交。 无论文件重命名如何,都将跟随文件。

  • git log --follow my_script.py

    git log-关注my_script.py

Show the commits that are on one branch and not on the other. This will show commits on a-branch that are not on b-branch.

显示位于一个分支而不是另一个分支上的提交。 这将显示a-brancha-branch提交,而不是b-brancha-branch提交。

  • git log a-branch..b-branch

    git log a分支 .. b分支

Look at reference logs (reflog) to see when the tips of branches and other references were last updated within the repository.

查看参考日志( reflog ),以查看分支和其他参考的提示何时在存储库中最后更新。

  • git reflog

    git reflog

Show any object in Git via its commit string or hash in a more human-readable format.

通过Git的提交字符串或哈希以更易于理解的格式显示任何对象。

  • git show de754f5

    git show de754f5

显示变更 (Show Changes)

The git diff command shows changes between commits, branches, and more. You can read more fully about it through the Git documentation.

git diff命令显示提交,分支等之间的更改。 您可以通过Git文档更全面地了解它。

Compare modified files that are on the staging area.

比较登台区域上的已修改文件。

  • git diff --staged

    git diff --staged

Display the diff of what is in a-branch but is not in b-branch.

显示a-branch而不是b-branch的差异。

  • git diff a-branch..b-branch

    git diff a分支 .. b分支

Show the diff between two specific commits.

显示两个特定提交之间的差异。

  • git diff 61ce3e6..e221d9c

    git diff 61ce3e6 .. e221d9c

藏匿 (Stashing)

Sometimes you’ll find that you made changes to some code, but before you finish you have to begin working on something else. You’re not quite ready to commit the changes you have made so far, but you don’t want to lose your work. The git stash command will allow you to save your local modifications and revert back to the working directory that is in line with the most recent HEAD commit.

有时您会发现您对某些代码进行了更改,但是在完成之前,您必须开始进行其他工作。 您还没有准备好提交到目前为止所做的更改,但是您不想丢失您的工作。 git stash命令将允许您保存本地修改并恢复到与最新HEAD提交一致的工作目录。

Stash your current work.

隐藏您当前的工作。

  • git stash

    git存储

See what you currently have stashed.

查看您当前藏匿的东西。

  • git stash list

    git存储列表

Your stashes will be named stash@{0}, stash@{1}, and so on.

您的stash@{0}将被命名为stash@{0}stash@{1} ,依此类推。

Show information about a particular stash.

显示有关特定存储的信息。

  • git stash show stash@{0}

    git stash show stash @ { 0 }

To bring the files in a current stash out of the stash while still retaining the stash, use apply.

要将当前存储区中的文件带出存储区,同时仍保留存储区,请使用apply

  • git stash apply stash@{0}

    git stash apply stash @ { 0 }

If you want to bring files out of a stash, and no longer need the stash, use pop.

如果要将文件带出存储库,而不再需要存储库,请使用pop

  • git stash pop stash@{0}

    git stash pop stash @ { 0 }

If you no longer need the files saved in a particular stash, you can drop the stash.

如果不再需要保存在特定存储中的文件,则可以drop该存储。

  • git stash drop stash@{0}

    git stash drop stash @ { 0 }

If you have multiple stashes saved and no longer need to use any of them, you can use clear to remove them.

如果您保存了多个存储卡,并且不再需要使用其中任何一个,则可以使用clear删除它们。

  • git stash clear

    git隐藏

忽略文件 (Ignoring Files)

If you want to keep files in your local Git directory, but do not want to commit them to the project, you can add these files to your .gitignore file so that they do not cause conflicts.

如果要将文件保留在本地Git目录中,但又不想将其提交到项目中,则可以将这些文件添加到.gitignore文件中,以免引起冲突。

Use a text editor such as nano to add files to the .gitignore file.

使用文本编辑器(例如nano)将文件添加到.gitignore文件。

  • nano .gitignore

    纳米.gitignore

To see examples of .gitignore files, you can look at GitHub’s .gitignore template repo.

要查看.gitignore文件的示例,您可以查看GitHub的.gitignore模板repo

变基 (Rebasing)

A rebase allows us to move branches around by changing the commit that they are based on. With rebasing, you can squash or reword commits.

通过重新设置基础,我们可以通过更改分支所基于的提交来移动分支。 使用重定基,您可以压缩或重命名提交。

You can start a rebase by either calling the number of commits you have made that you want to rebase (5 in the case below).

您可以通过调用要进行基准的提交次数(在以下情况下为5 )来启动基准。

  • git rebase -i HEAD~5

    GIT中变基-i HEAD〜5

Alternatively, you can rebase based on a particular commit string or hash.

或者,您可以基于特定的提交字符串或哈希值进行基准调整。

  • git rebase -i 074a4e5

    git rebase -i 074a4e5

Once you have squashed or reworded commits, you can complete the rebase of your branch on top of the latest version of the project’s upstream code. Note that some repositories may use master instead of main.

压缩提交或重新措辞后,您可以在项目上游代码的最新版本之上完成分支的基础。 请注意,某些存储库可能使用master而不是main

  • git rebase upstream/main

    git rebase 上游/主要

To learn more about rebasing and updating, you can read How To Rebase and Update a Pull Request, which is also applicable to any type of commit.

要了解有关重新设置基础和更新的更多信息,可以阅读如何重新设置基础和更新拉取请求 ,这也适用于任何类型的提交。

还原和重置 (Reverting and Resetting)

You can revert back the changes that you made on a given commit by using revert. Your working tree will need to be clean in order for this to be achieved.

您可以使用revert对给定提交所做的更改。 您的工作树将需要清洁才能实现。

  • git revert 1fc6665

    git恢复1fc6665

Sometimes, including after a rebase, you need to reset your working tree. You can reset to a particular commit, and delete all changes, with the following command.

有时,包括在重新设置基准之后,您需要重置工作树。 您可以使用以下命令重置为特定的提交,并删除所有更改

  • git reset --hard 1fc6665

    git reset --hard 1fc6665

To force push your last known non-conflicting commit to the origin repository, you’ll need to use --force.

要强制将最后一次已知的非冲突提交推送到原始存储库,您需要使用--force

Warning: Force pushing to the main (sometimes master) branch is often frowned upon unless there is a really important reason for doing it. Use sparingly when working on your own repositories, and work to avoid this when you’re collaborating.

警告 :除非确实有重要原因,否则通常不赞成强行推到master分支(有时是master分支)。 在您自己的存储库上时,请谨慎使用,并在协作时避免这种情况。

  • git push --force origin main

    git push --force起源主

To remove local untracked files and subdirectories from the Git directory for a clean working branch, you can use git clean.

要从Git目录中删除本地未跟踪的文件和子目录以获得干净的工作分支,可以使用git clean

  • git clean -f -d

    git clean -f -d

If you need to modify your local repository so that it looks like the current upstream main branch (that is, there are too many conflicts), you can perform a hard reset.

如果您需要修改本地存储库,使其看起来像当前的上游主分支(即冲突太多),则可以执行硬重置。

Note: Performing this command will make your local repository look exactly like the upstream. Any commits you have made but that were not pulled into the upstream will be destroyed.

注意 :执行此命令将使您的本地存储库看起来完全像上游。 您所做但未拉到上游的所有提交都将被销毁

  • git reset --hard upstream/main

    git reset --hard上游/主要

结论 (Conclusion)

This guide covers some of the more common Git commands you may use when managing repositories and collaborating on software.

本指南介绍了在管理存储库和在软件上进行协作时可能会使用的一些更常见的Git命令。

You can learn more about open-source software and collaboration in our Introduction to Open Source tutorial series:

您可以在我们的《开源简介》教程系列中了解有关开源软件和协作的更多信息:

There are many more commands and variations that you may find useful as part of your work with Git. To learn more about all of your available options, you can run the following to receive useful information.

作为使用Git的一部分,您可能会发现更多有用的命令和变体。 要了解有关所有可用选项的更多信息,可以运行以下命令以接收有用的信息。

  • git --help

    git-帮助

You can also read more about Git and look at Git’s documentation from the official Git website.

您还可以从Git官方网站上阅读有关Git的更多信息,并查看Git的文档。

翻译自: https://www.digitalocean.com/community/cheatsheets/how-to-use-git-a-reference-guide

git使用指南

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值