0

I am new to GIT and I am trying to wrap my head around its concepts. As a result I have some basic questions. Please pardon my naivety. I searched the internet but ended up getting more confused. Here are my questions:-

$ git log -n 10 --oneline
170daa2 (HEAD -> master, origin/master, origin/HEAD) Revert "Undo Pull request from feature branch test"
e3f0714 Revert pull request test This reverts commit c6b3b1a
c6b3b1a Revert commit
b72e92e Undo Pull request from feature branch test
3077d10 (origin/feature/test1, feature/test1) First commit
1ba77e8 Updated test1
8fceee2 Revert Commit 1
ecddd63 Test for remote status
6c5a094 Cherry Pick 1
3b66732 Fixed conflict

$ git log -n 6 --oneline feature/test1
3077d10 (origin/feature/test1, feature/test1) First commit
1ba77e8 Updated test1
8fceee2 Revert Commit 1
ecddd63 Test for remote status
6c5a094 Cherry Pick 1
3b66732 Fixed conflict
  • As seen from above, when I do git log while being on the master branch (first output above) why does it also show commits from my feature branch (feature/test1) along with the ones in my master ? When I explicitly specify the branch in the git log it then shows the commits pertaining to that branch only, right ? Is it because git will show all commits (irrespective of any branch) combined together in chronological order ? Can anyone please explain.

  • How to delete commits on a remote branch ? -
    Suppose I have made 2 commits on feature branch and merged them to remote master via a pull request.
    Now, I realize those are bad commits and I want to cleanup and rollback to the state where my remote master (and the local master) branches were before the pull request was approved. I know I could use: git reset --hard HEAD~2
    to get rid of these 2 commits on the local master branch, but how do I get rid of them from the remote master branch since they were already merged to master ? Is it that reset is used for local commits while revert is used for remote commits ? What is the difference between these 2 commands ? and how do I solve my question -

    • Do, I need to first reset master locally and then "force push" this state to remote master by running: git push origin +master ? Is this a correct approach ? OR
    • Do I use git revert -m 1 <commit-hash> to undo commits from remote branch with the caveat that the revert will rollback and make a new commit. Is there any way to rollback on both remote and local master branch without making a new commit ? Please suggest the best approach.

Since I am just getting started with git it may be possible that I have asked a wrong question. Either way please excuse my naivety. Any help is appreciated.

marie20
  • 723
  • 11
  • 30
  • please can you specify exactly what you want to do? I mean in the example you provided, what do you want to achieve exactly? cz u asked a general question, and if u ask a general question you will get a general answer, and if you're beginner, then I don't think that general answer will help you. So, if you could particularize your question and specify your goal, that would be great. – Alan Deep May 21 '20 at 18:29
  • Here is your answear buddy: https://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch – Ran Eldan May 21 '20 at 18:31
  • @Alan- My remote master has been merged with bad commits from my feature branch by a pull request. How can I revert back the remote master to its earlier state.. The commits in the example are few random commits i made for testing. – marie20 May 21 '20 at 18:31
  • If you want to delete commits to remote branch, then you answered it yourself, just `git reset --hard ` and `git push --force` if you're on the same branch – Alan Deep May 21 '20 at 18:32

3 Answers3

1

if you need to delete the commit (bad code or whatever ) , is simply go back to the commit just before then copy the hash (sha-commit) :

1) git reset --hard sha-commit

2) git reset --soft HEAD@{1}

3) this commit message is an example feel free to change it :

git commit -m "Reverting to the state of the project at sha-commit"

4) git push origin master

now your last commit in the master branch is on the commit you choose

OAH
  • 1,160
  • 2
  • 11
  • 24
  • I don't think that this will work as expected since the bad commits have already been pushed to remote master. Please correct me if I'm wrong – marie20 May 22 '20 at 06:25
  • this is the solution for bad commit already pushed into master – OAH May 22 '20 at 09:26
0
git revert -m 1 <commit-hash> 
git push -u origin master

the -m 1 is needed because your commit is actually lives on 2 diffrent branches (master and feature/test1) once you merged it. So you need to tell git what directions to choose when reverting the commit.

Ran Eldan
  • 1,350
  • 11
  • 25
  • which one between `git reset --hard && git push --force` OR `git revert -m 1 && git push -u origin master` ? If I use it revert I still need to do `git reset --hard` for local, right ? – marie20 May 21 '20 at 18:37
  • Don't use --force. Just don't. git revert should be your choise – Ran Eldan May 21 '20 at 18:40
0

ok to answer your question briefly and simply:

If you want to correct an error that has been made on the Remote branch, then simply do the following:

1) checkout locally to the branch you want to push to master, basically it's master branch, checkout there using:

git checkout master

2) git log --oneline

to check all your commits (I know you know that)

3) inspect and find the last commit you want your master branch to point at

4) after finding the hash commit, do the following:

`git reset --hard

5) Now you need to force push to the master branch

Make sure you're still checked out on master and:

git push --force

and Voila, you're done! However, just make sure that if some other fellows will re-pull again from master, then they have to git pull --rebase or otherwise their git pull will fail if they were already on the old repository.

Alan Deep
  • 2,037
  • 1
  • 14
  • 22
  • thanks for your reply. Could you also clarify why `git log` on the master branch also shows commits from my feature branch besides commits of the master branch ?? – marie20 May 21 '20 at 21:14
  • First you have to understand what a branch is. A branch is actually a series of commits, where the head point to a specific commit. So to answer your question, they will surely show because you checked out the feature branch from the master branch. – Alan Deep May 22 '20 at 12:14
  • which means that, if you have a branch master consisting of only three commits A,B and C and you decide to checkout a feature branch from the master branch right now, your feature branch will also have the three commits A, B and C. So, right now they are the same. – Alan Deep May 22 '20 at 12:16
  • Once you start committing on the feature branch, for example you decide to make some changes and then you commit your commit D, then your feature branch will have A, B, C and D and master branch will have A, B and C. – Alan Deep May 22 '20 at 12:17
  • So this is what happened exactly in your case, you checkout from master branch to feature branch and you see A, B and C in your feature branch because they actually come from master branch. – Alan Deep May 22 '20 at 12:18
  • You have to understand something, that a commit is nothing but some -CHANGES- you made, you can't carry this commit and give it to me and tell me check what I did, because I don't have your previous history from which this commit was created. – Alan Deep May 22 '20 at 12:20
  • I would also like to explain something when you rebase, that once you rebase, or modify your commit history, you are not modifying the commits itself, you are modifying the commit history only. For instance, you decide in your master branch to modify commit B, for example you wanna change the name you gave to that commit, well in this case, as a beginner you will think that you only changed the name, well that's wrong, because by changing the commit name, you are also changing the commit hash of B, so right now it's B', and since C depended on B, then C will not remain C, it will become C' – Alan Deep May 22 '20 at 12:23
  • and the commit hash of C changed as well, so master branch become A, B' and C' – Alan Deep May 22 '20 at 12:23