Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rewrite shared Git history already on Github?

Tags:

git

github

I keep reading about the dangers of squashing/rebasing/amending commits that are already shared with your team members. If the team is asking for this change, how do we do it?

Does everyone stop committing files and just have one person rewrite history, push to GitHub, and then let everyone pull + resume working?

like image 343
Ka Mok Avatar asked Nov 29 '16 14:11

Ka Mok


People also ask

Does git rebase rewrite history?

Git rebase Rebasing is the process of taking all the changes that were committed on one branch and applying them to a new branch. Run git rebase and add in the -i option to rewrite, replace, delete, and merge individual commits in the history. You can also: Rewrite a past commit message.

Can you delete commit history in GitHub?

If you commit sensitive data, such as a password or SSH key into a Git repository, you can remove it from the history.

Which of the following commands could alter a repository's existing commit history?

(T/F) Git revert is a dangerous command that alters the repository's history.


1 Answers

Basically we can rewrite git history any time as long as the git repository is not shared.

If you want to rewrite a shared branch (master for instance), you should rewrite it in local. Once you're done, you should cherry-pick the new commits and stop all activities on this branch from all collaborators. Then you push -f origin main (don't forget to unprotect it if protected)

At this point you're done. BUT everybody else needs to get that branch without loosing current unpushed work. This can be done if everyone cherry-pick local non-shared commits from the branches created from the old master to the branches created from the new master.

like image 67
smarber Avatar answered Sep 22 '22 17:09

smarber