Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Undo local changes; git add . + git rm?

Need help figuring out a couple common workflows with Github. I come from a VS TFS background, so forgive me.

Undoing Pending Changes

Let's say I have cloned of a git repository to my local file system. At this point, the project's local files match exactly what's in the remote repoistory.

Then I decided to make some changes to the code, and change the local versions of a couple files. After doing some testing, I figure out that I want to discard my local changes and revert the local files back to what they are in the remote repoistory.

How do I undo these local changes, restoring them to the current versions in the repository?

Committing all Changes

Whenever I modify the contents of local files in my repository clone, or add new files, and want to push the changes, I issue "git add .", "git commit" with my comments, then "git push" to my master.

However, when I delete a file locally that's tracked in the repository, "git add ." doesn't capture the rm changes. Instead, I have to "git rm [filename]" before I "git commit" to update the repository. I always forget to do this though.

Is there a git command that will "git add ." and "git rm" any files I've deleted locally, in one step? After modifying local files and deleting a couple, I'd like to issue just one command that captures all my changes before I "git commit".

like image 657
user979672 Avatar asked Oct 14 '11 16:10

user979672


People also ask

What is git add rm?

git rm is used to remove a file from a Git repository. It is a convenience method that combines the effect of the default shell rm command with git add .

How do I get my local changes back in git?

If you have committed changes to a file (i.e. you have run both git add and git commit ), and want to undo those changes, then you can use git reset HEAD~ to undo your commit.

How do I undo a change in git add?

To undo git add before a commit, run git reset <file> or git reset to unstage all changes.

How do you undo git add but keep changes?

git reset -- FILE_NAME will do it. Note this works even if the file was untracked prior to adding, and you want to unstage (un-add) it but preserve it (keep the changes), and make it untracked again.


2 Answers

How do I undo these local changes, restoring them to the current versions in the repository?`

git reset --hard

(this will reset your index and working directory to HEAD)

Is there a git command that will git add . and git rm any files I've deleted locally, in one step?`

git add -u

(it wont add new files)

If you want to add new files, remove rm'ed files, and stage modifications to files:

git add -A
like image 112
manojlds Avatar answered Sep 21 '22 16:09

manojlds


You can revert to your last valid commit (i.e. copy of the remote repo, in tour example) by issuing:

git reset --hard HEAD

Additionally, I suggest reading the useful http://progit.org/2011/07/11/reset.html page to better understand how reset work and what the index is, and why git add and git rm are separate commands.

like image 21
p4010 Avatar answered Sep 24 '22 16:09

p4010