I have a problem, I've committed some code and now I want to delete this commit, but this is not the last commit in whole project.
I want to delete whole blue branch( if its possible).
if you want to remove the commit use
git rebase -interactive
and
git filter-branch
But you'll have to recommit every commit which goes after the commit you want to delete.
Or if you just want to remove the changes of the specific commit will be better to use
git revert <hash of the commit>
If you want to delete the whole branch, Click the branches icon on the left side of bit bucket, it will display all the branches. There will be columns like branch, Behind/Ahead, Updated, pull req, build, Actions. Click 3 dots in action column for your branch-> choose delete branch. Keep a backup in local before deleting.
I recommended removing the selected commits. use this
git reset --soft <commit ID>
For example
git reset --soft 23wfee 3eelw2 34efff
Then your particular commit will remove but if you don't want that commit changes then use hard instead of soft like this.
git reset --hard <commit ID>
Solve the problem with git rebase -i id of last right commit, then you can delete commits that you don't want to see , then git push -f origin master
Related
How to undo git pull origin command?. i connected to the amazon server ec2-instance using ssh and pemfile. and i navigated to the project directory on this server. And ran the "git pull origin". i want to undo this operation on this aws instance. how can i do this. this is a ruby on rails application
The answers mentioning only git reset --hard are not correct. Once you have done a pull, your HEAD has moved forward to the new point you updated to. The implied ref for git reset is HEAD, so that command will just reset you to... the place you already are.
JonasJR was closer when mentioning reflog. The reflog is a local history of every action you take. If you just performed a pull, it might look something like this.
$ git reflog
6dd1a7b93 HEAD#{0}: pull: Fast-forward
e45d35313 HEAD#{1}: checkout: moving from feature to master
... more ...
Here I have the last two actions I have taken. A pull, and just prior to that I used checkout to change branches.
To go back to the state before the pull, I can use its ref (HEAD#{1}).
git reset --hard 'HEAD#{1}'
Now if I look in the reflog, it will show I reset to that prior state. The reset itself has become part of the history, so you can also use reflog to undo a mistaken undo, if you need to.
$ git reflog
e45d35313 HEAD#{0}: reset: moving to HEAD#{1}
6dd1a7b93 HEAD#{1}: pull: Fast-forward
e45d35313 HEAD#{2}: checkout: moving from feature to master
As you can see, we've now gone away from 6dd1a7b93 and gone back to the previous state, e45d35313 (which is both HEAD#{2} and HEAD#{0} at this point).
Note that the reflog is local to this copy of the repo. If you checkout a copy somewhere else, it has its own completely independent reflog. This is not shared with other people on your project, etc.
The usual disclaimer with reset --hard apply here. If you have uncommitted changes at the moment, this command will wipe them out. In that case, use git stash to set them aside while you do your reset.
You want to undo the merge. Run this command:
git reset --hard
You can also reset to a specific state by running the same command and adding the SHA1 of the decired state. Run:
git reflog
to find out more about it!
I am working a project, and noob to git architechture. I have accidentally commited a few changes to mainline, now I want to revert them back to what is in the main repository.
Now I am on a new branch, but those commits are still there in the mainline branch. How do I do that?
And what exactly is HEAD, I am very much confused.
There are 3 commits i need to revert. So for one of them I did "git revert ". Now when I do "git revert HEAD~3" it shows I need to merge changes and commit n all. But I dont want those changes, that is why I reverted.
I might sound very foolish and confusing, but I am very new to this, and dont want to loose any changes, as its a very crucial project and rather dont want to mess up the mainline branch as well.
I'm assuming your git topology looks something like this:
---A---B---C---D master
newbranch
You'll want to git reset your master branch back like this:
git checkout master
git reset --hard HEAD~3
Your topology should now look like this:
---A---B---C---D newbranch
\
master
To answer your question about HEAD, it refers to the tip of the current branch you're on. If you're on master, then HEAD is A; if you're on newbranch, then it's D.
See the following questions:
What is HEAD in Git?
How to revert Git repository to a previous commit?
Maybe I misunderstood how GIT works.
I've run git rebase -i HEAD~10 and I could squash 10 commits into one. The issue is that all squashed commits are still there, and I thought they were going to be dropped after merging them all into one.
Is this the expected result? If so, can I rewrite the history to remove useless commits (since these changes are already in the commit which all previous commits were squashed)?
When you began your interactive rebase session, you should have been prompted with a list of the last 10 commits from the current branch:
git rebase -i HEAD~10
pick as2i8dw first commit
pick ee361eb second commit
...
pick b2762sx most recent commit
You need to change this file to the following:
pick as2i8dw first commit
squash ee361eb second commit
...
squash b2762sx most recent commit
Then you need to do a git commit to save the changes. Now when doing a git log you should only see the as2i8dw commit and none of the other ten.
That being said, is this what you did?
The issue is that all squashed commits are still there
If those commits are still accessible by any other reference (other branch or tag), there would still be visible, even after the current branch is rebased.
Try instead squashing the commits with a git reset --soft.
If HEAD does still reference your 10 commits:
git reset --soft HEAD~10
git commit -m "squashed 10 commits"
I faced the similar issue and figured out the actual cause for it:
The flow:
git rebase -i HEAD~10
# Retain the first commit from below( as pick) and change the rest of the `pick` to `squash`
# After your rebase is successful
git log
# You can see all your commits squashes to one commit
Then now when you git pull from your remote branch, it will pull the rest of the commits which is not there in local ( basically all the commits you had squashed previously, since it is now present in one commit) and hence you are seeing the previous commits as well.
Better way is to git push -f to your remote branch, if you are confident that you have no new changes added there.
P.S: If you have any new changes in the remote branch, better to:
git rebase origin remote_branch
and then squash your commits.
I have reset my master to a certain commit (hard) in the sourceTree. The master has been reset but I still get the a message to pull (4 behind). On pull all the previous unwanted changes has been added to my repo. How can I ignore any pull requests after Hard rest?
This is because those changes are on your remote. A hard reset will just discard the changes in your local branch and in your working copy.
To fix this you'd either have to do a force push, but this isn't recommended as it's dangerous and pisses off other devs working on that repo, and is generally dangerous. Your other option is going to be resetting the changes to a particular commit. This would look something like this:
git reset -q <SHA> -- <filename>
git checkout <SHA> -- <filename>
Where is the commit which you'd want to revet to, and is the file.
You can do this in SourceTree by context-clicking on a file at a specific commit and hitting "Reset to commit".
Hope that helps.
I created a local Git repo on my laptop and then pushed the source to Heroku creating a remote branch. After a few days of commits and pushes, I need to rollback to an earlier commit. Here's what I did.
cd <app root>
git checkout 35fbd894eef3e114c814cc3c7ac7bb50b28f6b73
Someone told me that doing the checkout created a new working tree and not the branch itself, so when I pushed the rollback changes to Heroku, it said everything is up to date and nothing was pushed. How do I fix this situation? Thanks for your help in advance.
When you checkout a direct commit name (using the SHA-1 hash of the commit object) instead of checking out a branch name, you end up with a “detached HEAD”. HEAD is the “ref” that keeps track of what is currently checked out. It becomes detached when you directly checkout a commit instead of a branch (it is not attached to any branch). No branches are updated when you detach a repository's HEAD. You might think of the detached head state as if you had an anonymous branch checked out.
To reattach your repository's HEAD, you will want to save the current HEAD as a branch and check that branch out:
To save the current HEAD in a new branch do this:
git branch <new-branch-name>
To overwrite an existing branch you need to use --force:
git branch --force <existing-branch-name>
Then, reattach your repository's HEAD by checking out the new/updated branch:
git checkout <branch-name>
(where <branch-name> is the same as <new-branch-name> or <existing-branch-name>, depending on which of the above two commands you used)
This sequence (git branch to make a ref point to the current HEAD commit, then git checkout that updated branch) will carry forward any uncommitted content that you might have in your working index and/or tree.
In the future, if you want to ‘roll back’ the current branch to some previous commit, you should use this instead of detaching your repository's HEAD:
git reset --hard <commit>
This will reset the current branch (or your detached HEAD, if it is already detached) to the named commit, and make the index and the working tree reflect that commit (i.e. it throws away any commits since the specified commit along with any uncommitted content).
The detached HEAD state is useful for revisiting old states, and sometimes for short-term work that you are not sure you will keep. Other than that you probably want to avoid it.
You want to reset:
git reset --hard 35fbd894eef3e114c814cc3c7ac7bb50b28f6b73