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:
在本文中,我們將學習如何執行以下操作:
- Add remote repositories 添加遠程存儲庫
- Change remote repositories 更改遠程存儲庫
- Delete a branch 刪除分支
- Merge a file from one branch to another 將文件從一個分支合并到另一個分支
- 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中執行以下操作的命令:
- Add Remote Repositories 添加遠程存儲庫
- Change remote repositories 更改遠程存儲庫
- Delete a branch 刪除分支
- Merge a file from one branch to another 將文件從一個分支合并到另一個分支
- 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