简体   繁体   中英

Rewriting remote git branch without force push or delete permissions

I wrote a first draft of a C# .NET core program as proof of concept, which was uploaded as both master and dev branches. Unfortunately, I forgot to use a.gitignore file and it uploaded all of the debug and release build files.

Since then I've rewritten the whole program to work much more efficiently, and I'd like to rebase/replace the entire master/dev branch with that. We're using DevOps, but I've always used TFS, and I don't understand why git "refuses" to push my branch. It wants me to pull down all of the obsolete remote dev work, then doesn't merge them when I do. Also tried deleting every file in the remote dev branch, so it is now empty on the DevOps site. The admin won't give me force push and branch deletion permissions, probably because I'm new to the commands.

How would I rewrite the dev and master branches with my local code?

Pushing my code (I am on dev branch and have already done "git add." and "git commit -m "Comment"):

git push -f origin dev
Total 0 (delta 0), reused 0 (delta 0)
To [remote repository]
 ! [remote rejected] dev -> dev (TF401027: You need the Git 'ForcePush' permission to perform this action. Details: identity 'a7cf8844-7bf6-48af-88cc-3063f00ff148\[my credentials]', scope 'branch'.)
$ git pull origin dev
From [remote repository]
 * branch            dev        -> FETCH_HEAD
fatal: refusing to merge unrelated histories

From a fresh clone:

git clone [remote repository]
Cloning into 'MyProject'...
remote: Azure Repos
remote: Found 407 objects to send. (316 ms)
Receiving objects: 100% (407/407), 6.47 MiB | 332.00 KiB/s, done.
Resolving deltas: 100% (61/61), done.

$ cd MyProject

$ git checkout -b dev
Switched to a new branch 'dev'

$ git pull origin dev
From [remote repository]
 * branch            dev        -> FETCH_HEAD
fatal: refusing to merge unrelated histories

Git (actually the server) does not allow that.

Git rejects all pushes that don't just add commits to branches.

If you want to rewrite history, you need to force push.

If you rewrite and force push something and there is work based of it, those people need to fix it on their machine too as they have non-rewritten version.

I also recommend you to use --force-with-lease instead of a normal force push as it rejects if the remote contains work you don't know of.

Successfully rewrote the remote branch without doing a force push through the following:

  1. Backup local dev repo.

  2. Recreate the remote repo locally:

git clone [remote repository]

$ cd MyProject

$ git checkout -b dev

$ git pull origin dev --allow-unrelated-histories
  1. Replace new dev repo files with local file backup.

  2. Commit changes and push again:

$ git add .

$ git commit -m "Comments"

$ git push origin dev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM