vue项目示例代码git_您应该了解的5个Git命令以及代码示例

vue项目示例代码git

I've used Git for some years now, and I still find myself googling how to do some basic tasks. So this article is my attempt to learn how to do some of these things by writing about them. And even if I still forget, at least I'll have a reference where I can easily find these commands – and you will, too.

我已经使用Git几年了,但我仍然发现自己在搜寻如何执行一些基本任务。 因此,本文是我尝试通过撰写文章来学习如何做这些事情的尝试。 即使我仍然忘记了,至少我会有一个参考,在这里我可以轻松找到这些命令,您也将找到。

Before we move on to learn these things, something a colleague of mine once said has stuck with me. He told me that everything is possible with Git and that nothing is lost in Git.

在我们继续学习这些东西之前,我的一位同事曾经说过一句话。 他告诉我,Git可以实现一切,而Git不会丢失任何东西。

I don't know if the former part of his statement is entirely true but I keep it in mind every time I try to do something with Git. I believe that I'm going to find a command that will help me do what I need to do. I just have to google with the right words. If you are new to Git, I want you to believe that, too.

我不知道他声明的前一部分是否完全正确,但是每次我尝试对Git进行操作时,我都会牢记这一点。 我相信我将找到一条命令,该命令将帮助我完成需要做的事情。 我只需要用正确的词来谷歌搜索。 如果您是Git的新手,我也希望您也相信这一点。

In this article, we'll learn how to do the following:

在本文中,我们将学习如何执行以下操作:

  1. Add remote repositories

    添加远程存储库
  2. Change remote repositories

    更改远程存储库
  3. Delete a branch

    删除分支
  4. Merge a file from one branch to another

    将文件从一个分支合并到另一个分支
  5. Undo a commit locally and remotely

    本地和远程撤消提交

Though this article is intended for people with a basic knowledge of Git, I'll do my best to explain terms as much as possible.

尽管本文是为具有Git基础知识的人准备的,但我会尽力解释尽可能多的术语。

1.添加远程存储库 (1. Add Remote Repositories)

Remote repositories are versions of your projects that are stored on the internet or elsewhere. Adding a remote repository is a way of telling Git where your code is stored.

远程存储库是存储在Internet或其他地方的项目的版本。 添加远程存储库是一种告诉Git代码存储在哪里的方法。

We can do this using the URL of the repository. This could be the URL of your repository, another user's fork, or even a completely different server.

我们可以使用存储库的URL进行此操作。 这可能是您的存储库的URL,另一个用户的fork甚至是完全不同的服务器。

When you clone a repository, Git implicitly adds that repository as the origin remote for you. To add a new Git repository, you use this command:

克隆存储库时,Git隐式将该存储库添加为您的origin远程站点。 要添加新的Git存储库,请使用以下命令:

git remote add <shortname> <url>

where shortname is a unique remote name and url is the url of the repository you want to add.

其中shortname是唯一的远程名称,而url是要添加的存储库的url。

For example, if I want to add a repository with the shortname upstream, I can do this:

例如,如果我想添加一个短名称为upstream的存储库,则可以这样做:

git remote add upstream https://github.com/sarahchima/personal-website.git

Remember that your shortname can be anything, it just has to be unique, that is different from what the names of the remote repositories you already have. It should also be something you can easily remember for your sanity.

请记住,您的shortname可以是任何东西,它必须唯一,这与您已有的远程存储库的名称不同。 您也应该为自己的理智而轻易记住它。

To view the list of remote URLs you have added, run the following command:

要查看已添加的远程URL列表,请运行以下命令:

git remote -v

You'll see a list of the remote names and the URLs you have added.

您将看到一个远程名称和添加的URL的列表。

But what if you want to change these remote URLs? Let's move to the next Git command.

但是,如果您想更改这些远程URL怎么办? 让我们转到下一个Git命令。

2.更改远程存储库 (2. Change remote repositories)

There are several reasons why you may want to change a remote URL. For example, I recently had to move from using https URLs to SSH URLs for a project I worked on.

您可能要更改远程URL的原因有多种。 例如,最近我不得不从使用https URL转到我从事的项目的SSH URL。

To do this, you use the following command:

为此,请使用以下命令:

git remote set-url <an-existing-remote-name> <url>

For this command to work, the remote name has to be an existing remote name. That means it won't work if you've not added that remote name before.

为了使此命令起作用,远程名称必须是现有的远程名称。 这意味着如果您之前未添加该远程名称,它将无法正常工作。

Using the example above, if I want to change the remote URL, I'll do this:

使用上面的示例,如果要更改远程URL,将执行以下操作:

git remote set-url upstream git@github.com:sarahchima/personal-website.git

Remember to run git remote -v to verify that your change worked.

请记住运行git remote -v来验证您的更改是否有效。

Enough about remote repositories. Let's move on to something different.

足够用于远程存储库。 让我们继续一些不同的事情。

3.在本地和远程删除分支 (3. Delete a branch both locally and remotely)

A branch is a version of the repository that is different from the main working project. You may want to read up on Git branches and how to add a branch if you are not familiar with that process.

分支是存储库的版本,与主要工作项目不同。 如果您不熟悉该过程,则可能需要阅读有关Git分支以及如何添加分支的信息。

如何删除本地分支 (How to delete a local branch)

To delete a branch locally, make sure you are not on the branch you want to delete. So you have to checkout to a different branch and use the following command:

要在本地删除分支,请确保您不在要删除的分支上。 因此,您必须签出到其他分支并使用以下命令:

git branch -d <name-of-branch>

So if I want to delete a branch named fix/homepage-changes, I'll do the following:

因此,如果要删除一个名为fix/homepage-changes的分支,将执行以下操作:

git branch -d fix/homepage-changes

You can run git branch on your terminal to confirm that the branch has been successfully removed.

您可以在终端上运行git branch ,以确认该分支已成功删除。

Sometimes you may have to delete a branch you've already pushed to a remote repository. How can you do this?

有时,您可能必须删除已经推送到远程存储库的分支。 你该怎么做?

如何删除远程分支 (How to delete a remote branch)

To delete a branch remotely, you use the following command:

要远程删除分支,请使用以下命令:

git push <remote-name> --delete <name-of-branch>

where remote-name is the name of the remote repository you want to delete the branch from.

其中remote-name是要从中删除分支的远程存储库的名称。

If I want to delete the branch fix/homepage-changes from origin, I'll do this:

如果我想从origin删除分支fix/homepage-changes ,我将执行以下操作:

git push origin --delete fix/homepage-changes

The branch will be deleted remotely now.

现在该分支将被远程删除。

4.将文件从一个分支合并到另一个分支 (4. Merge a file from one branch to another)

Sometimes, you may want to merge the content of a specific file in one branch into another. For example, you want to merge the content of a file index.html in the master branch of a project into the development branch. How can you do that?

有时,您可能希望将一个分支中特定文件的内容合并到另一个分支中。 例如,您要将项目的master分支中的index.html文件的内容合并到development分支中。 你该怎么做?

First, checkout to the right branch (the branch you want to merge the file) if you've not already done so. In our case, it's the development branch.

首先,签出到右分支(要合并文件的分支)(如果尚未这样做)。 在我们的案例中,它是development部门。

git checkout development

Then merge the file using the checkout --patch command.

然后使用checkout --patch命令合并文件。

git checkout --patch master index.html

If you want to completely overwrite the index.html file on the development branch with the one on the master branch, you leave out the --patch flag.

如果要用master分支上的文件完全覆盖development分支上的index.html文件,则可以--patch标志。

git checkout master index.html

Also, leave out the --patch flag if the index.html file does not exist on the development branch.

另外,如果在development分支上不存在index.html文件,则--patch标志。

5.撤消提交 (5. Undo a commit)

There are times when you've committed your changes incorrectly and you want to undo this commit. Sometimes, you may have even pushed the changes to a remote branch. How do you undo or delete this commit? Let's start with undoing a local commit.

有时候,您错误地提交了更改,而您想撤消此提交。 有时,您甚至可能将更改推送到远程分支。 您如何撤消或删除此提交? 让我们从撤消本地提交开始。

如何撤消本地提交 (How to undo a local commit)

One way you can undo a commit locally is by using git reset. For example, if you want to undo the last commit made, you can run this command:

您可以在本地撤消提交的一种方法是使用git reset 。 例如,如果要撤消上一次提交的操作,可以运行以下命令:

git reset --soft HEAD~1

The --soft flag preserves the changes you've made to the files you committed, only the commit is reverted. However, if you don't want to keep the changes made to the files, you can use the --hard flag instead like this:

--soft标志保留您对提交的文件所做的更改,仅还原提交。 但是,如果您不想保留对文件所做的更改,则可以使用--hard标志,如下所示:

git reset --hard HEAD~1

Note that you should use the --hard flag only when you are sure that you don't need the changes.

请注意,仅当确定不需要更改时,才应使用--hard标志。

Also, note that HEAD~1 points to the last commit. If you want to undo a commit before that, you can use git reflog to get a log of all previous commits. Then use the git reset command with the commit hash (the number you get at the beginning of each line of history). For example, if my commit hash is 9157b6910, I'll do this

另外,请注意HEAD~1指向最后一次提交。 如果您想在此之前撤消提交,则可以使用git reflog获取所有先前提交的日志。 然后将git reset命令与提交哈希(在历史记录的每一行开头获得的数字)一起使用。 例如,如果我的提交哈希为9157b6910 ,我将这样做

git reset --soft 9157b6910

如何撤消远程提交 (How to undo a remote commit)

There are times you want to undo a commit you have pushed to a remote repository. You can use git revert to undo it locally and push this change to the remote branch.

有时您想撤消已推送到远程存储库的提交。 您可以使用git revert在本地撤消它,并将此更改推送到远程分支。

First, get the commit hash using git reflog.

首先,使用git reflog获取提交哈希。

git reflog

Then revert it. Assuming my commit hash is 9157b6910, I'll do the following:

然后还原它。 假设我的提交哈希为9157b6910,我将执行以下操作:

git revert 9157b6910

Finally, push this change to the remote branch.

最后,将此更改推送到远程分支。

摘要 (Summary)

In this article, we discussed commands to do the following in Git:

在本文中,我们讨论了在Git中执行以下操作的命令:

  1. Add Remote Repositories

    添加远程存储库
  2. Change remote repositories

    更改远程存储库
  3. Delete a branch

    删除分支
  4. Merge a file from one branch to another

    将文件从一个分支合并到另一个分支
  5. Undo a commit locally and remotely

    本地和远程撤消提交

Maybe someday, I'll write about more things you can do with Git.

也许有一天,我会写更多关于Git可以做的事情。

I hope you enjoyed the article. Thanks for reading.

希望您喜欢这篇文章。 谢谢阅读。

Want to get notified when I publish a new article? Click here.

想在我发表新文章时得到通知吗? 请点击这里

翻译自: https://www.freecodecamp.org/news/5-git-commands-you-should-know-with-code-examples/

vue项目示例代码git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值