How to save changes when the state is disconnected?

Working with Git, I had to go back to a specific commit. I made some changes, and now I want to fix them. What is the right way to do this?

My project is now in standached-HEAD state. Will my changes be saved if I commit using

git commit 

? Otherwise, what should I do to not lose my changes?

+6
source share
2 answers

Disclaimer: git is not complicated, it is just universal. Do not be alarmed simply because I made a long answer in a long answer :)

You had:
master: abcdef

and wanted to change c . You made:
* git checkout c (do not check commits in the future. Move the branch head instead)
* changed some files

You are at:

  master: abcdef
              \ uncommitted-work, detached

If you want to reapply def over your modified "c"

(If you clicked, people located downstream will have to recover from upward adjustment )

  • git stash .
  • git checkout master
  • git stash pop (resolve conflicts)
  • git stage .
  • git commit -m "temporary name for g"
  • ( master: abcdefg )
  • git rebase c -i ("reapply my current branch to point c and let me manipulate commits interactively", i.e. rename (rebase) def to new c )
  • Follow the interactive reboot guide . You want to reorder g , so after c, change the rebase command from pick to fixup . dd to delete the line, P to place it, i to enter insert mode to enter “fixup”, then :wq to save and exit vim.
  • ( master: ab-c'-d'-e'-f' , where c' is the result of merging g and c during rebase. def became d'-e'-f' as their origin changed, therefore they are not "" fixed before git, but their contents remain the same)

If you want to undo def (and rewrite history as if you didn’t)

(If you clicked, people located downstream will have to recover from an upward adjustment ):

  • git stash .
  • git checkout master
  • ( master: abcdef , with saved files based on c)
  • git reset --hard c (discard all files and write to master with c)
  • ( master: abc , with saved files)
  • git stash pop (resolve conflicts)
  • ( master: abc-* )
  • git stage .
  • git commit -m "description of g"
  • ( master: abcg )

If you want to undo def (but save them in history)

  • git stash
  • git revert --no-commit d
  • git revert --no-commit e
  • git revert --no-commit f
  • git push
  • git stash pop (there will be no conflicts)
  • git stage .
  • git commit -m "Undo def in order to fix..."
  • git push

If you have git push def and you want to keep them separate:

It looks like your new changes are for a new branch. git branch <foo> .

+12
source

Create a new branch from this commit, and then click commit :

 git checkout -b <branchname> git commit 

Assuming you have already installed (i.e. git add myfile1 myfile2 ) your files.

+4
source

Source: https://habr.com/ru/post/989537/


All Articles