Open main menu

CDOT Wiki β

Changes

DPS909 & OSD600 Winter 2017 - Git Walkthrough

4,240 bytes added, 17:03, 11 January 2017
Step 6: moving and removing files
===Step 6: moving and removing files===
We want to record every change we make to our files. This includes adding new files, making changes to those files, but also renaming, moving, and deleting. Every action we make with our code is something we want to record in git, something we want to be able to go back and examine or maybe undo. Git makes it easy to do all of this, by adding its own versions of common Unix file commands:* <code>git mv</code> - move or rename a file or directory* <code>git rm</code> - remove files from the working directory and index Let's try them both. First, we'll delete a file, then examine our repo's status: <pre>$ git rm CHANGELOG.mdrm 'CHANGELOG.md'$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)  deleted: CHANGELOG.md</pre>  Here we've '''staged''' a delete, but we still have to '''commit''' it, just like when we '''add''' a file: <pre>$ git commit -m "Remove CHANGELOG"[master 7c16e80] Remove CHANGELOG 1 file changed, 5 deletions(-) delete mode 100755 CHANGELOG.md</pre> The file is now removed from our working directory, and a snapshot of our code without that file has been added to the history. The file still exists in the older snapshots, if we ever want to get it back (see below). If you need to recursively remove files under a directory, you can pass the <code>-r</code> (i.e., Recursive) flag: <pre>$ git commit -m "Rename LICENSE"[master 437bd08] Rename LICENSE 1 file changed, 0 insertions(+), 0 deletions(-) rename LICENSE => LICENSE.txt (100%)$ git rm -r dist/rm 'dist/css/bootstrap-grid.css'rm 'dist/css/bootstrap-grid.css.map'rm 'dist/css/bootstrap-grid.min.css'rm 'dist/css/bootstrap-grid.min.css.map'rm 'dist/css/bootstrap-reboot.css'rm 'dist/css/bootstrap-reboot.css.map'rm 'dist/css/bootstrap-reboot.min.css'rm 'dist/css/bootstrap-reboot.min.css.map'rm 'dist/css/bootstrap.css'rm 'dist/css/bootstrap.css.map'rm 'dist/css/bootstrap.min.css'rm 'dist/css/bootstrap.min.css.map'rm 'dist/js/bootstrap.js'rm 'dist/js/bootstrap.min.js'$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)  deleted: dist/css/bootstrap-grid.css deleted: dist/css/bootstrap-grid.css.map deleted: dist/css/bootstrap-grid.min.css deleted: dist/css/bootstrap-grid.min.css.map deleted: dist/css/bootstrap-reboot.css deleted: dist/css/bootstrap-reboot.css.map deleted: dist/css/bootstrap-reboot.min.css deleted: dist/css/bootstrap-reboot.min.css.map deleted: dist/css/bootstrap.css deleted: dist/css/bootstrap.css.map deleted: dist/css/bootstrap.min.css deleted: dist/css/bootstrap.min.css.map deleted: dist/js/bootstrap.js deleted: dist/js/bootstrap.min.js $ git commit -m "Delete dist/"[master 6ec7cbc] Delete dist/ 14 files changed, 14668 deletions(-) delete mode 100755 dist/css/bootstrap-grid.css delete mode 100755 dist/css/bootstrap-grid.css.map delete mode 100755 dist/css/bootstrap-grid.min.css delete mode 100755 dist/css/bootstrap-grid.min.css.map delete mode 100755 dist/css/bootstrap-reboot.css delete mode 100755 dist/css/bootstrap-reboot.css.map delete mode 100755 dist/css/bootstrap-reboot.min.css delete mode 100755 dist/css/bootstrap-reboot.min.css.map delete mode 100755 dist/css/bootstrap.css delete mode 100755 dist/css/bootstrap.css.map delete mode 100755 dist/css/bootstrap.min.css delete mode 100755 dist/css/bootstrap.min.css.map delete mode 100755 dist/js/bootstrap.js delete mode 100755 dist/js/bootstrap.min.js</pre> Moving and renaming a file are just as easy, and both use the <code>git mv</code> command. Let's add a <code>.txt</code> extension to the <code>LICENSE</code> file, and commit the change: <pre>$ git mv LICENSE LICENSE.txt$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)  renamed: LICENSE -> LICENSE.txt$ git commit -m "Rename LICENSE"[master 437bd08] Rename LICENSE 1 file changed, 0 insertions(+), 0 deletions(-) rename LICENSE => LICENSE.txt (100%)</pre> Telling git about the changes to a file's name or path means that it can track changes to the file contents even when it moves (i.e., git will know that <code>LICENSE</code> and <code>LICENSE.txt</code> represent the same thing in the history)  
* Client Server (SVN) and Distributed (Git)