top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

GIT: How I can revert changes which I did accidentally from the file ?

+3 votes
487 views

I opened a file and wrote a piece of code.
When I ran $git diff
it is showing the differences what I did.
Can I get back the file (version) which is in remote branch ?
Is there any command ?

posted Mar 5, 2016 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

As git diff shows the difference between staged area and working directory, so it means that you changed a file and not staged yet, so you want you file to be according to the last commit, for this either you can use reset/ or checkout command.

git reset HEAD bring the file according to the HEAD is pointing to the latest commit.
git checkout which will bring the latest commit file to your working directory.

answer Mar 6, 2016 by Sachidananda Sahu
Similar Questions
+1 vote

At some point I added a large file into a git repository. It now exists on multiple branches, possibly with some changes to it. I'd like to remove it from git, but leave its current form (say the one on the master branch) on the file system.

I tried (on a dummy git archive)

git filter-branch --index-filter 'git rm --cached --ignore-unmatch bigfile' master branch1 branch2

That, however, does not leave a copy of bigfile on the file system.It isn't clear to me why not, though the description of the --tree-filteroption to filter-branch (I'm using the --index-filter option, but is is "similar") states:" (new files are auto-added, disappeared files are auto-removed ... )".
Is there a direct way to do what I want, with git? I've found similar requests;none of the responses point out that the above command actually deletes the file from the file system.

+1 vote

Suppose i have a directory with many files .. I commit a good version .. then I make a bunch of changes and I messed something up .. how do I revert back to the old version? will all the files be replaced from the current messed up files to the older good files? I am using SourceTree.

+2 votes

One practice of using git to have one feature per branch.

Let's say a developer has worked on many small features in many branches. Then he sends one pull request to the central (not controlled by him) for each feature he has developed. While he is waiting for all the features be merged into the central repository, he needs to use all these feature locally.

To do so, he may need to merge the changes in these branches to his local master branch. But this can be tedious when he has many branches.

Is there a way to somehow setup a branch so whenever something is committed to the branch, the changes will also be simultaneously committed to the local master branch? By this way, the develop can avoid having to merge changes from many branches.

+1 vote

Perhaps my git workflow is wrong.

I have committed numerous times in order to complete a task, but when the code is to be reviewed, Id like to show a non-contiguous view of my changes, which do not include the commits other developers have made. Is this possible?

Or should I be creating a branch and showing the differences from the master and my branch when it comes to a code review?

+1 vote

I wanted to avoid push if any of the files is deleted from the local git clone area. Can anyone please help me with that?

I am using Stash for repository management.

...