A B
A B

Reputation: 155

delete file from master branch of repo

A junior developer wrote code directly into the master branch of our repository and it's impacting our production jobs.

My supervisor just completed my code review and merged my PR. He did a git checkout master followed by git pull and that's when we noticed the new file.

Git log showed it was submitted by this developer directly into the master branch.

Can I do git rm --cached filename without damaging anything?

Upvotes: 3

Views: 5972

Answers (2)

Peter Lehwess
Peter Lehwess

Reputation: 91

One should never make changes directly on the master branch nor should you force push to the master branch. For the some insight into best practises read https://8thlight.com/blog/kevin-liddle/2012/09/27/mind-your-git-manners.html

Always branch off of it and make your changes there then merge this branch into master.

So specific for your example:

  • Branch of off master
  • Remove the file and commit the change
  • Merge the branch to master via a PR
  • All done.

This is the cleanest approach and is especially important for code collaboration.

Upvotes: 3

Maroun
Maroun

Reputation: 95978

You can rebase and drop the wrong commit:

git rebase -i HEAD~2

This will open a new window asking you what to do. Drop the junior's commit, and pick yours:

d 54f3714 Junior's commit
pick 044de34 Your commit

# Rebase 8de638d..044de34 onto 8de638d (2 commands)
...

Next, push the changes:

git push origin master -f

Upvotes: 1

Related Questions