Skip to content

Instantly share code, notes, and snippets.

@robertpainsi
Last active November 22, 2023 12:02
Show Gist options
  • Save robertpainsi/cb81b13b1008f6cf31f8 to your computer and use it in GitHub Desktop.
Save robertpainsi/cb81b13b1008f6cf31f8 to your computer and use it in GitHub Desktop.

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.

How to format your code?

   # 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 perform If 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.

@p4p4
Copy link

p4p4 commented Jun 23, 2016

just a suggestion: as 3rd step after formatting the last commit one might do

 git commit -c ORIG_HEAD

instead. This creates the commit with the commit message again.

@robertpainsi
Copy link
Author

@p4p4: Thanks for the hint, updated! 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment