git push to remote and lose all history - eclipse

I have a git project that I'm about to push to SourceForge. The origin for my local repo is a shared file system repo that gives me a backup facility.
When I eventually add SF as another remote I just want to push the latest (= versioned) commit to it as the starting base of my code on that repo, and not include all the previous commits that contain possibly rubbish/sensitive/embarrassing code.
My question is similar to this one, except that question was about just leaving out some of the history - I want to leave out all of the history and have the latest commit to become the starting point of the project code on SF. Importantly, having done this, I want "push to upstream" to continue to work even though origin and SF will be different.
Is this possible? Incidentally I'm doing this through Eclipse ie. eGit.
Update
My original question should have been clearer, although the answers so far have helped clarify exactly what I'm trying to achieve.
I want just consolidated commits pushed to SF, representing the published versions.
This is what I want to do:
[master] A--B--C--D--E--F--G--H--I... --> push to origin (private)
\ \
[public] V1----------V2... --> push to public remote repo
#michas's answer starts me off with V1 on branch public, but I can't figure out how to continue to extend this branch with subsequent version commits. I've experimented with rebase but can't get the result I want.

It sounds like you want to start with a new repo. Why don't you just delete or rename your old repo and create a brand new one. Then copy all of your files in, commit them, and push.

Well, you cannot push the current commit, as this commit contains the whole "rubbish" history.
However you can create a new commit with the same content but without any history.
git checkout --orphan fresh # create a new branch called `fresh` without any history
git commit # add your work as a new commit
git diff fresh master # the both branches should contain the same content (assuming you original branch was called `master`)
git log # verify the current branch does not contain any history
git push sf fresh # push that branch
git push sf fresh:master # (or you might want to call that branch master on sf)

The answer provided by #michas didn't allow me to subsequently maintain the branch with consolidated history. This required the use of git merge --squash. The scheme I eventually came up with was similar to the one described here.
Just tidying up so the question has an upvoted answer.

Related

What is the best practice to get the updates from another repo without making any PR after changes?

I'd like to know how to proceed in GitHub where I could to be able to get the updates from the original repo but prevent opening a PR after each time I push a change made by myself?
The concept I want to apply this is to use a blog template for my GitHub pages. I'd like to get the feature for the future if the contributors would make any but at the same time, I'd like to prevent pushing anything to the original repo as a PR since those commits wouldn't include anything related to making a contribution to the project.
PRs aren't generated automatically, you need to explicitly create them from a branch.
You can fork a repo and work on it, and when needed, fetch and rebase from the original repo you forked from. As long as you don't explicitly use this repo to create PRs on the original repo, you should be fine.
EDIT - Adding some details as per the last comment:
Assume there's a repo called something owned by someone. You can start off by forking it to youruser using the GitHub UI. Then you can clone your fork and work on it:
git clone https://github.com/youruser/something.git
In order to get the recent changes from the original someone/something repo, you need to set it up as a remote. By convention you'd call this remote your "upstream", but you can really give it any name you choose:
git remote add upstream https://github.com/someone/something.git
Once you've added it as a remote, you can fetch from it and rebase on top of it:
git fetch upstream && git rebase upstream/main
(note that using the main branch is just an example. You can of course rebase on top of any branch in the remote repo)
I think it's not possible because when you clone or fork that repo, from that time, you start to add your own content to it since it's your personal blog. So you cannot keep getting the features from main repo. Maybe you can try rebase but I'm not sure if it works for this case. Or you can add those features to your repo by your own whenever you need them.

Why can't I revert a GitHub commit?

