Keling agar qachondur sizda, o'zgarishlarni ishlab turgan branch-ga emas, balki ularni o'z asl holida qoldirib, bu o'zgarishlarni yangi branch-ga o'tkazishni ko'rib chiqamiz.

gitcheckoutswitch

1. Kirish

Git ancha mashxur va keng foydalanadigan versiya boshqarish sistemasi sifatida foydalnib kelinadi. Ushbu kichik qo'llanmada, biz qanday qilib bir branch-dagi o'zgarishlarni saqlamasdan, yangi branchga saqlashni o'rganamiz.

2. Muammoning ko'rinishi

Eng avvalo, keling odatiy bir ish jarayonini tasavvur qilamiz, masalan Git bilan boshqarilayotgan loyihaga yangi nimadur qo'shishilmoqda

  • Qo'shilayotgan yangiligimiz uchun yangi branch yaratish, masalan feature
  • Yangi qo'shimchamizni qo'shib uni lakalni repo-ga commit qilib olamiz.
  • remot-repo-ga ushbu feature branchni push qiling va pull so'rovi yaratish
  • Bundan so'ng boshqa jamodashlarimizdan biri, yangi o'zgarishlarni, ko'rib chiqadi va agar hammasi joyida bo'lsa, master yoki release branch-ga bilashtirshlari mumkin.

Lekin , bazan biz yana o'sha bir xil feature branch-ga o'zgarishlar kiritib ketaveramiz, va yangi branch yaratib o'sha bilan ishlashni esimzidan chiqarib qo'yamiz. Va natijada, biz xato branch-da ekanlgimizni kech anglab qolamiz, masalan, master branch-ni olsak.

Shuning uchun, endi biz yangi branch yaratib commit qilinmagan o'zgarishlarni yangi branch-ga o'tkazishimiz kerak bo'ladi. Yana ham aniq aytsan birdaniga, master branch o'zgartisilmasligi shart.

Quydagi, misol bilan tushunib olishimiz mumkin. Deylik, bizda myRepo degan Git Repo-miz bor:

.
sh
$ git branch
* master

$ git status
On branch master
nothing to commit, working tree clean
#

Tepada kelitirilgan ekrandan ko'rishimiz mumkin: Biz hozir master branch-damiz. Va, biz ishlab turgan tree toza turibdi.

Keyingi qadamda, keling nimadur o'zgarish kiritaylik:

.
sh
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   Readme.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    a-new-file.txt

no changes added to commit (use "git add" and/or "git commit -a")

3. git checkout buyrug'idan foydalanish

git checkout -b < BRANCH_NOMI > buyrug'i (commandasi) yangi branch yaratadi va shu yangi yaratilgan branchga o'tkazish uchun foydalaniladi. Va yana qo'shimchasiga, biz ishlab turgan barcha commit qilinmagan o'zgarishlarni ham olib o'tadi va eski branchni o'zgartishimizdan oldingi holatida qoldiradi.

Keygi qadamda, keling git checkout buyrug'uni myRepo-da joylashgan loyihamizda sinab ko'ramiz.

.
sh
$ git branch
* master

$ git checkout -b feature1
Switched to a new branch 'feature1'

$ git status
On branch feature1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   Readme.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    a-new-file.txt

no changes added to commit (use "git add" and/or "git commit -a")

Tepadagi buyruqlar natijasi ko'rsatganidek, biz feature1 branchini qo'shdik va barch commit qilinmagan o'zgarishlarni mastre dan feature1 ga ko'chirib o'tkazdik. Keyingi qadamda, keling o'zgarishlarni stage qilamiz va commit qilamiz:

.
sh
$ git add . && git commit -m "yangi qo'shimcha feature1-ni qo'shdim"
[feature1 2ffc161] implemented feature1
 2 files changed, 2 insertions(+)
 create mode 100644 a-new-file.txt

$ git log --abbrev-commit feature1
commit 2ffc161 (HEAD -> feature1)
Author: ...
Date:   ...
    yangi qo'shimcha feature1-ni qo'shdim

commit b009ddf (master)
Author: ...
Date:   ...
    init commit
#

Endi, master branchga qaytib ko'ramiz va biz oldingi holatida o'zgarishsiz qolganini ko'ramiz:

