Git-patch-operation

提供:Dev Guides
移動先:案内検索

Git-パッチ操作

パッチはテキストファイルで、その内容はGit diffと似ていますが、コードとともに、コミットに関するメタデータも含まれています。例:コミットID、日付、コミットメッセージなど コミットからパッチを作成でき、他の人はそれらをリポジトリに適用できます。

Jerryは彼のプロジェクトにstrcat関数を実装しています。 Jerryは自分のコードのパスを作成して、Tomに送信できます。 次に、受け取ったパッチを自分のコードに適用できます。

JerryはGit format-patch コマンドを使用して、最新のコミット用のパッチを作成します。 特定のコミット用のパッチを作成する場合は、format-patchコマンドで COMMIT_ID を使用します。

[jerry@CentOS project]$ pwd
/home/jerry/jerry_repo/project/src

[jerry@CentOS src]$ git status -s
M string_operations.c
?? string_operations

[jerry@CentOS src]$ git add string_operations.c

[jerry@CentOS src]$ git commit -m "Added my_strcat function"

[master b4c7f09] Added my_strcat function
1 files changed, 13 insertions(+), 0 deletions(-)

[jerry@CentOS src]$ git format-patch -1
0001-Added-my_strcat-function.patch

上記のコマンドは、現在の作業ディレクトリ内に .patch ファイルを作成します。 トムはこのパッチを使用してファイルを変更できます。 Gitは、パッチ* git am および *git apply をそれぞれ適用する2つのコマンドを提供します。 Git apply はコミットを作成せずにローカルファイルを変更し、 git am はファイルを変更してコミットも作成します。

パッチを適用してコミットを作成するには、次のコマンドを使用します-

[tom@CentOS src]$ pwd
/home/tom/top_repo/project/src

[tom@CentOS src]$ git diff

[tom@CentOS src]$ git status –s

[tom@CentOS src]$ git apply 0001-Added-my_strcat-function.patch

[tom@CentOS src]$ git status -s
M string_operations.c
?? 0001-Added-my_strcat-function.patch

パッチが正常に適用されると、 git diff コマンドを使用して変更を表示できます。

[tom@CentOS src]$ git diff

上記のコマンドは、次の結果を生成します-

diff --git a/src/string_operations.c b/src/string_operations.c
index 8ab7f42..f282fcf 100644
--- a/src/string_operations.c
+++ b/src/string_operations.c
@@ -1,5 +1,16 @@
#include <stdio.h>
+char *my_strcat(char *t, char *s)
diff --git a/src/string_operations.c b/src/string_operations.c
index 8ab7f42..f282fcf 100644
--- a/src/string_operations.c
+++ b/src/string_operations.c
@@ -1,5 +1,16 @@
#include <stdio.h>
+char *my_strcat(char *t, char *s)

{

   char *p = t;



   while (*p)
   ++p;

   while (*p++ = *s++)
   + ;
   + return t;

}

size_t my_strlen(const char *s)
{
   const char *p = s;
   @@ -23,6 +34,7 @@ int main(void)
   {