Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discard changes in one single line

Tags:

git

I've noticed that in Tower (Git client for the Mac) the user can discard changes even line by line. I wonder how could this be done using the command line? or maybe is something special of Tower?

I frequently find myself in this case:

@@ -391,7 +392,7 @@ extern BOOL validateReceiptAtPath(NSString *path);         NSURL *url = [self fileURL];         if (url != nil) {                 NSRect readFrame = [self _readPreferenceOfFileAtURL:url]; -                +                 for (NSScreen * screen in [NSScreen screens]) {                         NSRect screenVisibleRect = [screen visibleFrame];                         ... 

See how I have one + and one - ? I would like to discard it so my commit has the minimum changes (hence less possibilities of conflicts and easier review)

:)

like image 334
nacho4d Avatar asked Jun 01 '11 08:06

nacho4d


People also ask

How do I discard a specific change in git?

Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes. Use Git reset --hard <commit id> to point the repo to a previous commit.

How do you discard all changes?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.

What does discard changes mean in VS code?

In this process, i found a feature 'discard all changes' I did not realized it means 'discard all files that not tracked. and my files are all gone. I found it after that 'git clean -f -q' command has activated.


2 Answers

This is called interactive staging and can be done using git add -i or git add -p. See the git-add manpage, pro git and the Git Community Book for more information.

EDIT:

To interactively unstage a file, you can use:

git checkout -p HEAD 

Also see this SO question: Undo part of unstaged changes in git

like image 78
igorw Avatar answered Oct 15 '22 22:10

igorw


To undo hunks use

git reset --patch 

It’s a very hidden feature. You can stage hunk by hunk with git add --interactive, but you can’t unstage this way. git add also features the option --patch which is like --interactive, but goes directly to the “patch” menu point (otherwise you have to hit pENTER).

git reset does not mirror the --interactive option, but has --patch.

like image 29
Robert Siemer Avatar answered Oct 15 '22 22:10

Robert Siemer