If you aren't familiar how to restore a branch, please take a look at git reset
and git reflog
first.
If you have any problems during the procedure, let me know as well.
# Your working directory has to be clean
1. $ git checkout <YOUR-BRANCH>
2. $ git reset --soft $(git merge-base @ upstream/develop) #Adapt base if necessary
# Sync the Android Studio project with gradle.
# Right click on the packages you want to format and click 'Reformat Code'.
# Normally org.catrobat.catroid, org.catrobat.catroid(androidTest).
# Clicking on java, another subfolder or the project itself will NOT work properly.
# Enable 'Include subdirectories', 'Optimize Imports', 'Only VCS changed text' and
# click 'Run'. For an optimal result, run the formatter twice over all packages.
# Hint: If 'Only VCS changed text' is greyed out,
don't run the formatter for the selected package.
3. git commit -c ORIG_HEAD
or use an alias (https://gist.github.com/robertpainsi/cb81b13b1008f6cf31f8#gistcomment-1589707)
-
If you have a single commit, you can use
git reset --soft @~
as the second command instead. -
If you have a few commits and no merge commits, try to format each commit by interactive rebasing. At each rebase stop, skip step 1 and use
git reset --soft @~
as the second command. However, there can be nasty merge-conflicts during the rebase process. Consider to performIf you have many commits or merge commits
instead. -
If you have many commits or merge commits, create a new commit with the formatted code. Change the commit message accordingly.
Note: Never format the whole project. Always format your changes only.
git reset --soft $(git merge-base @ upstream/develop)
will set your branch to the latest commit of upstream/develop which you share with your branch but will keep all the changes of your branch in the staged area. So by formatting 'Only VCS changed text', you'll format all new changes in your branch only.
And for people lazy like me there is an alias:
.gitconfig
format-code = "!f() { git checkout $1 && git reset --soft $(git merge-base @ $2) && echo 'Sync your project with gradle and format the code. Do not add the formatted files to the staging area! Press enter to continue...' && read 'foo' && git reset $(git rev-list @{1} -1); }; f"
Usage:
Example:
git format-code @ @~
and amend the changes.git format-code @ @~
for each rebase stop.