last day (16 days later) » 

2:23 PM
1
A: Git revert back to previous commit and keep committing new changes to master

mechanicalsgit checkout -b experimental // Will create new branch "experimental" with all the current commits including "A" git checkout master // Will take you to the master which already has commit "A" git revert <commit-A> // Will revert the commit "A" from master & take master to las...

 
thanks for the quick answer! I'm actually following up with axiac to see what side effects should I expect given that I all my commits have been pushed to origin.
 
Even if you have pushed the changes. You can do a still "git revert <commit>" on master locally and then push that again to the origin. It will revert the "master" branch at origin back to the state you want. Let me know if that solves your confusion.
 
But would "experimental" get reflected in the remote repo?
 
Nope. You'll have to push "experimental" also. So, once you are on the "experimental" branch. You can do a "git push origin experimental". Only then "experimental" will get reflected in the remote.
 
Ok one last question. axiac is suggesting a git reset --hard while you're suggesting a git revert what's the difference between both? Also you're suggesting git checkout -b while he's suggesting git branch, is there a difference?
The answer to my second question is that git checkout -b will switch the current branch, whereas git branch just creates the branch without switching, so I guess I just need to make sure if there's any core difference between git reset --hard and git revert.
 
2:23 PM
"git revert <commit>" removes the specific commit you want to remove as you asked in the question to remove <commit-A>. While "git reset --hard <commit>" will bring current branch tip back to <commit> but will also reset the staging area and the working directory. So, you might also loose all your other changes.
 
Just re-reading you're comments on your answer, I noticed that you're running git revert on commit-A
which probably means that I will get reverted to the commit that is just before A
B is several steps before A
so I guess that means git reset is the right approach?
My working directory is clean (I have no staged or unstaged changes)
 
IF you want to revert a specific commit. Then do a git revert. Else, if you want to bring your branch to a specific point, then do a git reset --hard <commit>
Hope, that resolves the issue.
and yes, in your case if between A-B, there are other commits also & you want your branch to be at <commit-B> then you should go ahead with git reset --hard <commit>
 
ok so then just to summarize the steps are:
git branch experimental
git reset --hard <commit-B>
git add .
git commit -m "new commit C"
git push
git checkout experimental
git push origin experimental
can you confirm?
 
2:38 PM
You want all the changes from A-B on "experimental".
??
 
yes
 
Basically you want "experimental" to be in exact state as in your master is right now ?
 
absolutely
 
then do,
git checkout -b experimental
git checkout master
git reset --hard <commit>
git add <files>
git commit -m "new commit C"
git push origin master
git checkout experimental
git push origin experimental
or wait....
hold on..
 
ok
 
2:42 PM
after checkout just push the "experimental" first
new steps:
git checkout -b experimental
git push origin experimental
git checkout master
git reset --hard <commit>
git add <files>
git commit -m "new commit C"
git push origin master
 
ok thanks a lot!
 
So, now before making changes to master, your experimental will already be pushed to remote.
and you'll always have the changes saved in "experimental" just in case...
 
I'm gonna try this on a test repo and how things go, before checking your answer
 
sure. Let me know if you get stuck.
 
but before I do so, referring to your first suggested steps, I'm just wondering what's the difference between:
git checkout -b experimental
git checkout master
and
git branch experimental
wouldn't the second one create the experimental branch while leaving me on master?
 
2:52 PM
Yes, but it would be better to " experimental" and push your changes right now when you branch is clean. Else, if you modify anything or anything else modifies your files (maybe a server running, generating temp files), those will also get transferred to the "experimental"
But yes, as for your concern, "git branch experimental" would create the new branch without switching you to the new branch.
 
ok sounds good
 
3:12 PM
so i just followed the exact steps you last suggested
and right after git push origin master I get this error:
error: failed to push some refs to 'https://alayouni@bitbucket.org/alayouni/test-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
the test repo is here:
https://alayouni@bitbucket.org/alayouni/test-repo.git
 
3:30 PM
git push -f origin master solved the issue
both branches are intact and at the exact state I want them to be
please edit your answer with the new instructions and I'll be happy to check it
thanks again!
 
 
2 hours later…
5:54 PM
What you did or had to do is actually not related to the issue you faced.
This was happening because, you didn't have all the changes locally, which were there in the remote branch. Ideally whenever you start making changes, you should have done a "git pull" before starting.
Thats why git was asking yo do a "git pull" before you can push to origin.
Also, ideally you shouldn't have used "git push -f origin" . As this will overwrite the changes in the remote branch.
1. Consider a scenario, where you and your friend both are working on master.
2. Now he commits something and pushes it to remote master.
3. Now you do a "git pull origin master" to take in your friend's latest changes & start working on that locally.
4. In the meanwhile your friend again pushes some new commits to remote master.
5. Now if you commit something locally and want to push it to remote master. Then you'll get this message asking you to again do a "git pull" first and then push your changes to master branch.
------------------------------------------------------------------------------------------
So, if I add "git push -f origin" in the answer, This might lead somebody else to overwrite changes in remote branch. Hence, there is a chance that the answer might hamper somebody rather than help. Anyways, I have already explained you the reason you got the error.
-----------------------------------------------------------------------------------------------
Feel free to check the answer if it helped.
Hope I was able to help
Ciao !!!
 
6:20 PM
You actually were very helpful however I do not agree with what you just mentioned
I actually test the new instructions with a brand new test-repo
so there's nobody but me committing to it (which is the case in my my original project as well)
git is telling me I'm behind the remote repo because i did a git rest --hard
which made my local HEAD point to an older commit
otherwise the git push -f origin master is not the only thing that needs to be updated in your answer, the last instruction list you suggested in this chat session and solved my problem, is quite different from what you suggested in your original answer
 

  last day (16 days later) »