Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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

share|improve this question

marked as duplicate by Andrew C, random, bahrep, ArtOfWarfare, Mad Physicist Dec 2 '14 at 17:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
@AndrewC - The duplicate you linked to is itself a duplicate, plus newer than the duplicate I linked to, which is protected rather than closed a duplicate. – ArtOfWarfare Dec 2 '14 at 16:26
    
@ArtOfWarfare - The duplicate you linked to was specific to untracked files (git clean), the other one included both needed steps (git reset and git clean). Whomever closed them as duplicates originally might not have understood the distinction. – Andrew C Dec 2 '14 at 17:34

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
:Thank you for you reply. what I mean on 2nd part is to keep any changes or new files I did in the 'testing' branch I was and when I switch to another 'testing' branch those changes and untracked files don't exist, so I can create new stuff there. So basicly each time my working dir will be same as my last commit. – Lykos Dec 2 '14 at 11:37
    
You can do the reset on your current branch and then git checkout -b new_testing_branch to branch from your current commit. – Chasmo Dec 2 '14 at 11:47
1  
git clean -f -d # remove untracked files – CodingDefined Dec 2 '14 at 11:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.