Как удалить файлы из коммита idea
After you've added new files to the Git repository, or modified files that are already under Git version control and you are happy with their current state, you can share the results of your work. This involves committing them locally to record the snapshot of your repository to the project history, and then pushing them to the remote repository so that they become available to others.
Set your Git username
Git needs to know your username to associate commits with an identity. If you have not set your username, IntelliJ IDEA will prompt you to specify it when you first attempt to commit changes.
Open the Terminal and execute one of the following commands:
To set a name for every Git repository on your machine, use $ git config --global user.name "John Smith"
To set a name for a single repository, use $ git config user.name "John Smith"
Commit changes locally
Open the vertical Commit tool window Alt+0 located on the left:
As your changes are ready to be committed, select the corresponding files or an entire changelist.
If you press Ctrl+K , the entire active changelist will be selected.
You can also select files under the Unversioned Files node — IntelliJ IDEA will stage and commit these files in one step.
If you want to append local changes to the latest commit instead of creating a separate commit, select the Amend option.
Enter the commit message. You can click to choose from the list of recent commit messages.
You can also edit the commit message later before you've pushed the commit.
You can customize commit message rules on the Version Control | Commit page of the IDE settings Ctrl+Alt+S . There is also a quick-fix and the Reformat action that wrap a long line or reformat the message.
You can also define a commit template that will be used as the default commit message. Specify the boilerplate text you want to use in a .txt file and execute the following command in the terminal to add it to your Git config: git config --local commit.template
If you need to perform pre-commit checks, upload files to a server after the commit, or commit with advanced options, click :
The following options are available:
Author : if you are committing changes made by another person, you can specify the author of these changes.
Sign-off commit : select if you want to sign off your commit to certify that the changes you are about to check in have been made by you, or that you take the responsibility for the code you're committing.
When this option is enabled, the following line is automatically added at the end of the commit message: Signed off by:
In the Before Commit area, select the actions you want IntelliJ IDEA to perform before committing the selected files to the local repository.
The following options are available:
Reformat code : perform code formatting according to the Project Code Style settings.
Rearrange code : rearrange your code according to the arrangement rules preferences.
Optimize imports : remove redundant import statements .
Analyze code : analyze modified files before committing them. Click Choose profile to select an inspection profile from which the IDE will run inspections.
Check TODO () : Review the TODO items matching the specified filter. Click Configure to choose an existing TODO filter, or open the TODO settings page and define a new filter to be applied.
Cleanup : batch-apply quick-fixes from code cleanup inspections. Click Choose profile to select a profile from which the IDE will run inspections.
Run Tests : run tests as pre-commit checks. Click Choose configuration near Run Tests and select which configuration you want to run.
Update copyright : add or update a copyright notice according to the selected copyright profile - scope combination.
In the After Commit area, you can select the server access configuration or a server group to use for uploading the committed files to a local or remote host, a mounted disk, or a directory. See Deploy your application for details.
The following options are available:
Run tool : select the external tool that you want IntelliJ IDEA to launch after the selected changes have been committed. You can select a tool from the list, or click the Browse button and configure an external tool in the External Tools dialog that opens.
Upload files to : select the server access configuration or a server group to use for uploading the committed files to a local or remote host, a mounted disk, or a directory.
To suppress uploading, choose None .
To add a server configuration to the list, click and fill in the required fields in the Add Server dialog that opens.
The list is only available if the FTP/SFTP/WebDAV Connectivity plugin is enabled.
Always use selected server or group of servers : always upload files to the selected server or a server group.
The checkbox is only available if the FTP/SFTP/WebDAV Connectivity plugin is enabled.
When you're ready, click Commit or Commit and Push ( Ctrl+Alt+K ) to push the changes to the remote repository immediately after the commit. You will be able to review the current commit as well as all other commits before they are pushed to the remote.
Commit part of a file
Sometimes when you make changes that are related to a specific task, you also apply other unrelated code modifications that affect the same file. Including all such changes into one commit may not be a good option, since it would be more difficult to review, revert, cherry-pick them, and so on.
IntelliJ IDEA lets you commit such changes separately in one of the following ways:
select modified code chunks, that you want to include in a commit right in the Commit Changes dialog and leave other changes pending so that you can commit them later.
put different code chunks into different changelists on the fly, when you edit code, and then commit these changelists separately.
You can also create a new changelist and make it active, then all changes that you make after that will fall into that changelist, while any modifications you made before that will stay where they are.
Select chunks you want to commit
Open the vertical Commit tool window Alt+0 .
To display the differences between the repository version and the local version of the selected file, in the Commit tool window Alt+0 , click on the toolbar or press Ctrl+D .
Select the checkbox next to each chunk of modified or newly added code that you want to commit, and leave other changes unselected:
You can also select Move to Another Changelist from the context menu of a modified chunk to split changes between different changelists that you can commit separately.
To assign a custom shortcut for this action, look for the Move Lines to Another Changelist action under Version Control Systems on the Keymap page of the IDE settings Ctrl+Alt+S .
Click Commit . Unselected changes will stay in the current changelist, so that you can commit them separately.
Put changes into different changelists
When you make a change to a file in the editor, click the corresponding change marker in the gutter.
If there are no change markers in the gutter, make sure the Highlight modified lines in the gutter option is enabled on the Editor | General page of the IDE settings Ctrl+Alt+S .
In the toolbar that appears, select the target changelist for the modified code chunk (or create a new changelist):
Commit each changelist separately.
Use the Git staging area to commit changes
If you are more used to the concept of staging changes for commit instead of using changelists where modified files are staged automatically, select the Enable staging area option on the Version Control | Git page of the IDE settings Ctrl+Alt+S .
The Commit tool window will now look as follows:
Using the staging area allows you to easily commit changes to the same file separately (including overlapping changes), and see which changes are already staged without switching focus from the editor.
When you switch from using changelists to Git staging area, all existing changelists will be deleted, so make sure you've committed or shelved them to prevent losing data.
Stage changes for commit
Do one of the following:
To stage an entire file, in the Commit tool window Alt+0 , select this file and click on the right next to it or press Ctrl+Alt+A .
To stage a specific chunk inside a file, in the editor click the change marker in the gutter next to the modified chunk and click Stage .
Staged changes (including changes staged from outside IntelliJ IDEA) are marked with a border-shaped change marker in the editor:
To stage granular changes like a single line instead of a code chunk, or even one of a number of changes to a single line, in the Commit tool window Alt+0 , select the file containing the change and choose Compare HEAD, Staged and Local Versions from the context menu.
This will open a three-way diff viewer where the left pane shows the repository version, the right pane shows the local version, and the central pane is a fully-functional editor where you can make the changes you want to stage.
When ready, commit the changes as described in Commit changes locally.
Push changes to a remote repository
Before pushing your changes, sync with the remote and make sure your local copy of the repository is up-to-date to avoid conflicts.
IntelliJ IDEA allows you to upload changes from any branch to its tracked branch or to any other remote branch.
Do one of the following:
To push changes from the current branch press Ctrl+Shift+K or choose Git | Push from the main menu.
To push changes from any local branch that has a remote, select this branch in the Branches popup and choose Push from the list of actions.
The Push Commits dialog opens showing all Git repositories (for multi-repository projects) and listing all commits made in the current branch in each repository since the last push.
If you have a project that uses multiple repositories that are not controlled synchronously, only the current repository is selected by default (for details on how to enable synchronous repositories control, refer to Version Control Settings: Git).
You can press Ctrl+Q for the selected commit to display extra info, such as the commit author, time, hash and the commit message.
If there are no remotes in the repository, the Define remote link appears. Click this link and specify the remote name and URL in the dialog that opens. It will be saved and you can edit it later via Git | Manage Remotes (for details, see Add a remote repository).
If you want to modify the target branch where you want to push, you can click the branch name. The label turns into a text field where you can type an existing branch name, or create a new branch. You can also click the Edit all targets link in the bottom-right corner to edit all branch names simultaneously.
Note that you cannot change the local branch: the current branch for each selected repository will be pushed.
You can also switch to the editing mode by pressing Enter or F2 for the selected element.
If you have some commits you've made but not yet want to push to a remote branch, in the Log tab of the Git tool window select the last commit you want to push and choose Push All up to Here… option from the list of actions.
The Push Commits dialog opens showing all commits up to the selected commit hash.
If you want to preview changes before pushing them, select the required commit. The right-hand pane shows the changes included in the selected commit. You can use the toolbar buttons to examine the commit details.
If the author of a commit is different from the current user, this commit is marked with an asterisk.
If you select an entire repository, all files from all commits will be listed in the right pane.
If the same file was modified within several commits, it will only be listed once if you select these commits or the entire repository, and if you invoke the Differences Viewer for this file, all changes will be zipped together.
Click the Push button when ready and select which operation you want to perform from the drop-down menu: Push or Force push (equivalent to push --force-with-lease ).
These choice options are only available if the current branch is not listed in the Protected branches field (see Version Control Settings: Git), otherwise, you can only perform the push operation.
Update your working copy if push is rejected
If push is rejected because your working copy is outdated, IntelliJ IDEA displays the Push Rejected dialog, provided that the Auto-update if push of the current branch was rejected option in the Git settings page of the Settings/Preferences dialog is not selected. Do the following:
If your project uses several Git repositories, specify which of them you want to update. If you want to update all repositories, no matter whether push was rejected for them or not, select the Update all repositories option. If this option is cleared, only the affected repositories will be updated.
If you want IntelliJ IDEA to apply the update procedure silently the next time push is rejected using the update method you choose in this dialog, select the Remember the update method choice and silently update in the future option.
After you leave this dialog, the Auto-update if push of the current branch was rejected checkbox in the Git settings page of the Settings/Preferences dialog will be selected, and the applied update method will become the default one.
To change the update strategy, deselect this option to invoke the Push Rejected dialog the next time push of the current branch is rejected, apply a different update procedure, and select the Remember the update method choice option once again.
Select the update method (rebase or merge) by clicking the Rebase or Merge button respectively.
When do I need to use force push?
When you run push , Git will refuse to complete the operation if the remote repository has changes that you are missing and that you are going to overwrite with your local copy of the repository. Normally, you need to perform pull to synchronize with the remote before you update it with your changes.
The --force push command disables this check and lets you overwrite the remote repository, thus erasing its history and causing data loss. Under the hood, when you choose to force push, IntelliJ IDEA performs the push --force-with-lease operation which is a safer option that helps you ensure you do not overwrite someone else's commits (see git push for more details on the push options).
A possible situation when you may still need to perform --force push is when you rebase a pushed branch and then want to push it to the remote server. In this case, when you try to push, Git will reject your changes because the remote ref is not an ancestor of the local ref. If you perform pull in this situation, you will end up with two copies of the branch which you then need to merge.
VCS has an Add option (Git Add) but seems to lack Git Remove.
What's the idiomatic way to Git Remove with VCS?
7 Answers 7
In the terminal, use git rm --cached -r .idea/ . This will remove the files from the GIT index, but leave the files locally.
perfect! I used git rm --cached -r ./ in home directory of project for total delete from git and add them just as I wanted. Thanks mate! :)
This should be the selected answer as it doesn't delete the file completely, just unstages it from GIT. Thanks!
To change a file from green (staged) to red (untracked) using Intellij:
- Right click the file(s)
- Hover over Git (the git pane will expand)
- Click Rollback. (in older versions Revert )
- Check that Delete local copies of added files is not checked and click the Rollback button
This will not delete the file, only unstage it (remove it from git's index).
@AksharPatel That is not true. If the file is green (added using git add
@Jyapx Ah. That makes sense. By changes, I did mean the blue ones and that is why I posted a comment rather than editing the answer.
Given your project is linked to a git repo already, you can just use the standard IntelliJ feature to "delete" the file.
In the project pane, focus the file and hit Delete key. The "Safe Delete" popup will appear, click OK .
Now observe under 9: Version Control -> Local Changes that the file is listed there in "grey" - when you commit and push to your git repo, the file will be deleted on the current branch.
Edit: if these are IntelliJ files, this becomes more difficult.
First, close IntelliJ, make a list of the exact files you want to delete from repo, and take a copy of those files on your local file system.
Then use git rm to remove them and then commit.
Next step, add a .gitignore file to ignore local IntelliJ files. A good start is *.iml and .idea .
Later, I found that a file had mistakenly been added to the commit.
How can I remove a file from the last commit?
34 Answers 34
I think other answers here are wrong, because this is a question of moving the mistakenly committed files back to the staging area from the previous commit, without cancelling the changes done to them. This can be done like Paritosh Singh suggested:
Then reset the unwanted files in order to leave them out from the commit (the old way):
Note, that since Git 2.23.0 one can (the new way):
Now commit again, you can even re-use the same commit message:
Thanks for this. It's worth adding that if you have already pushed your earlier (wrong) commit, and now try to git push your fix up to your repo, it will complain Updates were rejected because the tip of your current branch is behind its remote counterpart. . If you're sure that you want to push them (e.g. it's your fork) then you could use the -f option to force the push, e.g. git push origin master -f . (Do not do this to an upstream repo that others are fetching from)
@PabloFernandez at the top of all of the answers are three tabs which allow you to control the ordering of answers: active, oldest and votes. My guess is yours is set to oldest. Switch it to votes even though the accepted answer will still be at the top, this answer will be second.
I knew this much about git reset but wanted a way to affect the existing commit "in place". I just learned about git commit -C . So for me, what I want is your exact recipe with one more step, the "new commit again" spelled out as git commit -C [hash of original HEAD commit from first step] .
Remember to do an actual commit, not an amend. If you do, it will be amending the previous commit. – oops
ATTENTION! If you only want to remove a file from your previous commit, and keep it on disk, read juzzlin's answer just above.
If this is your last commit and you want to completely delete the file from your local and the remote repository, you can:
- remove the file git rm
- commit with amend flag: git commit --amend
The amend flag tells git to commit again, but "merge" (not in the sense of merging two branches) this commit with the last commit.
As stated in the comments, using git rm here is like using the rm command itself!
Warning to those browsing this answer: make sure you want to DELETE the file (as in gone gone gone!), not just remove it from the Commit list.
To add to what others say (and to make it easier to remember not to do this unless you really want to): The rm in the git command is doing what rm itself does!
Note that the files can still be restored, in case you changed your mind, the commit before git commit --amend is still there and can be found for example with git reflog . So it is not as bad as the other comments suggest.
Existing answers are all talking about removing the unwanted files from the last commit.
If you want to remove unwanted files from an old commit (even pushed) and don't want to create a new commit, which is unnecessary, because of the action:
- Find the commit that you want the file to conform to using ;
git log --graph --decorate --oneline
you can do this multiple times if you want to remove many files.
git commit -am "remove unwanted files"
Find the commit_id of the commit on which the files were added mistakenly, let's say "35c23c2" here
git rebase 35c23c2~1 -i // notice: "~1" is necessary
This command opens the editor according to your settings. The default one is vim. If you want to change the global git editor, use;
git config --global core.editor
Move the last commit, which should be "remove unwanted files", to the next line of the incorrect commit("35c23c2" in our case), and set the command as fixup :
You should be good after saving the file.
If you unfortunately get conflicts, you have to solve them manually.
If you want to actually remove the files from the repo (not the filesystem), rather than merely revert them to a previous version, then instead of step 1 do git rm --cached .
This process could be made a bit easier by adding --fixup=35c23c2 to the git commit command. This will setup the commit automatically as a fixup of the required commit and so you won't need to specify it in the rebase. Additionally, if you add --autosquash to the git rebase command, git will automatically move your commit to the correct location, so you don't need to do anything in the interactive rebase - just save the result (which means you don't even need to -i flag, though I like to use it anyway to make sure everything looks as I expect).
As the accepted answer indicates, you can do this by resetting the entire commit. But this is a rather heavy handed approach.
A cleaner way to do this would be to keep the commit, and simply remove the changed files from it.
The git reset will take the file as it was in the previous commit, and stage it in the index. The file in the working directory is untouched.
The git commit will then commit and squash the index into the current commit.
This essentially takes the version of the file that was in the previous commit and adds it to the current commit. This results in no net change, and so the file is effectively removed from the commit.
@ThatsAMorais indeed :-). I'm wondering if this question got merged with another, and thats why I didn't see it. Or maybe I'm just blind. Either case, I think I'm inclined to leave it as based on votes it seems to be more popular (maybe people prefer the short and direct answer).
By all means leave it! :) We clearly had the right idea, and the purpose is to help. I think merging is a good theory.
Nice answer, but there is one gotcha you may need to watch out for: if you have any added or deleted files currently staged they will be added to the commit via the git commit --amend --no-edit command. Not sure why this is but it just bit me.
If you have not pushed the changes on the server you can use
It will reset all the changes and revert to one commit back
If you have pushed your changes then follow steps as answered by @CharlesB
not a big deal, but because the OP wants to remove unwanted file from last commit, and it just resets to the state before committing. still you have to redo the commit.
Removing the file using rm will delete it!
You're always adding to a commit in git rather than removing, so in this instance return the file to the state it was in prior to the first commit (this may be a delete 'rm' action if the file is new) and then re-commit and the file will go.
To return the file to some previous state:
or to return it to the state at the remote HEAD:
then amend the commit and you should find the file has disappeared from the list (and not deleted from your disk!)
I will explain to you with example.
Let A, B, C be 3 successive commits. Commit B contains a file that should not have been committed.
If the particular file is not in the last or previous commit, this is the most elegant way. I'd even say it's the most elegant way in general. I like interactive rebasing.
This is the best way to modify the last commit, the one that has not been pushed. This will reset changes in one file, effectively removing that file from the last commit.
The following will unstage just the file you intended, which is what the OP asked.
You'll see something like the following.
Changes to be committed: (use "git reset HEAD . " to unstage)
modified: /path/to/file
Changes not staged for commit: (use "git add . " to update what will be committed) (use "git checkout -- . " to discard changes in working directory)
modified: /path/to/file
- "Changes to be committed" is the previous version of the file before the commit. This will look like a deletion if the file never existed. If you commit this change, there will be a revision that reverts the change to the file in your branch.
- "Changes not staged for commit" is the change you committed, and the current state of the file
At this point, you can do whatever you like to the file, such as resetting to a different version.
When you're ready to commit:
or (if you've got some other changes going on that you don't want to commit, yet)
juzzlin's answer is great, but its excessive to unstage the entire commit when you only wish to unstage one. Unstaging the whole commit can cause problems if you have currently unstaged changes on files in that commit that you do not want to lose.
You can simply try.
and create a new commit.
However, there is an awesome software "gitkraken". which makes it easy to work with git.
And just to note: after this, you'd do git commit --amend to have the file removal updated in your last commit; and afterwards, you can check it has indeed been removed with git log -1 --stat
will leave you the local file still. If you don't want the file locally either, you can skip the --cached option.
If all work is on your local branch, you need to keep the file in a later commit, and like having a clean history, I think a simpler way to do this could be:
and you can then finish the rebase with ease without having to remember more complex commands or commit message or type as much.
This works for me. I assume if you want to add the files back into the mix, just use git add -A or git add . and they are back.
Using git GUI can simplify removing a file from the prior commit.
Assuming that this isn't a shared branch and you don't mind rewriting history, then run:
You can un-check the file that was mistakenly committed and then click "Commit".
The file is removed from the commit, but will be kept on disk. So if you un-checked the file after mistakenly adding it, it will show in your untracked files list (and if you un-checked the file after mistakenly modifying it it will show in your changes not staged for commit list).
Thank you! I was stuck with a problem (bug?) where a folder containing a .git repo was added, and all the regular remove commands didn't work. This however helped. It being a few commits back, I first utilized git rebase -i HEAD~4 and then ran your command to open the editor. Another note: "Unstaging" can be found in the "Commit" menu.
The easiest solution of all. Simplest to remember. And much less error-prone than using git reset --soft HEAD^ (remembering the --soft arg), followed by git commit -c ORIG_HEAD (rather than --amend, which screws everything up).
thanks, there were 3 binary objects in my last commit and push was taking forever. Your answer helped me to delete 3 objects.
If you want to preserve your commit (maybe you already spent some time writing a detailed commit message and don't want to lose it), and you only want to remove the file from the commit, but not from the repository entirely:
Just wanted to complement the top answer as I had to run an extra command:
Do a sequence of the following commands:
I've tried to do these steps, and I see this error error: failed to push some refs to 'git. ' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details. (after git pull I have the same changes)
It means that the state of your remote repo has changed while you were doing your local job. And after 'git pull' your local changes should be merged with remote ones, that's it. Of course, your changes must remain.
If you want to remove files from previous commits use filters
If you see this error:
Cannot create a new backup. A previous backup already exists in refs/original/ Force overwriting the backup with -f
Just remove refs backups on your local repo
Note that in case of existing backup, the error message clearly indicates the solution: add "-f" as in git filter-branch -f [. etc. ] . No need to manually remove the refs.
This will undo the last commit in local repos and move everything back to stage area like before doing the commit. Then just use any Git UI tool as normal (like TortoiseGit, Git UI, Git Extensions. ) to unstage the files that we do not want to commit then do the commit again.
Something that worked for me, but still think there should be a better solution:
Just leave the change you want to discard in the other commit, check others out
git reset --soft HEAD^ winds back your commit, and when you type git status , it tells you what to do:
Had the same issue where I have changes in a local branch where I wanted to revert just one file. What worked for me was -
(feature/target_branch below is where I have all my changes including those I wanted to undo for a specific file)
(origin/feature/target_branch is the remote branch where I want to push my changes to)
(feature/staging is my temporary staging branch where I will be pushing from all my wanted changes excluding the change to that one file)
Create a local branch from my origin/feature/target_branch - called it feature/staging
Merged my working local branch feature/target_branch to the feature/staging branch
Checked out feature/staging then git reset --soft ORIG_HEAD (Now all changes from the feature/staging' will be staged but uncommitted.)
Unstaged the file which I have previously checked in with unnecessary changes
Changed the upstream branch for feature/staging to origin/feature/target_branch
Committed the rest of the staged changes and pushed upstream to my remote origin/feature/target_branch
You can always undo the changes you've made locally before you commit them:
In the Commit tool window Alt+0 , select one or more files that you want to revert, and select Rollback from the context menu, or press Ctrl+Alt+Z . All changes made to the selected files since the last commit will be discarded, and they will disappear from the active changelist.
Unstage files
By default, IntelliJ IDEA uses the changelists concept where modified files are staged automatically.
If a file is already under version control and you do not want to commit it, you can:
Remove it from the commit: do not select it in the Changes area of the Commit tool window.
If you are more used to the staging concept, select the Enable staging area option in the Version Control | Git page of the IDE settings Ctrl+Alt+S .
Also, by default IntelliJ IDEA suggests adding each newly created file under version control. You can change this behavior in Settings/Preferences | Version Control | Confirmation using When files are created and When files are deleted settings respectively.
Undo the last commit
IntelliJ IDEA allows you to undo the last commit in the current branch.
You cannot undo a commit if it was pushed to a protected branch, that is a branch to which force --push is not allowed (configure protected branches in the Version Control | Git page of the IDE settings Ctrl+Alt+S ) Note that if a branch is marked as protected on GitHub, IntelliJ IDEA will automatically mark it as protected when you check it out.
Open the Git tool window Alt+9 and switch to the Log tab.
Select the last commit in the current branch and choose Undo Commit from the context menu.
In the dialog that opens, select a changelist where the changes you are going to discard will be moved. You can either select an existing changelist from the Name list, or specify the name of a new changelist (the commit message is used by default).
Select the Set active option if you want to make the changelist with the changes you are about to discard the active changelist.
Select the Track context option if you want IntelliJ IDEA to remember your context and reload currently opened files in the editor when this changelist becomes active.
Revert a pushed commit
If you notice an error in a specific commit that has already been pushed, you can revert that commit. This operation results in a new commit that reverses the effect of the commit you want to undo. Thus, project history is preserved, as the original commit remains intact.
Locate the commit you want to revert in the Log tab of the Git tool window Alt+9 , right-click it and select Revert Commit from the context menu. This option is also available from the context menu of a commit in the file History view. The Commit Changes dialog will open with an automatically generated commit message.
If you apply this action to multiple commits selected in the Log view, a separate commit will be created to revert each of them.
If the selected commit contains several files, and you only need to revert some of them, deselect the files you do not want to touch.
Click Commit to commit a changeset that reverts the changes to the selected files in this particular commit.
Revert selected changes
IntelliJ IDEA lets you undo selected changes from a pushed commit if this commit contains multiple files and you only need to revert some of them.
In the Log view select the commit containing the changes you want to discard.
In the Changed Files pane, right-click the file that you want to revert and select Revert Selected Changes from the context menu.
This results in a new commit that reverses the changes you want to undo.
Drop a commit
Unlike reverting a commit, which is reflected in the branch history, you can discard a pushed commit in the current branch without leaving any traces of the operation.
Like any operation that rewrites a branch history, dropping a commit requires a --force push and cannot be performed in protected branches (these can be configured in the Version Control | Git page of the IDE settings Ctrl+Alt+S .
Select a commit you want to discard in the Log view and choose Drop Commit from the context menu.
Reset a branch to a specific commit
If you notice an error in a set of recent commits and want to redo that part, you can roll back your repository to a specific state. This is done by resetting the current branch HEAD to a specified commit (and optionally resetting the index and working tree if you prefer not to reflect the undo in the history).
Open the Version Control tool window Alt+9 and switch to the Log tab.
Select the commit that you want to move HEAD onto and select Reset Current Branch to Here from the context menu.
In the Git Reset dialog that opens, select how you want your working tree and the index to be updated and click Reset :
Soft : all changes from commits that were made after the selected commit will be staged (that means they will be moved to the Local Changes view so that you can review them and commit later if necessary).
Mixed : changes made after the selected commit will be preserved but will not be staged for commit.
Hard : all changes made after the selected commit will be discarded (both staged and committed).
Keep : committed changes made after the selected commit will be discarded, but local changes will be kept intact.
Get a previous revision of a file
If you need to revert a single file instead of discarding a whole commit that includes changes to several files, you can return to a particular version of that file:
Select the required file in any view (in the Project tool window, in the editor, in the Local Changes view, and so on).
Select Git | Show History from the main VCS menu or from the context menu of the selection. The History tab is added to the Git tool window showing the history for the selected file and allowing you to review and compare its revisions.
When you've identified the revision you want to roll back to, select it in the list and choose Get from the context menu.
Use this dialog to commit (check in) local changes to the repository and, optionally, to create a patch file.
Starting from IntelliJ IDEA version 2020.1, you can switch to a non-modal commit interface: select the Use non-modal commit interface option (enabled by default for new installations) on the Version Control | Commit page of the IDE settings Ctrl+Alt+S .
The options available in this dialog depend on the version control system you are using.
Modified files pane
This section lists all files that have been modified since the last commit. Deselect the check-boxes next to the files that you want to exclude from the current commit.
Modified files pane toolbar
Tooltip and Shortcut
Open the Differences dialog that highlights the differences between your local working copy of the selected file and its repository version.
Revert all changes made to the local working copy of the selected files.
Click this button to reload the Changed files tree view so it is up-to-date.
Choose how you want to group the modified files - by directory or module
Expand or collapse all nodes
Click these buttons to expand or collapse all nodes in the directory tree. These buttons are not available in flat view.
From this list, select the changelist that contains the modified files to be checked in or included in the patch. The active changelist is selected by default.
The summary under the modified files pane shows statistics on the currently selected changelist, such as the number of modified, new, deleted and unversioned files. This area also shows how many files of each type are shown, and how many of them will be committed.
Commit Message pane
In this area, enter a comment to the current commit. You cannot commit your changes until you enter some description in the Commit Message field.
This comment will also be used as the name of the patch file, if you decide to create a patch.
You can click Commit Message history Ctrl+M to choose from the list of recent commit messages.
VCS-specific controls
The controls in this section are located in the top-right part of the dialog, and contain the options that are specific for the version control system you are using.
Use this list to select the author of the changes that you are going to commit. This may be useful when you are committing changes made by another person.
Select this checkbox to replace the previous commit with the current changes (see Git Basics: Undoing Things for details).
select if you want to sign off your commit to certify that the changes you are about to check in have been made by you, or that you take the responsibility for the code you're committing.
When this option is enabled, the following line is automatically added at the end of the commit message: Signed off by:
Select this checkbox to replace the previous commit with the current changes (see Git Basics: Undoing Things for details).
Keep files locked
Select this checkbox to keep the changed files locked after they are checked in.
These controls are available only if you select the Enable Perforce Jobs Support checkbox on the Perforce settings page.
Use the controls in this area to search for Perforce jobs, link jobs to the selected changelist, and detach the currently linked jobs.
Unlink selected jobs : click this button to detach the selected job from the changelist.
Edit associated jobs : click this button to open the Edit Jobs Linked to Changelist dialog where you can search for available jobs, view their details, and link jobs to the selected changelist.
Find and link job matching the pattern : click this button to start quick search for the job that matches the pattern specified in the field and attach the job to the changelist.
In the field, specify the exact name of the job or a search pattern according to the Perforce jobs syntax rules.
If only one job matching the pattern is found, it is attached to the changelist automatically. Otherwise, to select a job among several available jobs, click the button and find the desired job using the Edit Jobs Linked to Changelist dialog.
The list in the bottom of the area displays the jobs that are currently attached to the selected changelist.
Before Submit / Before Commit section
Use the controls in this area to define which additional actions you want IntelliJ IDEA to perform before committing the selected files.
Читайте также: