• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Git: Undo change in working directory and staging area

Như đã giới thiệu cho bạn cách undo change (quay lui thay đổi) các commit ở local repo. Vậy trong bài viết lần này, chúng tôi xin giới thiệu cách undo change ở staging area và working directory

Git-Undo-change-in-working-directory-and-staging-area.png


1. Undo change in staging area

Ở một số trường hợp, ghi chạy lệnh git add sẽ đẩy file từ working directory vào staging area để chuẩn bị commit, chúng ta nghĩ lại và nhận ra có một số file cần được sửa thêm trước khi commit, do đó cần phải gỡ nó ra khỏi staging area.

Xem thêm: lập trình ứng dụng

Ví dụ, vẫn với repo tôi dùng ở bài viết trước, tôi tạo 2 file style.css và script.js và add vào staging, sau đó kiểm tra trạng thái repo:

Xem thêm: ngôn ngữ lập trình web

Các bạn có thể thấy 2 file đều đã được thêm vào staging area

Xem thêm: lập trình website

Giờ tôi kiểm tra lại và thấy file script.js còn một số lỗi cần sửa, tôi muốn đưa nó ra khỏi vùng staging area để quay về working directory. Tôi chạy lệnh sau:

sau đó kiểm tra trạng thái repo bằng git status:

Các bạn có thể thấy file script.js đã được đưa ra khỏi staging area. Nếu chúng ta thực hiện lệnh git commit, sẽ chỉ có file style.css được commit vào local repo

2. Undo change in working directory

Nói một cách chính xác thì tôi sẽ undo change với những untracked files, tức là những file chưa được thêm vào staging area. Và với Git, undo untracked files đồng nghĩa với việc xoá sạch những file đó ra khỏi thư mục :v. Vì thế, hãy cân nhắc thật kĩ trước khi quyết định undo

Hiện tại repo của tôi đang có 1 untracked file là script.js. Tuy nhiên thì để cho chắc ăn, tôi sẽ xem Git sẽ undo (xoá) những file nào trong thư mục đi bằng lệnh sau:

Như vậy, khi undo (xoá) các untracked files ra khỏi thư mục, git sẽ xoá file script.js đi.

Sau khi đã thực sự chắc chắn rằng cần phải xoá file script.js đi, tôi sẽ chạy lệnh sau:

Như vậy, trong bài viết này tôi đã giới thiệu 2 lệnh để undo changes ở staging area và working directory:

- git reset HEAD <đường-dẫn-file>: Đưa file từ staging area về working directory

- git clean -f: Xoá các untracked files
 

Facebook Comments

Similar threads
Thread starter Title Forum Replies Date
T Hướng dẫn làm việc local repo và remote repo trong Git Tin tức CNTT 0
Admin Change the height of the cke text editor vBulletin Vbulletin 0
Admin [JoyFreak] Change forum_list title and H1 only Xenforo 0
Admin [OzzModz]/[Kirby] Filtered User Change Log Xenforo 0
Admin [ITD] Change User Online Indicator Icon & Color Xenforo 0
Admin Hướng dẫn đổi tên ứng dụng android hiển thị trên màn hình xamarin forms - Change app name on main screen Android, iOS 0
N Auto Change IP Vip72 SQL injection 1
N Auto Change IP Vip72 SQL injection 0
Admin Hướng dẫn thay biểu tượng chuyên mục với ảnh xenforo 2.x - How to change Node Icons to Images xenforo 2.x Xenforo 0
Admin Hướng dẫn đổi màu thanh tiêu đề, thanh tác vụ, viền windows 10 - change color title bar, taskbar, border windo Sử dụng, chia sẻ, hỏi đáp 0
rockymen9999 [vn5socks.com] change to [www.vn5socks.net] Good socks1h50 PM Tut, tool, mmo 1
rockymen9999 [vn5socks.com] change to [www.vn5socks.net] Good socks 4h50 PM Tut, tool, mmo 0
rockymen9999 Share [vn5socks.com] change to [www.vn5socks.net] Good socks 1h45 PM Tut, tool, mmo 1
rockymen9999 Share [vn5socks.com] change to [www.vn5socks.net] Good socks 10h30 AM Tut, tool, mmo 0
rockymen9999 Share Vn5socks.com change to www.vn5socks.net ! Thank you 12h10 AM Tut, tool, mmo 0
Admin Change threadrow color on mouseover Vbb tutorial 0
Admin Prevent Email Address Change For Vbb Add-ons 0
Admin Change Cursor for your forum 4.x Vbb tutorial 0
Admin Change username 1.2.0 Xenforo 1
Admin [DBTech] Username Change v1 (vB4) Add-ons 0
Admin TW7S - Change the look of the Login/Register in header Vbb tutorial 0
Admin TW7S - Change the look of "Last Post" by Vbb tutorial 0
Admin Change CODE and PHP tags design Add-ons 0
Admin Change quotation design Add-ons 0
Admin Hướng dẫn gửi mail change key domain tại melbourneit Hosting / Domain 4
Admin Forum Home Subscribe Image Change Add-ons 0
Admin Change Thread Title Color for Specific Forums on Homepage Add-ons 0
Admin Change Title of Podcast Add-ons 0
Admin Change Background Color for Specific Forums on Homepage Add-ons 0
Admin Brivium - Change Threads/Posts Owner 1.0.0 Xenforo 0
L Hướng dẫn Làm như thế nào change skin vbb? Vbulletin 1
M A Thang change dns giùm em Hosting & Domain Share 6
Admin Change Author And Creation Date Of Posts (Thay đổi tác giả và thời gian post bài) Add-ons 0
Admin How to change the (What's Going On?) box to100% in forumhome Add-ons 0
Admin Change link color only in post and/or cms articles Add-ons 0
Admin Change Threads/Posts Owner/Date for vB4 Add-ons 0
Admin Change Author And Creation Date Of Posts And Articles Add-ons 0
Admin Share video tut change pass 2 zing Hacking 0
A Code scam auto change pass Ý tưởng, góp ý, sáng kiến mới của thành viên 8

Similar threads

New posts New threads New resources

Back
Top