天天看點

成為Git專家的循序漸進指南 什麼是Git? (What is Git?) 基本的Git工作流程 (Basic Git Workflow) 日志記錄 (Log) 分支與合并 (Branching & Merging) 遠端倉庫 (Remote Repository) Git協作工作流程 (Git Collaboration Workflow) 而已! (That’s it!)

Git is the best and most widely used version control system in the world so far. It’s a very well supported open source project with over a decade of solid management that meets the long term needs of most software teams and individuals worldwide. And guess what! It’s free.

到目前為止,Git是世界上最好,使用最廣泛的版本控制系統。 這是一個受到良好支援的開源項目,擁有十多年的堅實管理,可以滿足全球大多數軟體團隊和個人的長期需求。 你猜怎麼着! 免費。

什麼是Git? (What is Git?)

Let’s make it simple and clear, Git is a software that helps you track the changes made to a project over time. It stores these changes and allows you to reference them when needed.

讓我們簡單明了,Git是一款軟體,可幫助您跟蹤随着時間的推移對項目所做的更改。 它存儲這些更改,并允許您在需要時引用它們。

Mastering Git is not as hard as you might think! It’s all about understanding the concepts behind everything it offers. Let’s Start!

掌握Git并不像您想象的那麼難! 一切都是為了了解其所提供的一切背後的概念。 開始吧!

基本的Git工作流程 (Basic Git Workflow)

Initially, enter your local project directory and run the following command:

最初,輸入您的本地項目目錄并運作以下指令:

$ git init
           

Init command will create a new git repository for this project. This local repository will be created as a ‘.git’ folder inside your project’s root directory so you will be the only person allowed to work with it by editing and committing changes.

Init指令将為此項目建立一個新的git存儲庫。 該本地存儲庫将在項目的根目錄中建立為“ .git”檔案夾,是以您将是唯一允許通過編輯和送出更改來使用它的人。

Take a look at the following workflow chart:

檢視以下工作流程圖:

成為Git專家的循序漸進指南 什麼是Git? (What is Git?) 基本的Git工作流程 (Basic Git Workflow) 日志記錄 (Log) 分支與合并 (Branching & Merging) 遠端倉庫 (Remote Repository) Git協作工作流程 (Git Collaboration Workflow) 而已! (That’s it!)

The local Git project consists of three parts:

本地Git項目包括三個部分:

  1. Working Directory: where you will be doing all the work: create, insert, modify, add, delete, copy, rename and organise files.

    工作目錄:您将在其中執行所有工作:建立,插入,修改,添加,删除,複制,重命名群組織檔案。

  2. Staging Area: where you will list all the changes that you want to commit.

    暫存區:您将在其中列出要送出的所有更改。

  3. Repository: where the changes will be permanently stored as a new version.

    存儲庫:更改将被永久存儲為新版本。

After editing the files in the working directory, use the following command in order to pass the changes you have made to the Staging Area:

在工作目錄中編輯檔案之後,請使用以下指令,以将您所做的更改傳遞到暫存區:

$ git add filename
           

‘filename’ refers to the name of the file that you want to add.

“檔案名”是指您要添加的檔案的名稱。

You can always track the difference between the working directory and the staging area by using:

您始終可以使用以下方法跟蹤工作目錄和暫存區之間的差異:

$ git diff filename
           

The last step of the basic Git workflow is to commit the changes. A commit will permanently store all the changes from the staging area into the repository.

基本的Git工作流程的最後一步是送出更改。 一次送出會将所有從登台區域進行的更改永久存儲到存儲庫中。

$ git commit -m "[descriptive message]"
           

Make sure to describe what this commit is all about by writing a clear message wrapped with quotation marks.

確定通過編寫帶引号引起的清晰消息來描述此送出的全部内容。

This is all you need to know for the basic Git workflow! knowing these simple commands and mastering them will give you the ability to track changes in your local repository easily and professionally.

