-1

Basically I cloned a git repository and started coding on it, but it still shows the original collaborators and their commits on my repository. Ideally I could fix this without starting over on a fresh repository (have already done a lot to the wiki pages for current repository)

I want my result to look like this:

1.) I rm -rf .git my git files in my local system but keep the original code files

2.) I initialize a new local git repository

3.) I delete all files in my remote repository

4.) I link my new local repository to the remote repository

5.) I git add . my code files so it is now in the remote repository

  • 2
    "*but it still shows the original collaborators and their commits on my repository*" That is how it's supposed to work. By squashing your fork into one big initial commit that says you're the author you may be violating their license and committing plagiarism. I'd just get used to the idea that you'll be building on other's work and will keep their attributions. – Schwern Jul 21 '22 at 19:38
  • You've almost given the answer yourself. Caveat, I agree it's not good practice, you should keep credit where credit is due, unless you started from a template that specifically allows use without attribution in their terms. But, `rm -rf .git`, `git init`, `git add .`, `git commit` will give you the local state you want. Then you can force push that onto your remote, or just delete your remote and start fresh with a new remote. – joanis Jul 21 '22 at 20:04
  • 1
    Actually, nevermind all that, see https://stackoverflow.com/a/23486788/3216427 for a much better answer. Use that solution, and follow it with `git push -f origin main --allow-unrelated-histories` as suggested by @AndrewMascillaro in his answer. – joanis Jul 21 '22 at 20:09
  • 2
    Does this answer your question? [How to squash all git commits into one?](https://stackoverflow.com/questions/1657017/how-to-squash-all-git-commits-into-one) – joanis Jul 21 '22 at 20:12
  • For everyone saying it's plagiarism, this is for school and it's a starter they gave us, so we aren't supposed to be showing the old collaborators on it – mrspoopybutthole Jul 22 '22 at 04:43

1 Answers1

2

It's bad practice to overwrite all previous commits like this, as this essentially removes all previous version history and potentially takes away credit from whoever made those previous commits. However, the cleanest way to do this would be to squash all previous commits. This way you don't need to create a new repository at all.

git rebase --root -i

This will open an editor with a list of all of the commits everyone has made. For all of the lines with commits except the first one, add the word 'squash' to the beginning. Then, force push these changes to your main branch. It will have a different first commit so you need to add an extra flag to push.

git push -f origin main --allow-unrelated-histories
  • 2
    Won't removing all the lines but the first commit delete all the following work? – Schwern Jul 21 '22 at 19:40
  • 1
    Good catch - instead of deleting the line, replace the word pick with squash. I'll update the answer. – Andrew Mascillaro Jul 21 '22 at 19:44
  • This can be messy if there were merge and if the history is complex. You're potentially making git do a lot of work if the history is complex. And since this won't include the merge commits by default, any conflict resolution that was performed in the past is potentially lost. – joanis Jul 21 '22 at 20:02
  • I ended up making a new repository and deleting the old one, and copied over all of the old files. Thanks though and I'll try this next time if I ever run into a similar problem again :) – mrspoopybutthole Jul 22 '22 at 05:12