git diff --name-only and spaces


I have a commit that includes several files

One of them is a text file that i would like to commit as new instead of the previous one.

This file has allot of changes from the previous version - and the changes are not readble.

I would like that, once i use

git diff


git format-patch

The diff file created - will delete the previous version of this specific file and add the entire modified file.

As for the rest of the file commited - it will use the diff as it is

is there a way to do it?

Hi @eshlomi1,

Welcome to the Github Community Forum!

This is a little triky, but i think we can get on it…

From now on i will call B the commit that you have create last and A the previous commit, and also consider to start from the master branch.

With a git log you can see the id of the commit , something like this:

commit **cb2fa6226febb3f3b6be53b15bba4c83adeaff9f \<-------- This is the id** Author: User name <>
Date: Thu Feb 14 16:04:07 2019 +0100


Now create a branch from A with the git command

git checkout -b <name_of_branch> <commit_id>

In my case i will use this:

git checkout -b **cb2fa6226febb3f3b6be53b15bba4c83adeaff9f**

(You can also use the HEAD^ for the commit id, this refer to the previous commit of the HEAD commit)

Now i have this new branch called and I want to import the file _ test.txt _ for example.

The merge --squash we are going to use is the merge command but without commit:

git merge --squash **master**

This will import all the changes made with the commit B.

Now you can take 2 ways:

  1. Use this branch like a substitute of the previous one (if is not the master branch);

  2. Reset the branch master to A, do changes in the branch than merge.

Way 1

This works only if the main branch is not the master, if it is go to Way 2 below.

Reset all the files:

git reset **cb2fa6226febb3f3b6be53b15bba4c83adeaff9f**

And do wherever you want like create a new commit with only the test.txt. Do not delete the other branch before you’ve concluded. When you are sure to commit all the changes you want in this branch you can remove the other branch with this command:

git branch -D <the_other_branch>

And if you also have the same refs in the remote you need to use this:

git push origin --delete <the_other_branch>

And you have done.

Way 2

Do your commits in this new branch. When you have done and you want this changes in the other branch you can checkout in the previous branch, here i will use the master:

git checkout master

Now you need to remove the old commit B and move to the previous one in this example we call that A:

git reset --hard **cb2fa6226febb3f3b6be53b15bba4c83adeaff9f**

Let’s merge the branch testOne:

git merge

Now you need to force push this master into the remote version:

git push --force origin master

(Be carefull to use this command because this update the remote history for you and all of your collaborators)

This is it.

Hope this helps, ask if you have more questions :slight_smile:


Mark helpfull posts with Accept as Solution to help other users locate important info. Don’t forget to give Kudos for great contents!