這是基本的Git工作流程所需的全部知識! 了解這些簡單指令并精通它們将使您能夠輕松,專業地跟蹤本地存儲庫中的更改。

日志記錄 (Log)

From time to time, you would like to take a look of what happened so far in your project. This is where the log command comes to help:

您不時希望了解一下項目中到目前為止發生的情況。 這是log指令提供幫助的地方:

$ git log
           

All the commits you’ve done are stored chronologically and can be viewed. The log command will list all these commits allowing you to see which changes were made and helps you comprehend how the project evolved.

您所做的所有送出都按時間順序存儲,并且可以檢視。 log指令将列出所有這些送出,使您可以檢視進行了哪些更改,并幫助您了解項目的發展方式。

分支與合并 (Branching & Merging)

In real software project, there are always multiple contexts where developers work. You might have to develop new feature and also fix some troubling bugs, or maybe you need to work remotely with other teammates each in different feature.

在實際的軟體項目中,開發人員始終在多個環境中工作。 您可能需要開發新功能并修複一些令人煩惱的錯誤,或者您可能需要與其他使用不同功能的隊友進行遠端合作。

The best way to do this is by creating a new branch for each different context and keep the development process isolated. This will also gives you the freedom to experiment new things without worrying about what might happen to the project, since you can keep the original branch untouched and working perfectly.

最好的方法是為每個不同的上下文建立一個新分支,并使開發過程保持隔離。 這也使您可以自由地嘗試新事物,而不必擔心項目可能會發生什麼,因為您可以使原始分支保持不變并且可以正常工作。

成為Git專家的循序漸進指南 什麼是Git? (What is Git?) 基本的Git工作流程 (Basic Git Workflow) 日志記錄 (Log) 分支與合并 (Branching & Merging) 遠端倉庫 (Remote Repository) Git協作工作流程 (Git Collaboration Workflow) 而已! (That’s it!)

In order to know in which branch you currently exist, use the following command:

為了知道您目前在哪個分支中,請使用以下指令:

$ git branch
           

To create a new branch, use:

要建立一個新分支,請使用:

$ git branch new_branch_name
           

‘new_branch_name’ is the name of the new branch you would like to create, it should be related to the purpose of the branch.

“ new_branch_name”是您要建立的新分支的名稱,它應與分支的用途有關。

The new created branch is identical to the current branch. In order to start working on our new branch, we probably want to switch to it:

新建立的分支與目前分支相同。 為了開始在我們的新分支上工作,我們可能想切換到它:

$ git checkout branch_name
           

After we are done with developing the new feature and we want to include it in our main branch, we can easily accomplish this by using the merge command:

開發完新功能并将其包含在主分支中之後,我們可以使用merge指令輕松完成此操作:

$ git merge branch_name
           
成為Git專家的循序漸進指南 什麼是Git? (What is Git?) 基本的Git工作流程 (Basic Git Workflow) 日志記錄 (Log) 分支與合并 (Branching & Merging) 遠端倉庫 (Remote Repository) Git協作工作流程 (Git Collaboration Workflow) 而已! (That’s it!)

In case the main branch hasn’t changed since the time we created the new branch, the merge process will be completed successfully. Otherwise, if the main branch contains new commits which are not included in our new branch, we will face conflicts. In this case, Git will need your help to decide which changes to keep, and what to dismiss.

如果自建立新分支以來主分支沒有發生變化,合并過程将成功完成。 否則,如果主分支包含新送出而不包含在新分支中,則我們将面臨沖突。 在這種情況下,Git将需要您的幫助來決定保留哪些更改以及取消哪些更改。

After the conflict occurs, you can open the files manually and edit them the way you want them to be, add the files to the staging area and finally commit the changes.

發生沖突後,您可以手動打開檔案并按所需方式對其進行編輯,然後将檔案添加到暫存區中,最後送出更改。

After the development process of the new feature is done, and the new changes are merged into the main branch, there is no need to keep the new branch. You can simply remove it by using the following command:

