2

I'd like to push my project and share the repo on Github and my coworkers. But I have one problem, while I'm developing the project I committed some private stuff e.g. passwords to the git, so I don't want to commit the history to Github. I just need to share the latest version of the Project, one last commit to Github, without past commits, history.

What I tried:

git push -u github_tmp d3710442f80889be21324d5be14a25fe4a0d0274:refs/heads/main

This didn't work, this does commit to Github but it shows me not 1 commit but like 26 commits (yes included all the past commits history on the branch)

So, how can I do that? Thanks.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
ー PupSoZeyDe ー
  • 1,082
  • 3
  • 14
  • 33
  • I don't think "shallow push" exists (the analog, a shallow pull exists and does the opposite of what you want). See [this question](https://stackoverflow.com/questions/50992188/how-to-push-a-shallow-clone-to-a-new-repo) for details. **tl;dr** better to rebase to make the local history appear as you want and then push that. – Joachim Sauer Mar 27 '21 at 14:33
  • If you just want to push your last commit and don't need any history why not just delete the `.git` directory and re-initialize your repo with `git init`, set your remote with `git remote add` command and then push to that remote GitHub repo. Or you could try squashing your commits before pushing. If `Squash and Merge` is the general approach that your GitHub PR reviewers follow, then I don't believe you have nay reason to worry as they will anyways be `Squashing and then Merging` your PR. If you have the permission to merge PRs then you could do it by yourself too. – Asif Kamran Malick Mar 27 '21 at 14:49
  • Does this answer your question? [How to push new branch without history](https://stackoverflow.com/questions/12543055/how-to-push-new-branch-without-history) – AD7six Mar 27 '21 at 15:10
  • 2
    Please note that commits _are the history_. – evolutionxbox Mar 27 '21 at 15:32
  • Have you tried `git rebase` to delete those commits locally? – astrochun Mar 27 '21 at 15:46
  • It's unclear what the goal is. Are we making a completely new repo consisting of just one commit, or are pushing a branch to an existing repo while squashing that _branch_ down to one commit? – matt Mar 27 '21 at 23:08

2 Answers2

1

The best approach is git rebase. You can delete the commits that you do not want to be pushed. You can also "squash" together any commits that you want to be seen as single commit.

If you want to keep the commits locally then you create a new branch to be pushed.

git switch -c my-branch-to-push
git rebase -i oldest-commit-to-start  my-branch-to-push

Now you get to edit the list of commits, and you can remove entries from it. Then you git push the new branch. (might have to set upstream tracking)

Of course, if your current branch already has the upstream set, then you keep the newly created branch, and rebase the current.


You can also do

git filter-branch

https://git-scm.com/docs/git-filter-branch But it has its pitfalls....

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Martin
  • 521
  • 5
  • 8
0

If you don't need your history in local you can reset to the origin branch using the mixed reset,

git reset --mixed origin/<branch>

with mixed reset you lost your history, but not your changes, nothig happens to your working directory

more info about reset https://git-scm.com/docs/git-reset

a this point you don't have nothing to push and you can commit in a new commit