gangelo
gangelo

Reputation: 3182

How to fix "Trailing/Leading spaces" error in commits previous to HEAD?

Apologies up front, I'm an aspiring git guru, but not quite there yet...

I currently have 5 local commits that have not yet been pushed to my remote branch; the first 2 are prohibiting me from pushing because they have trailing/Leading spaces errors in some of the source files.

When I tried to push...

$ git push -u origin my-branch

I received the following error...

$ git push -u origin my-branch
Counting objects: 124, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (122/122), done.
Writing objects: 100% (124/124), 23.19 KiB | 3.31 MiB/s, done.
Total 124 (delta 94), reused 2 (delta 2)
remote: Resolving deltas: 100% (94/94), completed with 16 local objects.
remote: bin/pre_receive_hook: failed with exit status 1
remote: Starting pre_receive_hook.rb at 02:30:52.384
remote: Updating refs/heads/my-branch from 89e1e6deeec412a914c4cfc4c6dec101b15ba4c5 to 0cd270067efecff743505d6613c2cff07a4f7f14
remote: Running Conventions Check...
remote: Starting ConventionCheck at 02:30:53.162
remote: Checking Commit 0cd270067: OB-221 Remove pry bindings left in spec, remove leading/trailing spaces in files, add newline to end of files
remote: Checking Commit 1342336cd: OB-221 Remove pry bindings left in spec
remote: Checking Commit 7dc993531: OB-221 Refactored SoapRequest and CalculateInvoiceTaxRequest to accomodate a more traditional Adapter pattern
remote:     Trailing/Leading spaces error, use --check in your diff to see the problem: Conventions
To github.my-site.com:my-site/argh_express.git
 ! [remote rejected]         OB-221-taxware-api-adapter -> OB-221-taxware-api-adapter (pre-receive hook declined)
error: failed to push some refs to '[email protected]:on-site/argh_express.git'

I've narrowed the trailing/leading spaces errors to source files committed to first two (2) commits using

$ git diff --check -R 7dc993531 bbfd2723d

Which produced the following output (trailing whitespace represented by \b)

ROOT/rails/app/models/integration/tax_ware/calculate_invoice_tax_result_model.rb:100: trailing whitespace.
+\b\b\b\b\b\b\b
ROOT/rails/spec/models/integration/tax_ware/soap_request_spec.rb:17: trailing whitespace.
+\b\b
ROOT/rails/spec/models/integration/tax_ware/soap_request_spec.rb:18: trailing whitespace.
+  let(:soap_request) do\b

So the commits in error are the following (marked in bold)

I basically want to either

  1. fix those source files (which doesn't quite make sense to me because the files have changed with more recent comments) or
  2. somehow just push the most recent commit (0cd270067)

However, the most recent commit doesn't contain ALL the files that need to be pushed! How do I go about pushing the most recent local commit, include ALL the files I need, and discard all the previous local commits?

Upvotes: 0

Views: 709

Answers (2)

LightBender
LightBender

Reputation: 4253

In your situation, where you just want to have the current changes on a new commit and haven't previously pushed any of the commits, an interactive rebase is actually overkill. The easiest method is to perform a soft reset back to the previous version and make a new commit:

git reset --soft bbfd2723d^

This will move the head back before you made your commits, but leave your working directory and index unchanged. Then you can build your new commit(s) as you would normally.

For reference, this method is described in the git book chapter 7.7.

Upvotes: 1

ashmaroli
ashmaroli

Reputation: 5444

Since you want to discard older comments and push just the latest commit singularly, git rebase -I will help you.

# activate `git rebase` on the last 5 commits on your branch
git rebase -i HEAD~5

You now have the following choices (apart from pick and reword):

  • edit: make changes to the commit individually
  • squash: flatten the commit into the previous commit, but merge the two commit messages as a single commit message
  • fixup: flatten the commit into the previous but discard the current commit message

If you want to keep a record of the 5 commit messages, choose squash else fixup like in the example below:

pick  bbfd2723d
fixup 9ca4f853f
fixup 7dc993531
fixup 1342336cd
fixup 0cd270067
  • note that the commits are in reverse here..
  • pick the oldest commit you want to adjust and squash / fixup the subsequent commits.
  • You may want to choose reword instead of pick on the oldest commit so that the final commit message mirrors the final diff.
  • the result will leave you with a single unpushed commit with a different SHA entirely and since you say the whitespace issues have been resolved in the newer commits, the final commit will be one single clean pushable commit

Upvotes: 0

Related Questions