博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
git 切换本地git账号_如何在本地Git存储库中的问题之间切换
阅读量:2521 次
发布时间:2019-05-11

本文共 8085 字,大约阅读时间需要 26 分钟。

git 切换本地git账号

On my journey to open source I ran into a simple (yet tricky) situation that can trip you up if you do it wrong. And that's what we'll discuss in this article.

在开源过程中,我遇到了一个简单(但很棘手)的情况,如果您做错了,可能会使您绊倒。 这就是我们将在本文中讨论的内容。

Participating in the open source community means you're contributing to the development of free or open source software. There are many organizations that are always welcoming contributors to their codebases.

参与开源社区意味着您为自由或开源软件的开发做出了贡献。 有许多组织一直欢迎其代码库的参与者。

To get started with open source, you need to have a basic understanding of version control tools such as . Contributors use Git to track changes in project files and it also helps people coordinate their work on those files.

要开始使用开源软件,您需要对版本控制工具(例如有基本的了解。 贡献者使用Git跟踪项目文件中的更改,它还可以帮助人们协调对这些文件的工作。

先决条件 (Prerequisites)

  1. Have installed

    安装了

  2. Have a basic understanding of Git

    对Git有基本的了解

为什么会出问题? (Why issues? )

Issues in a repository can be used to keep track of tasks, ideas, bugs, or enhancements for the project you are working on. Basically, they provide you with a description of what the task entails.

存储库中的问题可用于跟踪您正在处理的项目的任务,想法,错误或增强功能。 基本上,它们为您提供了任务要求的描述。

To take up an issue the project administrators need to assign that particular issue to you. This way other team members will know that someone is working on the issue.

要解决问题,项目管理员需要将该特定问题分配给您。 这样,其他团队成员将知道有人正在处理此问题。

处理问题 (Working on an issue )

To start working on an issue, you need to make a clone or copy of the target repository using the Git clone command on your local machine.

要开始解决问题,您需要使用本地计算机上的Git clone命令制作目标存储库的克隆或副本。

git clone 

Create an upstream allowing you to keep track of latest upstream (i.e original repository) changes. This way Git keeps you informed when there are changes so you can update the cloned repository.

创建一个上游,使您可以跟踪最新的上游(即原始存储库)更改。 这样,当有更改时,Git会通知您,以便您可以更新克隆的存储库。

git remote add upstream 

To preview the list of available remote and tasks that can be performed (fetch and push) type:

要预览可用遥控器和可以执行(获取和推送)任务的列表,请输入:

git remote -v

To stay updated with the latest changes, you always need to try to fetch from the upstream. This way you get all the commits from every team member who also worked on various features.

为了保持最新更改,您始终需要尝试从上游获取。 这样,您就可以从每个团队成员那里获得所有提交,这些团队成员还从事各种功能。

git fetch 
/

Moving forward, you'll need to merge the commits of other contributors to the local repository.

展望未来,您需要将其他贡献者的提交合并到本地存储库中。

git merge 
/

The goal of Git merge is to make the local copy of the master branch match exactly the same as the upstream copy of the master branch.

Git合并的目标是使master分支的本地副本与master分支的上游副本完全匹配。

Next, create a branch for the issue you were assigned. Why do you have to create a branch? And what is a branch used for? Let's investigate further.

接下来,为您分配的问题创建一个分支。 为什么必须创建一个分支? 分支的作用是什么? 让我们进一步调查。

Git分支 (Git branch )

A branch gives you a snapshot of the changes that have been made. When a commit is made, Git stores the information from the commit. This provides a pointer that can later be used to reference or track the changes that were made. This is why it's helpful to create a branch when working on new task, bug fix, or any other feature.

分支为您提供了所做更改的快照。 提交后,Git会存储来自提交的信息。 这提供了一个指针,以后可以用来引用或跟踪所做的更改。 这就是为什么在处理新任务,错误修复或任何其他功能时创建分支会有所帮助的原因。

When we get started, Git provides us with a master branch. The master branch contains working code. To avoid mixing your changes with production code, you need to create a new branch.

当我们开始时,Git为我们提供了一个master分支。 master分支包含工作代码。 为了避免将更改与生产代码混淆,您需要创建一个新分支。

To create a branch you need to enter the following Git command:

要创建分支,您需要输入以下Git命令:

git checkout -b 

This command creates a new branch based on the current branch, although you can also specify the branch where you want your new branch created.

尽管您还可以指定要在其中创建新分支的分支,但该命令将基于当前分支创建一个新分支。

git checkout -b 

To list all available branches in your repository, type:

要列出存储库中所有可用的分支,请输入:

git branch

When the task you're working on has been completed, push changes on the local repository for review. After that, create a pull request to notify project administrators on the current state of the assigned task.

完成您要处理的任务后,将更改推送到本地存储库中以供查看。 之后,创建一个拉取请求,以将所分配任务的当前状态通知项目管理员。

git push -u origin 

现在如何切换到下一个问题? (Now how do I switch to work on the next issue?  )

Create a different branch with a descriptive name, like this:

使用描述性名称创建另一个分支,如下所示:

git checkout -b 

Once we have our branch we'll use a utility command from . The command will help us fetch code from upstream, and it'll also run the merge (if you install the.

有了分支后,我们将使用来自的实用程序命令。 该命令将帮助我们从上游获取代码,并且还将运行合并(如果安装了 。

hub sync

The command retrieves the upstream changes and merges them with the newly created branch. You can always check for changes with your branch and upstream using the Git status command:

该命令检索上游更改并将其与新创建的分支合并。 您始终可以使用Git status命令检查分支和上游的更改:

git status

Now you can proceed and work on the new branch. Just remember to commit your changes and push to the remote branch as we did above.

现在,您可以继续进行新分支的工作了。 只要记住要像上面所做的那样提交您的更改并推送到远程分支即可。

您可能会犯的错误。 (Mistakes you might make.)

You might make a mistake while working on multiple issues - which can lead to to deleting commits from a branch.

处理多个问题时,您可能会犯一个错误-这可能导致删除分支中的提交。

Here's a sample walk-through of what can be done to erase unwanted commits from a branch:

以下是如何清除分支中不需要的提交的示例演练:

Step 1: Switch into the branch where you would like to remove unwanted commits:

步骤1:切换到您要删除不需要的提交的分支:

git checkout 

Step 2: Run the records of commits made to the branch. This will help you decide which commits you would like to retain based on the unique Commit Hash (SHA1 40 character checksum of the commits contents) usually in this form: da034f6ff3e856b5ba155bc01def0847a1c4ed7e.

步骤2:运行对分支所做的提交记录。 这将帮助您根据唯一的提交哈希 (提交内容的SHA1 40字符校验和)来决定要保留的提交,通常采用以下形式: da034f6ff3e856b5ba155bc01def0847a1c4ed7e

git log

It's also worth noting that if you are looking to retain the most recent commit (say the last on) you can simply do this:

还值得注意的是,如果您希望保留最近的提交(例如最后一次提交),则可以执行以下操作:

git log -n 1

Step 3: Since you want to discard all the other commits on that branch, simply apply that one single commit to the branch. Discard and apply are two steps:

步骤3:由于您要舍弃该分支上的所有其他提交,因此只需将一个提交应用于分支即可。 丢弃并应用是两个步骤:

First, discard all commits on the branch with:

首先,使用以下命令丢弃分支上的所有提交:

git reset --hard 
/

In simple terms, the above command tells Git to throw away all staged and un-staged changes. It'll forget everything on the current local branch and make it exactly the same as the upstream/master.

简单来说,上面的命令告诉Git放弃所有已分阶段和未分阶段的更改。 它将忘记当前本地分支上的所有内容,并使它与upstream/master完全相同。

Second, apply that one single commit to the branch with the command:

其次,使用以下命令将一次提交应用于分支:

git cherry-pick Hash//where Hash is a commit hash from other branch

This command picks a single reference (i.e commit) by default from a branch and applies it to another.

默认情况下,此命令从分支中选择一个引用(即提交)并将其应用于另一个。

Step 4: When you run git status it will report that your branch <origin>/<descriptive-branch-name> diverged. Since that's expected we need to force the remote to only contain those changes we have cherry-picked.

步骤4:当您运行git status ,它将报告您的分支<origin>/<descriptive-branch-name> 。 由于这是预料之中的,我们需要强制遥控器仅包含我们精心挑选的那些更改。

To get this done, we need to use a command to help erase the remote history and replace it with a different history:

为此,我们需要使用命令来帮助清除远程历史记录并将其替换为其他历史记录:

git push --force origin

This command will discard the extra commits on the remote, just as we discarded the extra commits on the local copy. This is dangerous because it is one of the very few git commands that will discard something - so be careful when using it.

该命令将丢弃远程主机上的多余提交,就像我们丢弃本地副本上的多余提交一样。 这很危险,因为它是极少数会丢弃某些内容的git命令之一-使用时请务必小心。

Now when you run git status it reports that the branch is up to date with <origin>/<descriptive-branch-name>. This shows you that the operation was carried out successfully.

现在,当您运行git status它将报告该分支是最新的<origin>/<descriptive-branch-name> 。 这表明操作已成功执行。

Thank you for reading 🎉! Big shout out to 👏  

谢谢您阅读🎉! 向大喊

Follow me on .

在上关注我。

翻译自:

git 切换本地git账号

转载地址:http://qvzzd.baihongyu.com/

你可能感兴趣的文章
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
学习进度
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>
一款jQuery打造的具有多功能切换的幻灯片特效
查看>>