Say I have two files "version 1.py" and "version 3.py". I want to have a version in between "version 2.py" because for example, I made two changes (e.g. feature A and bugfix 1) and I don't want to commit both changes simultaneously, as that isn't considered good practice. What I did is, I had the diff open on one side of the screen and manually copied the changes I want into the Pycharm-IDE. So what is the workflow in this case? There should be a tool to do that, shouldn't it? I didn't found one. I would take any tool I find and is advanced enough, I would however prefer a way to do that in Pycharm or Git. I found emacs with patch files, however emacs doesn't take the original file so it don't know when you're trying to change the source file. That resulted in me corrupting the patch and then the patch not being accepted by git.
If you made multiple changes to a file and you want to separate them into multiple commits, use git add -p
. "p" for "patch". This will walk you through your changes one-by-one and ask if you want to stage them. Stage the ones you want.
git diff
will show you your remaining unstaged changes.
git diff --staged
will show you the changes you've staged.
If you want to test just your staged changes you can stash your unstaged changes and retrieve them later.
Once you're sure of what you've staged, commit it. git commit
, not git commit -a
.
You can use PyCharm to do all of this. You can Commit part of a file, equivalent to git commit -p
. But if you do that you can't test what you're committing.
For full flexibility, see "Use the Git staging area to commit changes" on how to enable the staging area in PyCharm and how to use their commit tool to stage partial changes. Equivalent to git add -p
.
And you can use PyCharm to stage your unstaged changes. PyCharm calls this "shelving". See Use Git to work on several features simultaneously.
This works because in Git you build up what you want to commit in the "staging area". This is what git add
does, it copies files to the staging area. When you git commit
you commit what is in the staging area, not what is on disk.
For most tasks you can just git add .
to copy everything to the staging area, or git commit -a
to copy everything and commit. But you can also select what you copy. You can only stage certain files with git add <filename>
. And you can only stage certain changes with git add -p
.