Python Forum

Full Version: deleting git branches
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
So in one of my tutorials i use git branches to show code as there are numerous modules not illustrating it well on the forums posts. So each branch is a modification described more in detail on the tutorial. However, because i was writing the tutorial without any clear destination i rewrote the code mid-tutorial making it a little harder to understand. I dont normally revert published commits so i am not sure of the commands used in this scenario.
Because its a tutorial repo i would rather not mess it up either, otherwise i wouldnt really care.

I could just link to the last current branch, but if they actually did poke around the repo it would be a little confusing with previously modified code that no longer exists. As well as the comparison page would not show all modifications associated with the tutorial. And i use that to indicate the changes that occurred in the tutorial.

tldr; i want to delete mid-branches

All has already been published and pushed. I basically want to go from commit "added game over state and restart" to "cleanup3" as the in-between is a little confusing for a tutorial. Then rewrite the tutorial to never mention those commits.

$git log
commit e4dd5feaeac7832b0e352cdfe497109aa46139f2
Author: metulburr <[email protected]>
Date:   Sun Jan 26 18:02:54 2020 -0500

    cleanup3

commit 81cdf2324b9adf793ac3480c3c8240b1b0bcec6e
Author: metulburr <[email protected]>
Date:   Sun Jan 26 17:45:38 2020 -0500

    cleanup2

commit c657adb0e0e4eec52bb3949dafbdb99e28b51880
Author: metulburr <[email protected]>
Date:   Sun Jan 26 17:31:03 2020 -0500

    cleanup after menu

commit ecef4a5220f182eac15fd3f6351f86e6dea4f833
Author: metulburr <[email protected]>
Date:   Sat Jan 25 22:51:47 2020 -0500

    added game over and restart

commit 787630dd7e44b44e4f25d0ca64737b7fa3020659
Author: metulburr <[email protected]>
Date:   Sat Jan 25 19:13:42 2020 -0500

    added options menu

commit 7944f090e52e3ca908fa6756ce71247b1c56151e
Author: metulburr <[email protected]>
Date:   Sat Jan 25 18:57:35 2020 -0500

    added basic menu system

commit 3308118fbc6d41636fbad30759976c03dbc947ee
Author: metulburr <[email protected]>
Date:   Sat Jan 25 18:00:39 2020 -0500

    organizing states

commit 04b3f8f8356d9ca77796b04f13ac6d461baf1560
Author: metulburr <[email protected]>
Date:   Fri Jan 24 23:48:46 2020 -0500

    added state machine

commit 810571ec6ed6d979346920dd7ed0e2d254692738
Author: metulburr <[email protected]>
Date:   Wed Nov 29 17:08:40 2017 -0500

    moved all_in_one_file.py into data and renamed it to control.py

commit 0f3405ee63e3f78b7e2d51fb8932650af321272d
Author: metulburr <[email protected]>
Date:   Wed Nov 29 17:00:34 2017 -0500

    moved player and enemy classes out of main file and into their own modules
:
Restating, you wish to remove the last 3 commits and then re-commit the most recent. Correct me if wrong.

1. Copy the files in the current commit somewhere safe. Maybe twice.
2. git reset --hard HEAD~3 (that's a tilde) to remove the last 3 commits
3. Copy files back to your git and commit and push.

I'm new to gits and github so suggest backing up before doing.
the files i am not worried about...it is the branch history for the tutorial sake.

That should in theory work. I was just thinking there was a command to do it instead of manually doing it.
If you want to keep the changes that are made generally there are different ways to do that. You could delete the last commits (including cleanup3) but save your changes you made and push everything with a new commit, then this could help https://stackoverflow.com/questions/4114...ous-commit
But the best way to do it would be to simply rewrite the history:
git rebase -i HEAD~3
there you can edit the commits or simply squash them and even reorder them. This has no offect on the code only the commit history.
If you want to discard what you did before cleanup3, I would say you create a new branch, revert master to the commit you want and then cherry pick what you want to keep from the new branch. But be careful if someone already has this changes and wants to push something it could get a bit messy.
(Jan-29-2020, 12:52 PM)ThiefOfTime Wrote: [ -> ]If you want to discard what you did before cleanup3, I would say you create a new branch, revert master to the commit you want and then cherry pick what you want to keep from the new branch. But be careful if someone already has this changes and wants to push something it could get a bit mess
I just realized i never merged any of the branches into master at all. Ive just been using individual branches that turned stale and link to the individual branch i am referring to in the tutorial. Now thinking about that i am unsure if this modifies it further?
I am not sure how exactly your git history timeline is looking like, but still rewriten the history like I discribed should work on every branch, where ever your commits were made (you just need to switch to the branch). But it cleanup is on one branch and cleanup2 on a different branch and cleanup3 on a third branch you would need to rewrite history on them (you could of course squash them while merging to master, but when I understand you correctly you do not want to do that at all right?) but will still have commits in any form, that will not change.
So I am still not completely sure what you want to do, but if it is just renaming/discarding the commits it will work through rewriting the history, though it depends where the commits are located at. But if they are all on one branch it can be done through that :)
first i mirrored the repo in case i messed something up.

