作為 git 中的菜鳥,我不知道如何對當前修改過的檔案相對于原始檔案打補丁,即第一個 git 克隆。任何在此開發軟體版本控制方面有經驗的專家,主要是patch作為合作者(在 fork、PR 等中)作業的差異,以便解釋正確的方法?
uj5u.com熱心網友回復:
制作補丁
只需將輸出保存git diff到檔案,您就可以創建一個表示所有未提交更改的補丁:
git diff > my-changes.patch
此補丁只是一個檔案,因此您可以將其通過電子郵件發送給某人或將其保存在 U 盤上或使用它做任何您想做的事情(我不確定您為什么需要這樣做,但您可以)
然后,補丁可以通過
git apply my-changes.patch
您通常不需要手動制作補丁,當您確實需要制作補丁時,它們應該始終來自預先存在的提交。
可以從預先存在的提交中制作補丁,如下所示:
git diff <start commit>..HEAD > my-changes.patch
這將創建一個包含自<start commit>. 您還可以指定<start commit>一個分支、一個標簽(指的是特定的提交)或特定的東西main~10(這意味著主分支,10 次提交前)。
# Get a patch of all changes that haven't been pushed
git diff origin/main..main > local-changes.patch
# Get a patch of all changes on feature-branch
git diff main..feature-branch > changes-on-feature-branch.patch
# Get the last 10 commits as a patch
git diff main~10..main > last-10-commits.patch
# Get all the changes since a particular tag
git diff v1.0.0..main > changes-since-v1.0.0.patch
再一次,您通常不需要手動創建補丁!!!作為一種工具,git diff它對于探索歷史、查看當前更改、查看將被合并的更改或一百萬種其他用途非常有用。它以可以直接應用的形式輸出內容git apply,但我一生中的任何時候都不必手動向某人發送補丁。
在 git 中協作,正確的方式
共有三種典型的協作方式,我將從最簡單到最高級的順序列出。
方法 1:多個協作者,誰都可以訪問
這是您在學校或大多數作業環境中會看到的最典型情況。每個人都可以將代碼推送到同一個存盤庫。
在您的機器上擁有一份 repo 副本后,作業流程如下所示:
對代碼進行更改或更新
提交您的更改:
git add <your changes> && git commit您可以在上傳之前進行任意數量的提交。最好的做法是在每次完成重要的代碼(例如函式或類)時進行小的提交。
下載遠程更改:
git pull如果您的合作伙伴或同事已使用更改更新了存盤庫,則您必須在推送之前執行此操作。這允許您檢查以確保您的更改與其更改一起使用。
上傳您的更改:
git push
方法二:你在做別人的開源專案
You won't always be able to push your changes directly. For example, if you're working on an open source project, you'll typically submit changes for review, and the maintainers will be the ones to merge them.
This is typically done by forking the project, making changes, uploading your changes to your repository (the forked version), and then submitting a pull request to integrate your changes back into the original project.
It looks like this:
- Fork (on github, gitlab, or another service
- Clone the forked version onto your local machine
- Make changes
- Commit your changes
- Push your changes
- Create a pull request to merge your changes back in (You can do this on github, gitlab, or wherever you forked the repo)
Method 3: Email your commits as a patch
This method is typically used for larger, older open-source projects, that don't want to rely on a centralized service like github or gitlab.
It's done by using git send-email to email the patches directly from the command line. Git will create a patch for you automatically. All you have to do is specify the range of commits you want to send as a patch.
For example, if you made your changes on a local branch called dev, then you would use it like this:
git send-email --compose --from=<your email> --to=<their email> master..dev
Here, master..dev is the range of patches you're sending. It's all the patches on the dev branch, that aren't on the master branch. AKA, your changes.
This command will open a text editor where you can write the body of your email, and git will attach the patch to the email, and send it.
Note that you have to configure your email to work with git send-email. Beginners will not typically need to do this.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352847.html