I am a newbie in the GitHub world. I've been working on a project for my coding bootcamp. I had everything working just right to the specifications of the project and I was all done. Then I foolishly seem to have committed an old early version of the project, thus covering over the final version. I have tried to revert the last commit from the GitHub website, but the interface does not seem to follow the instructions. I tried the "git revert {commit#}'" command from my terminal, but that did not work either. I need suggestions. I'd like to get it done from the command line if possible.
git revert commits a reverse change, so from the history point of view you will have two unnecessary commits that cancel each other. The {commit#} in your case should be the ID of the commit that you want to undo (= the last one). This should work as long as there are no other commits on top of it, otherwise you might get conflicts which require more work.
If you don't have any other commits apart from the one you want to undo, there is also a better way - simply move the branch back to point to the last commit you want to keep (= one before last).
Something like this (I assume you are working on master, that you didn't do revert yet and that there are no other people involved):
git checkout -b tmp_branch master~1
git branch -f master tmp_branch
git checkout master
git branch -D tmp_branch
git push -f origin master
And voilĂ . If your master is protected in GitHub, you will have to unprotect it. You can repeat this to go further back (or just use ~2, ~3 etc.)

Deleting a commit in github

Is it possible to undo one or two faulty commits I have made to Github. I'm not very good at coding either so if there's a way to do it without coding, that would be great.
Thank you very much
You can do this via doing the following command in your local workspace:
git reset --merge <hash>
Where <hash> is the commit prior to the commits that you want to revert.
The above command is enough if you haven't pushed to GitHub. If you have pushed to GitHub, you then run the following to force an update of the remote branch:
git push --force origin <branch>
(Note: This is assuming that your remote is named origin, but rename as appropriate.)
Note that this re-writes history on the remote branch, and is not recommended if other developers are using the same branch. If you know who the developers are, you can communicate with them to delete their local copies of the modified branch and ask them to run git fetch to get a new copy of the branch.

Stopped Using Git - Now Want to Start Again. How to Get Synced Back Up?

We had a number of developers working on a large website project using Git. We have a GitHub repository and then we have the website on the server, plus all the developers have their local versions.
When we finally launched the project, I got lazy (hangs head in shame) and started making changes directly to the server, without pushing them back to the Github repo. However, other people made changes to the repo, for reasons I don't quite understand, that were never pushed down to the server and are now either outdated or wrong. We have been doing this for almost seven months.
Now the server and repo are hopelessly out of sync. I would now like to get the most updated version of the site (which is the server) back up to the Git repository so we can begin another round of development. I basically want to start with a fresh copy of what is on the server.
How would you recommend I proceed? That was the first time I had used Git. It didn't seem like such a big deal at the time but now seems like it is harder to start up again than I thought.
I have looked for instructions and don't really see anything that fits. Because I am not super confident in my Git skills, I am afraid to just start trying the few ideas I did find and losing what I have on the server.
(I know I could restore from a backup if I really messed it up but would prefer not to do that as it would take the site down.)
Can I uninstall git and start again with a fresh repo? Or is there a safe way to push the current version up to the repo?
Thanks for your help.
UPDATE: I found this answer elsewhere (Replace GitHub repo while preserving issues, wiki, etc) but I am not sure how to do this:
cd into "new" repository
git remote add origin git#github.com:myusername/myrepository (replacing myusername & myrepository accordingly)
git push --force origin master
Possibly delete other remote branches and push new ones.
Not sure what they mean by "new repository"
Make a new branch and push it to GH.
Make a new branch based on the previous
branch.
Switch to the new branch (created on #2).
Delete all the files and folders on this branch repository
except the .git folder and contains
(maintain the README.md,
.gitignore and other files if you want it).
Copy all the files from the server except
.git folder.
Commit.
Switch to local Master (created on #1)
Merge this new branch with the previous one.
Solve conflicts
(I use SmartGITthat have a visual conflict solver and helps me a lot, but you can use gitdiffif you don't want a visual interface)
Commit
Push it to GH.
I hope this helps
I figured this out. What I did was:
Make a new branch on Github to effectively store a backup.
$ git add . to stage all changes
$ git commit -m "Commit message" to commit changes
$ git push --force origin master to force changes from server to remote branch master
Once I did this, there were still hundreds of files I had deleted on the server that were not reflected on the remote github.com repository. I used the following:
$ git rm $(git ls-files --deleted)
See Removing multiple files from a Git repo that have already been deleted from disk
Then repeated git commit and git push. Now my github repo matches my server exactly.
I have not yet deleted the "backup" branch I created on github but I will.
Hope that helps someone.

Reverting to last commit in Xcode 4 using Git

We just moved over to Git from SVN. In trying to clean up some unused files. I saved before deleting one folder that I thought we weren't using. I did not push this to the origin. I realized we are using one of the files in the folder after all, and would like to revert to my last commit. This is on my own branch from the master. I can't find a way to do that in Xcode. Am I missing something? Thanks.
You can see here for rsanchezsaez answer: Xcode 4 git integration
Xcode 4 won't let you to checkout older commits within the user interface, unless you created a new branch for that commit. Nevertheless, you can do it from the command line. For that, use the following command from your project folder
$ git log --format=oneline
to get the hash code of the commit you want to go to, and then use:
$ git checkout desired-hash-code
to checkout that particular version. Once there, you can make tests, changes, and possibly create a new branch. If you do a commit without creating a new branch, you will lose the newer commits in your current branch. If you want to go back to the newest commit after having performed some tests on your older version use:
$ git checkout master
note again that this won't work if you do a new commit from your old code version without creating a new branch, because newer commits in the current branch get dereferenced.
Also, please consider searching SO before asking. Many questions had already been asked and answered.
From the command line, run a program called gitk - this will allow you to visualize the commits you currently have. Find the ID of the commit you want (e.g. the previous commit) and do the following on your branch:
git tag JustInCase
git reset --hard <commit ID>
Refresh gitk, and if you're happy with the results then delete the tag using:
git tag -d JustInCase
If you're not happy with it, just do:
git reset --hard JustInCase
git tag -d JustInCase
To visualize this for you:
1) Start
2) After tagging and resetting your branch to the previous commit.
3) After deleting the tag and doing Reload in gitk.