4

My goal is to get rid of one file in all my commits on github so i did next steps:

I have filtered my branch with this command:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch [file_name]' HEAD

to delete one single file in all commits and now after running

git status 

it asks me to pull,

ubuntu@ubuntu:/var/www/html/laravel/app_folder$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 18 and 18 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean

but i don't want to pull. I'm afraid that if i pull i will have this file again in my commits.

Shouldn't i push it now so the changes take effect on my git hub....i don't know what to do.

lewis4u
  • 14,256
  • 18
  • 107
  • 148

4 Answers4

4

Sure, git on github and on your local computer have different histories : one with the file, one without. You need :

git push --force

to overwrite the github repository.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
3

Yes, you should push... probably.

git filter-branch rewrites history... except it doesn't rewrite it, it creates a new history.

To explain, if your repository looked like this, with the master branch on commit E as well as origin.

A -- B -- C -- D -- E [master]
                      [origin/master]

Then after you ran git filter-branch those commits will have been rewritten into new commits. But the origin (on Github) will still be the same.

A -- B -- C -- D -- E  [origin/master]
 \
  -- B1 - C1 - D1 - E1 [master]

They have "diverged". This is what git status is telling you. Your master is not a child of origin/master.

Usually this means that you've made local changes to master while other people have pushed changes to origin's master. So it's telling you to pull other people's changes.

But this is incorrect. You're overwriting history. So you should git push your new version of master. Except git won't allow you to do that for safety reasons, so you have to git push --force.

Schwern
  • 153,029
  • 25
  • 195
  • 336
2

What git filter-branch does is it rewrites history. Once you have rewritten it, all commits from the point of divergence are considered different. That's why it's telling you that you have 18 different commits - the point of divergence here was 18 commits ago.

You shouldn't merge the branch, either make a backup and then overwrite master with git push --force or just push it as a new branch.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • For the error, see https://stackoverflow.com/questions/19720711/git-push-warning-questions. Just use `git push origin master` and it should be OK. Don't forget to make a backup first, just in case. – sashoalm Oct 23 '16 at 09:50
1

Note: Git 2.42 (Q3 2023) will change the message, suggesting that there are cases where force-pushing is a valid and sensible thing to update a branch at a remote repository, rather than reconciling with merge/rebase.

See commit c577d65, commit d92304f, commit b6f3da5 (12 Jul 2023) by Alex Henrie (alexhenrie).
(Merged by Junio C Hamano -- gitster -- in commit 88d08c3, 25 Jul 2023)

remote: don't imply that integration is always required before pushing

Signed-off-by: Alex Henrie

In a narrow but common case, the user is the only author of a branch and doesn't mind overwriting the corresponding branch on the remote.
This workflow is especially common on GitHub, GitLab, and Gerrit, which keep a permanent record of every version of a branch that is pushed while a pull request is open for that branch.
On those platforms, force-pushing is encouraged and is analogous to emailing a new version of a patchset.

When giving advice about divergent branches, tell the user about git pull(man), but don't unconditionally instruct the user to do it.
A less prescriptive message will help prevent users from thinking that they are required to create an integrated history instead of simply replacing the previous history.
Likewise, don't imply that git pull is only for merging.

So instead of:

use "`git pull`" to merge the remote branch into yours

You will see:

use "`git pull`" if you want to integrate the remote branch with yours
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250