I've pushed into a master branch at work and do not know how to remove it

I've pushed into a master branch at work and do not know how to remove it
0

Hi folks,

At work we have a private repo. 2nd day in and I’ve pushed and pulled into the master branch and feel like an idiot.

Does anyone know how on the site website to remove my pull into the master?

I feel like learning git is a massive undertaking… My boss is cool with it and the other devs are back in next week and will sort it but want to salvage any respect I had lol!

Help,

Cheers!

You can rebase the master branch and drop any commits which you not be there. You can also look into git revert.

2 Likes

Stupid question can this be done on the github website? I did the pull there. Cheers Randell.

Tell a coworker about it and do the revert together. It’s your second day at work, you should not do it alone.

5 Likes

100% do what @Horv87 says.

Ideally, you shouldn’t have been able to do that, and this is a good opportunity to suggest that branch protection be put on, which would prevent this occuring again (it seems to have been very easy for you to do, but it should not have been):

https://help.github.com/en/articles/enabling-required-status-checks

It would just stop any direct pushes to master (you would have need to have PR-ed a change, and PRs can be set up to require review as well)

3 Likes

Thanks for this, yes I am getting this sorted on Monday. I’ve let my boss know and I don’t think he even cares to be honest.

Cheers for help :slight_smile:

Thanks for this, I might suggest this to them. I do feel like a right idiot. I take courses on git / github etc but guess this is different when out in the field!

Don’t feel like an idiot: if it is easy to push direct to master, then people will accidentally push to master, it’s inevitable – it doesn’t matter how experienced you are, it will happen if there are no protections in place.

1 Like

I refer to this article sometimes - https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/ - It’s been handy for me in the past.

1 Like

Thank you everyone for your help, lets see what the team says this morning…

Cheers :slight_smile:

Horv87’s advice is a good one—anytime you think you’ve made a mistake, always tell someone first unless you absolutely know how to fix it yourself. Plus you’ll inevitably learn more about what not to do in the future from more-experienced co-workers.

Of course, you can always use Google to find out how to do things too. In this case, your search string might be something like “git undo last commit”, which returns these, among other results:


As @DanCouper said don’t feel like an idiot. This was your first mistake and many will follow. It’s a learning process that everyone has to go through. Just be open and talk to your team when something happens.

Sounds like you already got advice critical to your issue, but just wanted to throw in some extra encouragement and resources. Git screw-ups happen to all noobs (and even overeager experienced devs as well sometimes, haha). I made a bunch when I first started, then I watched the next junior who joined make a bunch as well. Personally, I found the tutorials at https://www.atlassian.com/git/tutorials/ really clear and illustrated well. Below I’ll list basically all the git commands I ever use so you can hopefully direct your own learning a bit more. I recommend creating a playground repo on your own github and practicing. You may also look into getting something like “oh-my-zsh” set-up with a theme that displays your current branch on every line as a helpful reminder!

git branch
see all local branches you have for a repo and see which you are CURRENTLY on
git checkout
checkout an existing branch
git checkout -b branch-name
create a new branch with “branch-name” from current branch
git add --all
stage all changed files
git commit -m
commit all staged files and write a description of what the commit does
git push
push commits to origin branch (follow directions first time to create origin branch on github)
git branch -m
renames a branch, best if done before pushing branch to origin (which creates a branch copy on github)
git rebase branch-name
moves all of your current branch’s commits on top of “branch-name”, good for when your main branch (e.g. “develop”) has new changes from a coworker, but you’re still developing, so you rebase on the updated “develop” (make your own commits first). This is cleaner for dev history then merging, but can require fixing conflicts
git stash
save your un-committed changes from current branch to an array of stashes (good for temporarily saving your un-ready changes if you need to switch branches to work on something else or realize you started on “master” or “develop” main branches and need to create a clean branch first!)
git stash pop
apply the most recent stash of code changes to your current branch
git stash list
see the array of stashed changes
git stash apply [email protected]{n}
if you stashed a few things and have an array, n is the index of the stash you will apply
git status
see what files are modified, what files are staged, etc.
git diff
review your file changes in the terminal before you stage and commit (make sure you didn’t forget any temporary changes, etc.)
git log
see the history of commits of current branch which also shows commit hashes
git reset
un-stage your un-committed changes on current branch
git reset --hard
un-stage and DELETE any un-committed changes on current branch
git reset --hard commit-hash
delete all commits and un-staged changes on current branch, resetting the branch back to “commit-hash” commit (commit hash in this command is some long identifier like 23x323b3n2mkf45…). This will make it like your changes never happened.
git revert commit-hash
makes a NEW commit that undoes the changes going back to code status at “commit-hash”. Use this when you want to keep a record on the branch that you’re undoing previous changes.
git cherry-pick commit-hash
when you have a commit-hash for a change on another branch you want to copy over to your current branch, you can use this command. So for example, let’s say you made 3 commits to the wrong branch before realizing. You cherry-pick three times to get those changes on the desired branch then reset/revert that first branch.