I hard reset the head back 3 commits. Copied the updated files back to the repo and created a new branch for them.
metulburr@ubuntu:~/repos/ShooterGame$ git checkout -b cleanup_after_menu
M	data/states/game.py
M	data/states/game_over.py
M	data/states/menu.py
M	data/states/options.py
M	data/tools.py
Switched to a new branch 'cleanup_after_menu'
metulburr@ubuntu:~/repos/ShooterGame$ git add .
metulburr@ubuntu:~/repos/ShooterGame$ git commit -m "cleanup after adding menu"
[cleanup_after_menu ec53c60] cleanup after adding menu
 6 files changed, 118 insertions(+), 118 deletions(-)
 create mode 100644 data/menu_manager.py
metulburr@ubuntu:~/repos/ShooterGame$ git push origin cleanup_after_menu 
Username for 'https://github.com': GONE
Password for 'GONE': 
Counting objects: 10, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 1.56 KiB | 0 bytes/s, done.
Total 10 (delta 7), reused 0 (delta 0)
remote: Resolving deltas: 100% (7/7), completed with 6 local objects.
remote: 
remote: Create a pull request for 'cleanup_after_menu' on GitHub by visiting:
remote:      https://github.com/metulburr/ShooterGame/pull/new/cleanup_after_menu
remote: 
To https://github.com/metulburr/ShooterGame.git
 * [new branch]      cleanup_after_menu -> cleanup_after_menu
The comparison is correct as it goes to from the correct branch to the correct branch. However the old branches are still there. Its not a big deal as the comparison is correct in the last new branch, but i am wondering how these branches exist if i reset it back 3? Or is that because i never merged them into master in the first place? It also does not show in git log, but shows on the website.
metulburr@ubuntu:~/repos/ShooterGame$ git log
commit ec53c60f29b4a1586a26fa5e0a7cc95f124f9d13
Author: metulburr <[email protected]>
Date:   Wed Jan 29 08:33:58 2020 -0500

    cleanup after adding menu

commit ecef4a5220f182eac15fd3f6351f86e6dea4f833
Author: metulburr <[email protected]>
Date:   Sat Jan 25 22:51:47 2020 -0500

    added game over and restart

commit 787630dd7e44b44e4f25d0ca64737b7fa3020659
Author: metulburr <[email protected]>
Date:   Sat Jan 25 19:13:42 2020 -0500

    added options menu

commit 7944f090e52e3ca908fa6756ce71247b1c56151e
Author: metulburr <[email protected]>
Date:   Sat Jan 25 18:57:35 2020 -0500

    added basic menu system

commit 3308118fbc6d41636fbad30759976c03dbc947ee
Author: metulburr <[email protected]>
Date:   Sat Jan 25 18:00:39 2020 -0500

    organizing states
I am not sure why it is shown online but not locally on your computer. From where did you create the new branch (master or other branch)?
But regarding your question, you only created a new history, that has no effects on the branches, if you want to discard branches delete them ( https://koukia.ca/delete-a-local-and-a-r...df0b10d323 )
I created the new branch from a branch
then merge the new branch to the old branch :) this way you get back on your original track
Pages: 1 2