在完成新功能的開發過程并将新更改合并到主分支後,無需保留新分支。 您隻需使用以下指令即可将其删除:

$ git branch -d branch_name
           

Branching is one of Git’s most powerful features, it is fast and easy and there is no reason not to use it! If you aim to enhance your everyday workflow, use it extensively!

分支是Git最強大的功能之一,它既快速又簡單,沒有理由不使用它! 如果您打算改善日常工作流程,請廣泛使用它!

遠端倉庫 (Remote Repository)

When it comes to working with a team, remote repositories come into play. A remote is a shared Git repository that enables multiple collaborators from different locations to work on the same Git repository independently.

與團隊合作時,遠端存儲庫将發揮作用。 遠端是一個共享的Git存儲庫,它使來自不同位置的多個協作者可以獨立地在同一個Git存儲庫上工作。

In case you get on board for an existing project, you can have a copy of it in your local machine by using the clone command:

如果您參與了現有項目,則可以使用clone指令在本地計算機中獲得其副本:

$ git clone remote_location clone_name
           

‘remote_location’ is the location of the remote repository, it can be a web address or file path. Git will give the remote address the name ‘origin’ so you can easily refer to it.

“ remote_location”是遠端存儲庫的位置,它可以是網址或檔案路徑。 Git會将遠端位址命名為“ origin”,以便您輕松引用它。

Git協作工作流程 (Git Collaboration Workflow)

This is the typical Git collaboration workflow that you should always follow:

這是您應始終遵循的典型Git協作工作流程:

  1. Fetch new changes from the remote and merge them into your branch.

    從遠端擷取新更改,并将其合并到您的分支中。

  2. Create a new branch for your work (new feature, bug fixes, …).

    為您的工作建立一個新分支(新功能,錯誤修複等)。

  3. Commit your work.

    投入你的工作。

  4. Fetch and merge again.

    再次擷取并合并。

  5. Push your branch to the remote repository.

    将分支推送到遠端存儲庫。

You might be wondering why step 4 is needed. When you work with a team, other teammates might update the remote while you are working locally. This is why it is always an important and smart step to make, always fetch new changes before you update the remote.

您可能想知道為什麼需要執行步驟4。 與團隊一起工作時,其他團隊成員可能會在本地工作時更新遙控器。 這就是為什麼它總是重要而明智的步驟,總是在更新遙控器之前擷取新的更改。

To fetch new updates, use the following command:

要擷取新更新,請使用以下指令:

$ git fetch
           

You’ve got the new changes, but your local branch has not been updated yet. Use the merge command we’ve mentioned previously in order to integrate the new branch into your local branch.

您已經進行了新的更改,但是您的本地分支尚未更新。 使用我們前面提到的merge指令,以便将新分支內建到您的本地分支中。

And, for the last step:

并且,對于最後一步:

$ git push origin your_branch_name
           

This command will push your branch to the remote so other teammates can see it.

此指令會将您的分支推送到遠端,以便其他隊友可以看到它。

而已! (That’s it!)

Congratulations, you now know all you need to start working with Git! Mastering Git is one of the most powerful and essential skills that every developer should have. I hope that you enjoyed reading this guide and As always, if you have any questions, do not hesitate to leave a message.

恭喜,您現在知道開始使用Git所需的一切! 掌握Git是每個開發人員都應該擁有的最強大和必不可少的技能之一。 希望您喜歡閱讀本指南,并且與往常一樣,如果您有任何疑問,請随時留言。

In a future article, I will talk about Github! Subscribe to my blog to make sure you don’t miss it.

在以後的文章中,我将讨論Github! 訂閱我的部落格 ,以確定您不會錯過它。

Originally published at http://shadencodes.com on July 24, 2020.

最初于 2020年7月24日 釋出在 http://shadencodes.com 。

翻譯自: https://medium.com/swlh/a-step-by-step-guide-to-becoming-a-git-expert-1ff5ebdb2bfa
git