开发者

How to discard all changes made to a branch?

I'm working in a branch (i.e. design) and I've made a number of changes, but I need to discard them all and reset it to match the repository version. I thought git checkout design would do it, but it just tells me I'm already in branch design and that I have 3 modified files.

How would I discard those changes and get the branch as it stands now on t开发者_运维问答he remote server?


Note: You CANNOT UNDO this.

Try git checkout -f this will discard any local changes which are not committed in ALL branches and master.


git reset --hard can help you if you want to throw away everything since your last commit


git diff master > branch.diff
git apply --reverse branch.diff


If you don't want any changes in design and definitely want it to just match a remote's branch, you can also just delete the branch and recreate it:

# Switch to some branch other than design
$ git br -D design
$ git co -b design origin/design            # Will set up design to track origin's design branch


When you want to discard changes in your local branch, you can stash these changes using git stash command.

git stash save "some_name"

Your changes will be saved and you can retrieve those later,if you want or you can delete it. After doing this, your branch will not have any uncommitted code and you can pull the latest code from your main branch using git pull.


@Will, git immersion is a really nice and simple git tutorial. it will show you how to undo changes for the following cases: unstaged, staged and committed. labs 14-18


In the source root: git reset ./ HEAD <--un-stage any staged changes git checkout ./ <--discard any unstaged changes


REVERSIBLE Method to Discard All Changes:

I found this question after making a merge and forgetting to checkout develop immediately afterwards. You guessed it: I started modifying a few files directly on master. D'Oh! As my situation is hardly unique (we've all done it, haven't we ;->), I'll offer a reversible way I used to discard all changes to get master looking like develop again.

After doing a git diff to see what files were modified and assess the scope of my error, I executed:

git stash
git stash clear

After first stashing all the changes, they were next cleared. All the changes made to the files in error to master were gone and parity restored.

Let's say I now wanted to restore those changes. I can do this. First step is to find the hash of the stash I just cleared/dropped:

git fsck --no-reflog | awk '/dangling commit/ {print $3}'

After learning the hash, I successfully restored the uncommitted changes with:

git stash apply hash-of-cleared-stash

I didn't really want to restore those changes, just wanted to validate I could get them back, so I cleared them again.

Another option is to apply the stash to a different branch, rather than wipe the changes. So in terms of clearing changes made from working on the wrong branch, stash gives you a lot of flexibility to recover from your boo-boo.

Anyhoo, if you want a reversible means of clearing changes to a branch, the foregoing is a less dangerous way in this use-case.


git checkout -f

This is suffice for your question. Only thing is, once its done, its done. There is no undo.


For others, if you want to just revert changes on a single file:

git restore <fileName>


List to work on this

git checkout -f

git reset --hard

git reset --hard HEAD^

To remove a newly added file do the following command:-

git reset HEAD < file >

This will work for both modified file and newly added file.


So, there's a number of legacy answers here. I'm going to show you how I start over on a branch in 2021:

When you have made dozens of commits and dozens of files changed and you need to reset

git checkout master
git pull origin master
git checkout -b feat-foo-v2 # make a second version of feat-foo branch

now you have a fresh branch. But you likely still did a bunch of work that is still good, you just need to pull in those files. While in your root git directory:

git checkout feat-foo -- path/to/file/to/be/used.java

Now you have a copy of the individual file from the old branch. Do this a few more times, and the old branch will be obsolete. Now you can feel free to delete that branch, and rename feat-foo-v2 to feat-foo.

Let's say you have a file with some changes, and you only want to pull in some of those changes. I suggest getting familiar with the --patch option on git checkout:

git checkout -p feat-foo -- path/to/file.java

will open up a dialog that allows you to select the parts of the changes to the file you want to keep.

When you can't just make a new branch For some reason, you're just enamored with your feature branch and you're unwilling or unable to give it up. You know you need to reset a few files, but you shouldn't need to reset the whole thing. Creating a whole new branch was actually just an un-necessary step. We can pull the fresh files from master:

git checkout master -- path/to/file.java

and now a file is reset! And if you just want to reset part of a file, --patch should work the same way.


If you want to redo/re-do all the changes on your branch:

git pull origin master --rebase # or, denote the latest "base" or "master" commit on your branch
git push
git reset --soft origin/<current branch name>
# re-evaluate all your changes, tweaking them at will
git reset --soft origin/master
# commit your tweaks, push


How about rm -rf <working dir> followed by git clone <repo>.git

After reading all these suggestions, its exactly what I ended up doing and it worked perfectly!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