.
sh
$ git checkout master
Switched to branch 'master'
$ git status
On branch master
nothing to commit, working tree clean
$ git log --abbrev-commit master
commit b009ddf (HEAD -> master)
Author: ...
Date:   ...
    init commit

Ko'rishimiz mumkinki, master branch da o'zgarishlar yo'q, va bizdan yangi commit ham talab qilinmaydi:

4. git switch buyrug'idan foydalanish

Biz kuzatishimiz mumkin-ki, Git-ning checkout buyrug'i Syevsariya Armiyasi pichog'iga o'xshaydi. Ushbu bitta buyruq ko'plab turdagi amallarni bajarishi mumkin, ya'ni, ishlab turgan tree-ning fayllarini tiklash, branch-larni o'zgartirish, yangi branch qo'shish, head-ni ko'chirish va x.k . checkout buyrug'ini ishlatish bir muncha ortiqcha sanaladi.

Lekin, Git o'zining 2.23 versiyasida, checkout buyrug'idan foydanishda uchuraydigan qiyinchilik/chalkashlilarni bartaraf qilish maqsadida, git switch buyrug'ini yangilik sifatida kiritdi. Nomidan kelib chiqib aytishimiz mumkin-ki, git switch bizga branchlar orasida ko'chib yurishga yordam beradi. Va yana qo'shimcha sifatida, biz -C qo'shimchasidan foydalangan holda yangi branch yaratishimiz va shu branchga ko'chib o'tishimiz mumkin. Bu deyarli aynan git checkout -b buyrug'iga o'xshab ishlaydi.

Keyingi qadamda, keling xuddi git checkout -b - ga o'xshagan harakatni takrorlaymiz:

.
sh
$ git branch
    feature1
*   master

$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm ...)
  (use "git restore ...)
    deleted:    Readme.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    ReadmeNew.md

...

Tepadagi masalada ko'rishimiz mumkinki, hozirda biz master branchdamiz. Bu galda, biz Readme.md faylni o'chirdik va yangi ReadmeNew.md faylini qo'shib qo'ydik.

Keyingi holatda, keling git switch buyrug'idan foydalanamiz va commit qilinmagan o'zgarishlarni yangi feature2 branchiga ko'chirib o'tkazamiz:

.
sh
$ git switch -C feature2
Switched to a new branch 'feature2'

$ git status
On branch feature2
Changes not staged for commit:
  (use "git add/rm ...)
  (use "git restore ...)
        deleted: Readme.md
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        ReadmeNew.md
...
$ git add . && git commit -m 'feature2 is done'
[feature2 6cd5933] feature2 is done
1 file changed, 0 insertions(+), 0 deletions(-)
rename Readme.md => ReadmeNew.md (100%)
$ git log --abbrev-commit feature2
commit 6cd5933 (HEAD -> feature2)
Author: ...
Date:   ...
      feature2 is done
commit b009ddf (master)
Author: ...
Date:   ...
      init commit

Ko'rib turganimzdek, git switch -C buyrug'i, yangi feature2 branch-ni yaratdi va hamma o'zgarishlarni feature2 branchiga olib o'tdi.

Keling endi biz yana master branchga o'tib o'zgarishlar bo'lmaganligini ko'raylik:

.
sh
$ git switch master
Switched to branch 'master'

$ git status
On branch master
nothing to commit, working tree clean
$ ls -1 Readme.md 
Readme.md

$ git log --abbrev-commit master
commit b009ddf (HEAD -> master)
Author: ...
Date:   ...
    init commit

Ko'rganimzdek master branchidagi hamma o'zgarishlar qaytib tiklanga, ya'ni biz o'chirgan Readme.md fayl qaytarilgan va yangi qo'shilgan ReadmeNew.md fayl yo'q qilingan. Va yana qo'shimchasiga, git log buyrug'i master branch-da yangi commit mavjuda emasligini ko'rsatmoqda.

5. Hulosa

Biz ushbu qisqa postimizda, qanday qilib o'zgarishlari commit qilinmagan branchni yangi branch yaratib barcha o'zgarishlarni ushbu branchga ko'chirib o'tkasizshni ko'rib chiqdik. Ikkita buyrug'umiz ham ishlatishga bir muncha osonligini ko'rishimiz mumkin:

  • git checkout -b <YANGI_BRANCH>
  • git switch -C <YANGI_BRANCH>