کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

Merge در Git

ادغام (Merge) Branch ها در Git


ادغام Branch ها


حالا که خطا اضطراری مان برطرف شده، باید emergency-fix branch را با master ادغام (Merge) کنیم.


ابتدا، باید به master branch برویم:


git checkout master
Switched to branch 'master'

             

حالا این branch ( master) را با emergency-fix ادغام میکنیم:


git merge emergency-fix
Updating 09f4acd..dfa79db
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

             

ازآنجا که emergency-fix مستقیما از master گرفته شده است و در حالی که ما بر خطا کار میکردیم، تغییری در master ایجاد نشده است، Git این بخش را به عنوان ادامه ی master در نظر میگیرد. تا با سرعت بیشتری عمل کرده و master و emergency-fix به یک commit اشاره کنند.


حالا که master و emergency-fix یکی شده اند، میتوانیم emergency-fix را حذف کنیم، زیرا دیگر نیازی به آن نداریم:


git branch -d emergency-fix
Deleted branch emergency-fix (was dfa79db).

             

ناسازگاری در Merge


حالا میتوانیم به hello-world-images بازگردیم و به کار خود ادامه دهیم. یک فایل دیگر تصویر ( img_hello_git.jpg) اضافه کرده و index.html را به شکلی تغییر میدهیم که این تصویر را نمایش دهد:


git checkout hello-world-images
Switched to branch 'hello-world-images'

             




Hello World!




Hello world!

Hello World from Space

This is the first file in my new Git Repo.

A new line in our file!

Hello Git

حالا که کارمان تمام شد، میتوانیم این branch را stage و commit کنیم:


git add --all
git commit -m "added new image"
[hello-world-images 1f1584e] added new image
 2 files changed, 1 insertion(+)
 create mode 100644 img_hello_git.jpg

             

حالا باید hello-world-images را با master ادغام کنیم. اما اخیرا تغییراتی در master ایجاد شده است.


مشاهده میکنید که index.html در هر دو branch تغییر کرده است حالا که میخواهیم hello-world-images را با master ادغام کنیم، تغییرات اخیر master چه میشود؟


git checkout master
git merge hello-world-images
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

             

ادغام با خطا مواجه شد، زیرا بین نسخه های index.html ناسازگاری وجود دارد.


وضعیت را بررسی میکنیم:


git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:
        new file:   img_hello_git.jpg
        new file:   img_hello_world.jpg

Unmerged paths:
  (use "git add ..." to mark resolution) both modified: index.html

             

مشاهده میشود که در index.html ناسازگاری وجود دارد، اما فایل های تصویر stage شده اند و آماده commit شدن هستند.


پس باید این ناسازگاری را برطرف کنیم. فایل را در editor خود باز کنید:






Hello World!




Hello world!

Hello World from Space

This is the first file in my new Git Repo.

<<<<<<< HEAD

This line is here to show how merging works.

=======

A new line in our file!

Hello Git
>>>>>>> hello-world-images

میتوانیم تفاوت های موجود در نسخه را مشاهده کرده و به شکل دلخواه تغییرش دهیم:





Hello World!




Hello world!

Hello World from Space

This is the first file in my new Git Repo.

This line is here to show how merging works.

Hello Git

حالا index.html را stage کرده و وضعیت را بررسی میکنیم:


git add index.html
git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
        new file:   img_hello_git.jpg
        new file:   img_hello_world.jpg
        modified:   index.html

             

ناسازگاری برطرف شده و میتوانیم با استفاده از commit، ادغام را انجام دهیم:



git commit -m "merged with hello-world-images after fixing conflicts"
[master e0b6038] merged with hello-world-images after fixing conflicts

             

و سپس hello-world-images branch را حذف کنیم


git branch -d hello-world-images
Deleted branch hello-world-images (was 1f1584e).

             

حالا که با branch ها و merge ها آشنا شده ایم، میتوانیم با remote repository کار کنیم.


1400/05/04 1853 379
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...