I'm working on a project, and I want to see how it ran in its last revision. How do I do it without losing my changes? - version-control

Specifically, I'm using bzr, but tips for any VCS are welcome.

I think there are three options.
Use shelving
bzr shelve --all
bzr unshelve
Create a separate branch with the
latest
Create a patch of you
changes and revert the changes.
Apply patch when you need your
changes back.

Using Git:
git checkout HEAD^ # get the previous version, changing files on disk to match
make # run your project (or whatever command you use)
git checkout master # return to the "master" branch
The above applies if you've already committed whatever current changes you're working on, and want to go back to the previous commit. If you have changes that have not been committed yet, then use the stash:
git stash # save the uncommitted changes away
make # ...
git stash pop # restore your uncommitted changes
You can make and commit other changes in between the stash and the pop; this is Git's solution to the "boss interrupts with an immediate bug fix request" problem.

Related

Git revert and reset : Files in the repository are not getting rolled back

I want my entire repository to roll back to a certain previous commit (that is 20 commits behind my latest commit).
I tried using git reset --hard <commit#> and git revert <commit#>
Both the git commands run successfully with the message - Head is now at <commithead>
But then when I see the code in my repo, I can still see the edits I made in my latest commits. How can I remove all the edits after the particular commit I am reverting to?
Am I missing out on some step here?
After your git reset --hard, use to be sure the git restore command
cd /path/to/repository
git restore -- .
That should restore (as its name implies) the working tree with what is in HEAD (and you just reset HEAD to the right commit)

Eclipse Git (EGit) - How to revert local master back to remote master after a foul merge & commit with a local branch

I have the following setup:
Remote origin/master (default branch)
Locally, I got the master and created another branch - NewBranch.
Someone in my team updated master on the remote. I was able to pull in all the changes that they made.
However, while merging I had conflicts. Unintentionally, instead of updating NewBranch with the conflicted result, I have updated the local master with NewBranch (because master was the one that was currently checked out-or "active" in Eclipse). Furthermore I committed this change (locally) to my local master branch...
I was able to switch to NewBranch and merge it with all the latest changes (so my Newbranch is perfectly the way I want it).
Now, I'd like the master to point to the same version as remote master does.
So that in the future I have a clean merge between my NewBranch and the Master
I've tried to "Reset" the master, but after performing a hard reset, the Hash Ids b/w the local master and remote master still do not match.
I also have References in my git eclipse and the reference of FETCH_HEAD is the one I'd like my master to revert to.
How can I do this using git in Eclipse?
Thanks in advance
I am guessing you are asking these 2 questions?
Now, I'd like the master to point to the same version as remote master
does
If I'm right follow this, if you master should point to remote master
git pull origin master
If your NewBranch should point to remote master
git pull origin NewBranch
I also have References in my git eclipse and the reference of
FETCH_HEAD is the one I'd like my master to revert to.
Please right-click on project in your eclipse--->go to team----> check the git options
These commands will help you to revert to a specific commit
git checkout master
git reset --hard e3f1e37
git push --force origin master
# Then to prove it (it won't print any diff)
git diff master..origin/master
Alternative
Sometimes you may want to undo a whole commit with all changes. Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it.
git revert ~specificCommit
But remember this after git revert, if you want to go back, you need to read this Git revert be careful
before you do it.
git revert 45ae34 e34526a #created two revert commits
git revert HEAD~2..HEAD #by taking ranges, it will revert last 2 commits
git revert -m 1 <merge_commit_sha> #this basically reverts a merge commit
# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 34e2w34 .
git commit #commit here
Docs:
git docs: undo merges

Git reset hard and going back

From git gui, I used gitk to git reset -hard to a few commits before my current one, as I needed to test if everything was working before the changes.
Since I even had a few uncommitted changes, I git stash in order to save them and being able to reapply them once going back to my last commit.
The problem is that gitk is not showing the top of my commit tree any more (the top commit is the current one and I don't see any commit above it)
It was sometimes since last time I used git, but I thought I can use git reset -hard to bring the current code to a previous version, and then git reset -hard to the old version.
How can I retrieve all the commit between the old HEAD and the revision I git reset -hard to?
Please tell me there is some kind of way.
I'm using Eclipse as development tool (in case I'll need to use it's cache)
What you did could work if you had first make a new branch, before the first git reset --hard.
Because git reset moved your current branch back, and those commits are no longer referenced by any branch (and not visible anymore)
You need to fallback to the command line, and try a:
git reset --hard ORIG_HEAD
# or
git reset --hard HEAD#{1}
ORIG_HEAD or HEAD#{1} should have the SHA1 you were before the first reset.
If not, git reflog can help (that is what HEAD#{1} should be listed in).
Not, as alluding to in "ORIG_HEAD and FETCH_HEAD from history view in Eclipse", you should be able to see ORIG_HEAD in the "History view" of Eclipse.

How to switch branches in eclipse without commiting changes

I've been using GIT for a couple of weeks now and trying to understand how to switch branches without commiting files. This is what I have done.
Cloned a git repository and have a local master branch.
Created a new local branch (Branch2) which is based on a remote branch.
Made changes to 2 files in the master branch.
What I want to do now is switch from master to Branch2.
The changes I made to the master branch are for local dev purposes only and should never be committed.
But when I try to do this in eclipse (i.e I double click on the local branch I try to switch to) it keeps telling me that there are uncommitted changes and I need to commit, stash or reset.
Can anyone tell me how i can make a change to a local file and have git ignore this change so that I don't get prompted with this message?
Note: if you need to stash a work in progress from Eclipse, Egit now supports stash:
Have a look at git stash. Stash allows you to store uncommitted changes.
Option 1
git stash
git checkout -b Branch2
Your changes will be stored in git (locally). When you want to re-apply those changes, you will do git stash pop and it will apply those changes for you.
Option 2
git stash
git stash branch temporarybranch
This will take your uncommitted changes to a new branch and keep them there for you. Compared to the previous option, this allows you to keep these branches on the server by pushing this new branch.
Hope it helps
If you don't want the changes to be ever made public you should just commit them in your own feature branch and not on the master branch.
You can create a new branch, give it a name that reflects the changes, maybe prefix is with something like "experimental-" and then commit to that branch before switching to your other branch.
You can use git stash to stash your changes, or use git stash to commit them directly to a new branch as mentioned by Ege Akpinar.

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.