Open main menu

CDOT Wiki β

Changes

DPS909 & OSD600 Winter 2017 - Git Walkthrough

4,393 bytes added, 17:41, 11 January 2017
Step 7: fixing common mistakes
</pre>
Another common scenario that happens is accidentally staging changes to files that you didn't mean to add. For example, you might be testing a fix you've made, and need to hard-code a filename in another file to see if it works. Once you know it's working, you want to commit the fix but not your testing code. People often get into this problem when they add too much at once, for example <code>git add *</code>. This will work, but it adds '''everything''' that's different in your working directory.
Let's modify two files, <code>CONTRIBUTING.md</code> and <code>README.md</code>. I've made two small modifications, replacing one word in each file. Here's my repo's status and diff: <pre>$ git statusOn branch masterChanges not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)  modified: CONTRIBUTING.md modified: README.md no changes added to commit (use "git add" and/or "git commit -a")$ git diffdiff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex a0745d7..ffcd657 100755--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -7,7 +7,7 @@ process easy and effective for everyone involved.  Following these guidelines helps to communicate that you respect the time of the developers managing and developing this open source project. In return,-they should reciprocate that respect in addressing your issue or assessing+they should reciprocate that respect in addressing your issue or reviewing patches and features.  diff --git a/README.md b/README.mdindex a8a2c26..27f324b 100755--- a/README.md+++ b/README.md@@ -12,7 +12,7 @@  [![Selenium Test Status](https://saucelabs.com/browser-matrix/bootstrap.svg)](https://saucelabs.com/u/bootstrap) -Bootstrap is a sleek, intuitive, and powerful front-end framework for faster and easier web development, created by [Mark Otto](https://twitter.com/mdo) and [Jacob Thornton](https://twitter.com/fat), and maintained by the [core team](https://github.com/orgs/twbs/people) with the massive support and involvement of the community.+Bootstrap is a fun, intuitive, and powerful front-end framework for faster and easier web development, created by [Mark Otto](https://twitter.com/mdo) and [Jacob Thornton](https://twitter.com/fat), and maintained by the [core team](https://github.com/orgs/twbs/people) with the massive support and involvement of the community.  To get started, check out <https://getbootstrap.com>!</pre> Let's "accidentally" add all changed files at once (i.e., use <code>*</code> instead of individual filenames): <pre>$ git add *$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)  modified: CONTRIBUTING.md modified: README.md</pre> What if I didn't mean to include the change to <code>CONTRIBUTING.md</code> in this commit? Git helpfully tells me what to do: <code>use "git reset HEAD <file>..." to unstage</code>. This means to reset what you have in the staging area for the given file with what was in the last commit (the <code>HEAD</code>), essentially undoing your last staged changes. Let's try it! <pre>$ git reset HEAD CONTRIBUTING.mdUnstaged changes after reset:M CONTRIBUTING.md$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)  modified: README.md Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)  modified: CONTRIBUTING.md</pre> My changes to <code>CONTRIBUTING.md</code> are still present in my working directory, but they are no longer '''staged''' as part of this commit. I could go one step further and ask git to "undo" all changes to <code>CONTRIBUTING.md</code> in my working directory. This is more destructive than what we did above, since it means rewriting the file exactly as it was in the last commit. But, when you want to get back to a known-good state, it's an easy way to correct things. <pre>$ git checkout -- CONTRIBUTING.md$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)  modified: README.md</pre> Now the changes I made to <code>CONTRIBUTING.md</code> are gone, and the last-known version of that file is what is in my working directory once more.
* Client Server (SVN) and Distributed (Git)