Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Undoing changes [duplicate]

Tags:

git

I'm currently trying to learn using Git within my workflow and I'd like to ask somethings how I could do them more easily.

I usually create different braches in order to do some thing and see if they work for me. When I'm switching branches how can I keep my working directory with the files and folders that has in the last commit? So for example when I switch from branch_A to master my working dir will have the folders and files of my last commit in master branch, or when I switch to branch_B my working dir will have all from last commit in branch_B, etc etc

like image 858
ltdev Avatar asked Dec 02 '14 11:12

ltdev


People also ask

How do I revert changes in git?

First, you'll need to find the ID of the revision you want to see. This assumes that you're developing on the default main branch. Once you're back in the main branch, you can use either git revert or git reset to undo any undesired changes.

What undos changes?

To undo an action, press Ctrl + Z.

Can you revert multiple commits?

To revert multiple commits in the middle of the history, use an interactive rebase. Find the last commit's hash containing all the commits you want to remove. Start an interactive rebase session with git rebase -i <hash>. In the interactive rebase edit screen, remove the commit lines you want to remove.


1 Answers

to set my working directory just how it was until my last commit, so basicly all new -untracked files and folders and all changes to existing files will no longer exist.

To reset your changes which were not commited, just hit

git reset --hard HEAD 

to revert to your last commit.

I'm not sure if I got your second part of the question right, but I will try to explain it:

If you want to keep your changes and want to apply them on another branch, combine it with Git stash.

git stash save
git reset --hard HEAD
// do what you want to do and switch branches
git stash pop
like image 65
Martin Seeler Avatar answered Oct 03 '22 18:10

Martin Seeler