I have used "git replace" to substitute a branch (with no common ancestor) 22b2b25 for a commit in master. I want this change to be permanent. I'm not interested in the previous history. In the output below the top five commits are from the original master branch and the bottom 2 come from a different branch.
When I pushed this to a new repository the git-replace was lost and the old history reappeared. Is there a way to make this change permanent?
branch master looks like the below in the original repo. I want to see this in the remote repo and subsequent clones.
[mike@localhost canal_site]$ git log --oneline --decorate
cdf0ae3 (HEAD, origin/master, master) MM: sqlencode course name for ad-hoc courses
2b57df5 MM: fixes for changes so far
7916eaf MM: ad hoc - more refactoring
1e00d22 MM: reformatted code
e36cee1 factored out equal ops
22b2b25 (origin/prod20140313, prod20140313) initial load from production 9-June-2015
08379cd initial inclusion of production files from 9-June-2015
Only when you are up-to-date will you be able to push your own new commits to the remote. The --force option for git push allows you to override this rule: the commit history on the remote will be forcefully overwritten with your own local history.
To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A unique remote name, for example, “my_awesome_new_remote_repo” A remote URL, which you can find on the Source sub-tab of your Git repo.
git replace
alone isn't enough: replacing one commit isn't enough.
The next commit must now reference the replaced commit as a parent.
As mentioned in "Removing a whole bunch of commits from git":
Backup your repo via git clone.
Find out the first commit you do like after the commits you do not like and remember the hash of its direct ancestor (in the followingbbb
).
Then find the first commit you do like before the commits you do not like (in the followingaaa
):
git replace bbb aaa
Take a look at your repository (with
git log
) and check whether everything is as it should be after your intended change.
But thereplace
command just adds some meta information into.git/refs/replace
, which git commands interpret correspondingly.
In order to make these changes permanent, you may usegit filter-branch
:
git filter-branch -- --all
Then you would need to force push (git push --force) in order to override the remote repo with the new history of the branch. Or you van just push to a new empty bare repo.
Update (inspired from Stephen's comment)
As I noted in Oct. 2019 in "Git commands that could break/rewrite the history", git filter-branch or BFG are obsolete.git filter-repo
is easier to use and more efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With