Discard all changes and sync to the HEAD of the repository
TweetPosted on Sunday Mar 09, 2014 at 09:38AM in Technology
Assume this:
kyle-no-MacBook:gitprac5 kyle$ ls -l total 8 -rw-r--r--+ 1 kyle staff 5 3 9 09:40 hoge.txt kyle-no-MacBook:gitprac5 kyle$ cat hoge.txt hoge kyle-no-MacBook:gitprac5 kyle$ git status # On branch master nothing to commit, working directory clean kyle-no-MacBook:gitprac5 kyle$
Wasting working tree
kyle-no-MacBook:gitprac5 kyle$ echo hoge >> hoge.txt kyle-no-MacBook:gitprac5 kyle$ echo untracked > untracked.txt kyle-no-MacBook:gitprac5 kyle$ echo tracked > tracked.txt kyle-no-MacBook:gitprac5 kyle$ git add tracked.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: tracked.txt # # 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: hoge.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # untracked.txt kyle-no-MacBook:gitprac5 kyle$ ls -l total 24 -rw-r--r--+ 1 kyle staff 10 3 9 09:45 hoge.txt -rw-r--r--+ 1 kyle staff 8 3 9 09:45 tracked.txt -rw-r--r--+ 1 kyle staff 10 3 9 09:45 untracked.txt kyle-no-MacBook:gitprac5 kyle$
Do it
- These 2 commands will work:
git reset --hard HEAD git clean -f
- Let's do it:
kyle-no-MacBook:gitprac5 kyle$ git reset --hard HEAD HEAD is now at 31e0ee6 initial kyle-no-MacBook:gitprac5 kyle$ ls -l total 16 -rw-r--r--+ 1 kyle staff 5 3 9 09:46 hoge.txt -rw-r--r--+ 1 kyle staff 10 3 9 09:45 untracked.txt kyle-no-MacBook:gitprac5 kyle$ git clean -f Removing untracked.txt kyle-no-MacBook:gitprac5 kyle$ ls -l total 8 -rw-r--r--+ 1 kyle staff 5 3 9 09:46 hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master nothing to commit, working directory clean kyle-no-MacBook:gitprac5 kyle$
- When you want to delete directories, you can use “git clean -fd”.
Tags: git
Delete and move
TweetPosted on Sunday Mar 09, 2014 at 09:20AM in Technology
Assume this:
kyle-no-MacBook:gitprac5 kyle$ ls -l total 8 -rw-r--r--+ 1 kyle staff 5 3 9 09:27 hoge.txt kyle-no-MacBook:gitprac5 kyle$ cat hoge.txt hoge kyle-no-MacBook:gitprac5 kyle$ git status # On branch master nothing to commit, working directory clean kyle-no-MacBook:gitprac5 kyle$
Delete
Delete from working tree
kyle-no-MacBook:gitprac5 kyle$ rm hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes not staged for commit: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: hoge.txt # no changes added to commit (use "git add" and/or "git commit -a") kyle-no-MacBook:gitprac5 kyle$
Add deletion to index
kyle-no-MacBook:gitprac5 kyle$ git rm hoge.txt rm 'hoge.txt' kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: hoge.txt # kyle-no-MacBook:gitprac5 kyle$
Commit
kyle-no-MacBook:gitprac5 kyle$ git commit -m 'delete' [master 8793936] delete 1 file changed, 1 deletion(-) delete mode 100644 hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master nothing to commit, working directory clean kyle-no-MacBook:gitprac5 kyle$ ls -l kyle-no-MacBook:gitprac5 kyle$
Move / Rename
kyle-no-MacBook:gitprac5 kyle$ git mv hoge.txt hige.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: hoge.txt -> hige.txt # kyle-no-MacBook:gitprac5 kyle$
- Another identical operation:
kyle-no-MacBook:gitprac5 kyle$ mv hoge.txt hige.txt kyle-no-MacBook:gitprac5 kyle$ git rm hoge.txt rm 'hoge.txt' kyle-no-MacBook:gitprac5 kyle$ git add hige.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: hoge.txt -> hige.txt # kyle-no-MacBook:gitprac5 kyle$
Tags: git
Showing diffs
TweetPosted on Sunday Mar 09, 2014 at 09:06AM in Technology
Assume this:
kyle-no-MacBook:gitprac5 kyle$ ls -l total 8 -rw-r--r--+ 1 kyle staff 5 3 9 09:04 hoge.txt kyle-no-MacBook:gitprac5 kyle$ cat hoge.txt hoge kyle-no-MacBook:gitprac5 kyle$ git status # On branch master nothing to commit, working directory clean kyle-no-MacBook:gitprac5 kyle$
Show unstaged changes
kyle-no-MacBook:gitprac5 kyle$ echo hogehoge >> hoge.txt kyle-no-MacBook:gitprac5 kyle$ git diff diff --git a/hoge.txt b/hoge.txt index 2262de0..1904c09 100644 --- a/hoge.txt +++ b/hoge.txt @@ -1 +1,2 @@ hoge +hogehoge kyle-no-MacBook:gitprac5 kyle$
Show staged changes
kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt kyle-no-MacBook:gitprac5 kyle$ git diff --cached diff --git a/hoge.txt b/hoge.txt index 2262de0..1904c09 100644 --- a/hoge.txt +++ b/hoge.txt @@ -1 +1,2 @@ hoge +hogehoge kyle-no-MacBook:gitprac5 kyle$
- There are no unstaged changes now so result of git diff is empty
kyle-no-MacBook:gitprac5 kyle$ git diff kyle-no-MacBook:gitprac5 kyle$
Show both of unstaged and staged changes
Edit hoge.txt again
kyle-no-MacBook:gitprac5 kyle$ echo hogehogehoge >> hoge.txt
Show unstaged changes
kyle-no-MacBook:gitprac5 kyle$ git diff diff --git a/hoge.txt b/hoge.txt index 1904c09..9d1d67e 100644 --- a/hoge.txt +++ b/hoge.txt @@ -1,2 +1,3 @@ hoge hogehoge +hogehogehoge
Show staged changes
kyle-no-MacBook:gitprac5 kyle$ git diff --cached diff --git a/hoge.txt b/hoge.txt index 2262de0..1904c09 100644 --- a/hoge.txt +++ b/hoge.txt @@ -1 +1,2 @@ hoge +hogehoge
- This means that an another copy is created when we execute git add.
Show diff between 2 branches
kyle-no-MacBook:hello kyle$ git diff b4 origin/b4 diff --git a/README.md b/README.md index b8630fa..9c70835 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ b4-local1 -CANCEL kyle-no-MacBook:hello kyle$
Tags: git
Statuses of resources
TweetPosted on Sunday Mar 09, 2014 at 08:28AM in Technology
Statuses
- untracked
- unmodified
- modified
- staged
untracked
- Newly created file what not connected to the any part of git.
- We use git add to connect these file to git.
unmodified
- Files connected to git but no changes were made yet.
modified
- Files connected to git and modified.
- When we do commit with this kind of resources only, no changes will be made.
staged
- Files connected to git and modified, and declared to commit next time.
Examples of an newly created file
Create a repository
kyle-no-MacBook:gitprac5 kyle$ git init Initialized empty Git repository in /Users/kyle/tmp/gitprac5/.git/ kyle-no-MacBook:gitprac5 kyle$
Create a file
kyle-no-MacBook:gitprac5 kyle$ echo hoge > hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # hoge.txt nothing added to commit but untracked files present (use "git add" to track) kyle-no-MacBook:gitprac5 kyle$
- Now hoge.txt is untracked.
Add to Index
kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: hoge.txt # kyle-no-MacBook:gitprac5 kyle$
- Now README is staged (?).
Commit
kyle-no-MacBook:gitprac5 kyle$ git commit -m 'initial' [master (root-commit) ffbe516] initial 1 file changed, 1 insertion(+) create mode 100644 hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master nothing to commit, working directory clean kyle-no-MacBook:gitprac5 kyle$
- Now README is unmodified.
Cancel git add to new file
- We can cancel it like this:
kyle-no-MacBook:gitprac5 kyle$ echo README! > README kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # README nothing added to commit but untracked files present (use "git add" to track) kyle-no-MacBook:gitprac5 kyle$ git add README kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: README # kyle-no-MacBook:gitprac5 kyle$ git reset HEAD README kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # README nothing added to commit but untracked files present (use "git add" to track) kyle-no-MacBook:gitprac5 kyle$
- Also this deletes all of untracked files:
kyle-no-MacBook:gitprac5 kyle$ git clean -f Removing README kyle-no-MacBook:gitprac5 kyle$ ls -l total 8 -rw-r--r--+ 1 kyle staff 5 3 9 08:40 hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master nothing to commit, working directory clean kyle-no-MacBook:gitprac5 kyle$
Examples of existing file that already tracked by git
kyle-no-MacBook:gitprac5 kyle$ echo hogehoge >> hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # 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: hoge.txt # no changes added to commit (use "git add" and/or "git commit -a") kyle-no-MacBook:gitprac5 kyle$
- Now hoge.txt modified but not staged.
kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: hoge.txt # kyle-no-MacBook:gitprac5 kyle$
- Now hoge.txt staged.
- Let's take one more modify.
kyle-no-MacBook:gitprac5 kyle$ echo hogehogehoge >> hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: hoge.txt # # 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: hoge.txt # kyle-no-MacBook:gitprac5 kyle$
- This means last change (hogehogehoge) will not send by next commit.
- Next commit will send the change (hogehoge) only.
- So when you want to commit (hogehogehoge) next time, you need to execute git add once again.
kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt kyle-no-MacBook:gitprac5 kyle$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: hoge.txt # kyle-no-MacBook:gitprac5 kyle$
References
Tags: git