This question has already been answered:

I work in the main master branch. Since the last commita there are changes. I do not want to commit current changes to the current thread. But I do not want to lose them either. I want to create a new branch, say test, and continue to work in the new branch from the current moment without losing any changes. With such a calculation that when I finish in the test branch, I will return back to the main (master) and salt changes.

How to do it right?

Reported as a duplicate of Nick Volynkin 17 Feb '16 at 12:36 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Noted a double to the question, which in my opinion is absolutely about the same. If I did make a mistake, let me know, re-open. ru.stackoverflow.com/questions/437779 - Nick Volynkin

3 answers 3

Take ...

... and do it!

Git will not let you just lose the changes made over the commit, so you can safely make a new branch:

git checkout -b test 

Fearing this would be worth switching to another branch that is on a different commit (say, with git checkout dev ); Git would pop you like this:

 error: Your local changes to the following files would be overwritten by checkout: файл1 папка/файл2 Please, commit your changes or stash them before you can switch branches. Aborting 

Backup plan

There is a stash in which you can place changes that you don’t want to commit, but you want to save them for a while. In this case, the use of stash is completely unnecessary, but it can be useful, for example, when the changes were applied to the wrong branch:

 git stash -u # с сохранением untracked (неотслеживаемых) файлов git checkout dev # меняем ветку на другую, скажем, уже существующую git stash pop # забираем изменения обратно и выкидываем из стэша # ^- этот прямоугольник получился случайно, честно 

Changes from the stash may well conflict with the state of the other branch, and if this happens, a conflict will arise and the set of changes will remain in the stash (because it was impossible to apply them purely).

Stash is one for the entire repository and is specific to a single local copy: relatively speaking, it is yours personally .

    If you have all changes in the master zakommicheny, then you can safely create a new branch. It will only include changes that are in the remote wizard. When you go back to master, the custom changes will remain in place (since they were saved in local). Do not forget to pull the main thing, if the contents of the test branch are already injected into the master.

    • 2
      I do not want to commit current changes to the current branch (master). - Mikhail Danshin
    • @MikhailDanshin and what is the reason? At commit, changes are saved only locally and do not fall into remout. - andreycha

    The easiest way to git checkout -b %branch_name%

    • Why checkout ? He has the same unchanged changes. - Qwertiy
    • Changes it does not hurt - sercxjo
    • You Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>. git-scm.com/docs/git-checkout Moreover, it is possible to immediately create a branch from the current one - stck
    • Yes that's right. Mixed up. - Qwertiy