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

دستور Reset در Git

Git Reset


Git Reset چیست


با استفاده از دستور reset میتوان repository را به یک commit پیشین بازگرداند. هر تغییری پس از commit منتخب از بین خواهد رفت.


گام اول : یافتن commit دلخواه:


انتخاب  commit مورد نظر برای reset

گام دوم: repository را به حالت منتخب باز میگردانیم:


بازگشت یک commit با استفاده از reset

پس از انجام مرحله قبل، میتوانیم به یکی از بخش های موجود در تاریخچه commit مان بازگردیم. این کار را با کمک reset انجام میدهیم.


یافتن commit مورد نظر در Log


ابتدا باید نقطه ای که قصدر داریم به آن بازگردیم را بیابیم. برای این کار Log را بررسی میکنیم.


برای جلوگیری از طولانی شدن بیش از حد log، از دستور - - oneline استفاده میکنیم. این دستور تنها یک خط از هر log را نمایش خواهد داد:


  • هفت کاراکتر اول commit bash - که با استفاده از آن دستور reset را اجرا میکنیم.
  • commit message

حالا نقطه مورد نظر را برای reset می یابیم:



git log --oneline
e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..."
52418f7 Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

               

قصد داریم به commit: 9a9add8 (origin/master) Added .gitignore ، یعنی درست پیش از آنکه پروژه را خراب کنیم، باز گردیم.


Git Reset


حالا با استفاده از دستور git reset commithash ، repository خود را به یک commit مشخص باز میگردانیم ( commithash ، هفت کاراکتر اول commit hash است که در log قرار دارد):



git reset 9a9add8

               

حالا دوباره Log را بررسی میکنیم:


git log --oneline
9a9add8 (HEAD -> master, origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

               
هشدار:

ایجاد تغییرات در تاریخچه commit اصلا توصیه نمیشود. البته اگر بر repository خودتان کار میکند تغییرات کوچک مشکلی ایجاد نخواهد کرد، اما از ایجاد تغییراتی که باعث حذف و یا باز نویسی تاریخچه remote repository میشود اجتناب کنید. مخصوصا اگر این repo مورد استفاده ی چند نفر باشد.


Undo Reset


با اینکه commit های پس از نقطه reset در log نمایش داده نمیشوند، اما از Git حذف نشده اند.


اگر commit hash را بدانید، میتوانید به آن نقطه بروید:


git reset e56ba1f
               

حالا دوباره log را بررسی کنید:


git log --oneline
e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..."
52418f7 Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

               
1400/05/13 2184 407
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

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