There are a group of commands that allow you undo:
- rm -rf {filename}
- git reset HEAD
- git reset HEAD {filename}
- git reset --hard HEAD
- git reset --hard origin/{branchname}
- git checkout -f
- git revert HEAD
First of all untracked files should not be the git's concern, unless you use rm {filename} to remove them from your disk, any commands start with git (except git clean) should not touch them.
git reset HEAD
this command put the files in the staging area back to the untracked state.
git checkout -f
this command will replace the local files with the remote version. Of course, your untracked files will remain untouched. It is more destructive than git reset HEAD, since the files in staging area are wiped out from disk, they are not put back into untracked state.
notice Neither "git checkout -f", "git reset HEAD" nor "git reset --hard HEAD" will touch the committed files.
In order to undo commit, we need to issue command "git reset --hard orign/{branchname}". This command is destructive -- it won't put the committed files back to untracked the state, it wipes them away from the disk.
Another way to undo commit is to use
git revert HEAD
it added a new commit to undo the local commits so that the end result is the same of HEAD version. It is a safer way of undo commit since it kept a copy of you committed files in history.
exmple
git reset HEAD
this command put the files in the staging area back to the untracked state.
git checkout -f
this command will replace the local files with the remote version. Of course, your untracked files will remain untouched. It is more destructive than git reset HEAD, since the files in staging area are wiped out from disk, they are not put back into untracked state.
notice Neither "git checkout -f", "git reset HEAD" nor "git reset --hard HEAD" will touch the committed files.
In order to undo commit, we need to issue command "git reset --hard orign/{branchname}". This command is destructive -- it won't put the committed files back to untracked the state, it wipes them away from the disk.
Another way to undo commit is to use
git revert HEAD
it added a new commit to undo the local commits so that the end result is the same of HEAD version. It is a safer way of undo commit since it kept a copy of you committed files in history.
exmple
============
GIT>git clone https://github.com/tekgadg/Pong_sept_20_2D.git demorepo
Cloning into 'demorepo'...
remote: Enumerating objects: 6, done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 6
Unpacking objects: 100% (6/6), done.
Checking connectivity... done.
GIT>cd demorepo/
GIT>ls
AndroidManifest.xml code
Pong_sept_20_2D.pde sketch.properties
GIT>echo a > a.txt
GIT>echo b > b.txt
GIT>echo c > c.txt
GIT>mkdir test
GIT>echo d > test/d.txt
GIT>echo e > test/e.txt
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git add --all
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
new file: b.txt
new file: c.txt
new file: test/d.txt
new file: test/e.txt
GIT>git reset HEAD test/
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
new file: b.txt
new file: c.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test/
GIT>git checkout -f
Your branch is up-to-date with 'origin/master'.
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>ls
AndroidManifest.xml code test
Pong_sept_20_2D.pde sketch.properties
GIT>echo a > a.txt
GIT>echo b > b.txt
GIT>echo c > c.txt
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git add -u .
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git add .
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
new file: b.txt
new file: c.txt
new file: test/d.txt
new file: test/e.txt
GIT>git reset HEAD c.txt test
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
new file: b.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
GIT>git commit -m "step 1 commit 2 files"
[master 6ff2dba] step 1 commit 2 files
Committer: Home Network <homenetwork@OK-MBP.home>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
2 files changed, 2 insertions(+)
create mode 100644 a.txt
create mode 100644 b.txt
GIT>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git log
commit 6ff2dba507fd244988282078e349f34807ca9bb9
Author: Home Network <homenetwork@OK-MBP.home>
Date: Tue Feb 5 08:14:49 2019 -0500
step 1 commit 2 files
commit 40aac7d161dc232299f81ed7112b88986b5fbe39
Author: SeanShin <seanshin@RetOne.local>
Date: Sun Jun 14 16:19:04 2015 -0700
upload
GIT>git reset HEAD
GIT>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git log
commit 6ff2dba507fd244988282078e349f34807ca9bb9
Author: Home Network <homenetwork@OK-MBP.home>
Date: Tue Feb 5 08:14:49 2019 -0500
step 1 commit 2 files
commit 40aac7d161dc232299f81ed7112b88986b5fbe39
Author: SeanShin <seanshin@RetOne.local>
Date: Sun Jun 14 16:19:04 2015 -0700
upload
GIT>git reset --hard HEAD
HEAD is now at 6ff2dba step 1 commit 2 files
GIT>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git log
commit 6ff2dba507fd244988282078e349f34807ca9bb9
Author: Home Network <homenetwork@OK-MBP.home>
Date: Tue Feb 5 08:14:49 2019 -0500
step 1 commit 2 files
commit 40aac7d161dc232299f81ed7112b88986b5fbe39
Author: SeanShin <seanshin@RetOne.local>
Date: Sun Jun 14 16:19:04 2015 -0700
upload
GIT>git reset --hard origin/master
HEAD is now at 40aac7d upload
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git log
commit 40aac7d161dc232299f81ed7112b88986b5fbe39
Author: SeanShin <seanshin@RetOne.local>
Date: Sun Jun 14 16:19:04 2015 -0700
upload
GIT>ls
AndroidManifest.xml c.txt sketch.properties
Pong_sept_20_2D.pde code test
GIT>echo a > a.txt
GIT>echo b > b.txt
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git add test/
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test/d.txt
new file: test/e.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
GIT>git checkout -f
Your branch is up-to-date with 'origin/master'.
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
nothing added to commit but untracked files present (use "git add" to track)
GIT>ls
AndroidManifest.xml b.txt sketch.properties
Pong_sept_20_2D.pde c.txt
a.txt code
GIT>git add c.txt
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: c.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
GIT>git clone https://github.com/tekgadg/Pong_sept_20_2D.git demorepo
Cloning into 'demorepo'...
remote: Enumerating objects: 6, done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 6
Unpacking objects: 100% (6/6), done.
Checking connectivity... done.
GIT>cd demorepo/
GIT>ls
AndroidManifest.xml code
Pong_sept_20_2D.pde sketch.properties
GIT>echo a > a.txt
GIT>echo b > b.txt
GIT>echo c > c.txt
GIT>mkdir test
GIT>echo d > test/d.txt
GIT>echo e > test/e.txt
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git add --all
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
new file: b.txt
new file: c.txt
new file: test/d.txt
new file: test/e.txt
GIT>git reset HEAD test/
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
new file: b.txt
new file: c.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test/
GIT>git checkout -f
Your branch is up-to-date with 'origin/master'.
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>ls
AndroidManifest.xml code test
Pong_sept_20_2D.pde sketch.properties
GIT>echo a > a.txt
GIT>echo b > b.txt
GIT>echo c > c.txt
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git add -u .
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git add .
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
new file: b.txt
new file: c.txt
new file: test/d.txt
new file: test/e.txt
GIT>git reset HEAD c.txt test
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
new file: b.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
GIT>git commit -m "step 1 commit 2 files"
[master 6ff2dba] step 1 commit 2 files
Committer: Home Network <homenetwork@OK-MBP.home>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
2 files changed, 2 insertions(+)
create mode 100644 a.txt
create mode 100644 b.txt
GIT>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git log
commit 6ff2dba507fd244988282078e349f34807ca9bb9
Author: Home Network <homenetwork@OK-MBP.home>
Date: Tue Feb 5 08:14:49 2019 -0500
step 1 commit 2 files
commit 40aac7d161dc232299f81ed7112b88986b5fbe39
Author: SeanShin <seanshin@RetOne.local>
Date: Sun Jun 14 16:19:04 2015 -0700
upload
GIT>git reset HEAD
GIT>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git log
commit 6ff2dba507fd244988282078e349f34807ca9bb9
Author: Home Network <homenetwork@OK-MBP.home>
Date: Tue Feb 5 08:14:49 2019 -0500
step 1 commit 2 files
commit 40aac7d161dc232299f81ed7112b88986b5fbe39
Author: SeanShin <seanshin@RetOne.local>
Date: Sun Jun 14 16:19:04 2015 -0700
upload
GIT>git reset --hard HEAD
HEAD is now at 6ff2dba step 1 commit 2 files
GIT>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git log
commit 6ff2dba507fd244988282078e349f34807ca9bb9
Author: Home Network <homenetwork@OK-MBP.home>
Date: Tue Feb 5 08:14:49 2019 -0500
step 1 commit 2 files
commit 40aac7d161dc232299f81ed7112b88986b5fbe39
Author: SeanShin <seanshin@RetOne.local>
Date: Sun Jun 14 16:19:04 2015 -0700
upload
GIT>git reset --hard origin/master
HEAD is now at 40aac7d upload
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git log
commit 40aac7d161dc232299f81ed7112b88986b5fbe39
Author: SeanShin <seanshin@RetOne.local>
Date: Sun Jun 14 16:19:04 2015 -0700
upload
GIT>ls
AndroidManifest.xml c.txt sketch.properties
Pong_sept_20_2D.pde code test
GIT>echo a > a.txt
GIT>echo b > b.txt
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
test/
nothing added to commit but untracked files present (use "git add" to track)
GIT>git add test/
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test/d.txt
new file: test/e.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
GIT>git checkout -f
Your branch is up-to-date with 'origin/master'.
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
c.txt
nothing added to commit but untracked files present (use "git add" to track)
GIT>ls
AndroidManifest.xml b.txt sketch.properties
Pong_sept_20_2D.pde c.txt
a.txt code
GIT>git add c.txt
GIT>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: c.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
No comments:
Post a Comment