2

Let's suppose i have made a lot of local commits. I now want to push last commit to my online repository but i do not want the other users to see the last commits. I just want to push last version.

I also want to keep my local commits history.

Do you know if it is possible without having to create a branch ?

Thanks

Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • If my answer helped you please accept this so it doesn't float around next to unanswered questions. – Philipp Jul 11 '17 at 08:10

1 Answers1

4

I will assume your current branch is called foo.

  1. git checkout foo to make sure you are on the foo-branch.
  2. git checkout -b foo-public to create a new branch called foo-public and switch to it.
  3. git rebase -i bar where bar is the hash of the commit before the first commit you created.
  4. The previous command will have brought up a text editor. There you should replace all pick keywords with squash, but not the first pick.
  5. Exit the editor and another one should appear, prompting you for the commit name. You can leave the name of the last commit if you want. Close the editor.
  6. You can now publish the foo-public-branch. It will only have one commit, which contains all of your changes. Your foo-branch still has your complete history.
Philipp
  • 876
  • 10
  • 23
  • 1
    Thanks. Do you know if there is a way to merge commits on gitlab ? – Bob5421 Jul 10 '17 at 09:47
  • Yes. However, rewriting the remote history is generally not a good idea, especially if multiple people are involved. If you want to do it anyway you should look at [this question](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git). If my answer solved your problem please accept it. – Philipp Jul 10 '17 at 09:49
  • thanks, i am working alone. Is there a Single command in order to merge commits on remote repository, without merging it in local repository ? – Bob5421 Jul 12 '17 at 06:00
  • 1
    I know it's an old post, but for people who can't work this out even with this solution, like me, what I ended up doing was copy the whole repo in a new directory, delete the .git folder and start over the process of `git init' etc, as if it was a new repo. – vefthym Aug 14 '21 at 10:48