2

For example, I start from a clean commit, make some changes to different files, then I want to remove changes I made from only one file. How do I do it?
I tried to do:

git stash -p train.py

But I've got the error:

fatal: subcommand wasn't specified; 'push' can't be assumed due to unexpected token 'train.py'
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
pascalamin
  • 83
  • 1
  • 7
  • Do you wan't to remove the changes, or stash them? There is a big difference. – Andreas Louv Nov 18 '21 at 09:15
  • Just `git restore train.py` if the file you want to remove changes from is yet uncommitted. If the changes are in the last commit (which is unclear from your description) then go for Antonio's solution. – Romain Valeri Nov 18 '21 at 09:22
  • If you want to use `git stash`, the error has told you how to solve it. Use `git stash push -p train.py`. – ElpieKay Nov 18 '21 at 09:27

2 Answers2

1

The reason why you're getting that error is because git stash push (which git stash -p is an alias for), doesn't allow custom arguments in order to avoid unintended stashing.

From the documentation for git stash push:

For quickly making a snapshot, you can omit "push". In this mode, non-option arguments are not allowed to prevent a misspelled subcommand from making an unwanted stash entry. The two exceptions to this are stash -p which acts as alias for stash push -p and pathspec elements, which are allowed after a double hyphen -- for disambiguation.

In your case, train.py is a pathspec, so you must separate it from the recognized options by adding a -- like this:

git stash -p -- train.py

Alternatively, you can spell out the entire command, in which case you don't have to separate the path with a double dash:

git stash push -p train.py

Note that using a double dash -- to clearly separate a pathspec from a command's known options is a convention used by all Git commands that accept paths.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
0

You should:

git rebase HEAD~1

# Change, by the editor just opened, the word pick into edit, save and exit
# Edit you file

git commit
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74