Я довольно новичок в git, и я борюсь с приведенным ниже сценарием. Я подробно опишу свой сценарий только с локальным репо, а в реальной жизни многие разработчики проталкивают / извлекают изменения у других разработчиков, прежде чем работать над новой веткой.
Сначала я создаю новое репо с начальным file:
cloudshell:~ $ mkdir test-git2
cloudshell:~ $ cd test-git2/
cloudshell:~/test-git2 $ git init
Initialized empty Git repository in /home/denis/test-git2/.git/
cloudshell:~/test-git2 $ echo "first commit" > b
cloudshell:~/test-git2 $ git add b
cloudshell:~/test-git2 $ git commit -m "first commit"
[master (root-commit) a21ac9b] first commit
1 file changed, 1 insertion(+)
create mode 100644 b
cloudshell:~/test-git2 $ git status
On branch master
nothing to commit, working tree clean
cloudshell:~/test-git2 $ cat b
first commit
cloudshell:~/test-git2 $
Первый разработчик изменяет этот файл через ветку:
cloudshell:~/test-git2 $ git checkout -b branch1
Switched to a new branch 'branch1'
cloudshell:~/test-git2 $ echo "branch1" >> b
cloudshell:~/test-git2 $ git add b
cloudshell:~/test-git2 $ git commit -m "branch1"
[branch1 571d6fe] branch1
1 file changed, 1 insertion(+)
cloudshell:~/test-git2 $ git status
On branch branch1
nothing to commit, working tree clean
cloudshell:~/test-git2 $ cat b
first commit
branch1
cloudshell:~/test-git2 $
После этого второй разработчик реализует что-то еще для этого файла:
cloudshell:~/test-git2 $ git checkout -b branch2
Switched to a new branch 'branch2'
cloudshell:~/test-git2 $ echo "branch2" >> b
cloudshell:~/test-git2 $ git add b
cloudshell:~/test-git2 $ git commit -m "branch2"
[branch2 9eb8609] branch2
1 file changed, 1 insertion(+)
cloudshell:~/test-git2 $ git status
On branch branch2nothing to commit, working tree clean
cloudshell:~/test-git2 $ cat b
first commit
branch1
branch2
cloudshell:~/test-git2 $ git branch
branch1
* branch2
master
cloudshell:~/test-git2
Позже, по любой причине, нас просят удалить изменения, сделанные в "branch1", и вот где я застрял:
- Я мог бы легко git вернуть HEAD, чтобы отменить последнюю зафиксировать, но это не то, что я хочу
- Я мог бы "go назад во времени" и git вернуть HEAD ~ 2, которые действительно отменяют мои модификации "branch1", но это также отменяет модификации "branch2", которые Я хочу сохранить. Затем я попытался выбрать вишню ветки 2, чтобы снова применить ее, но в моем файле это закончилось (я получил тот же результат с git revert):
cloudshell:~/test-git2 $ cat b
first commit
>>>>>> cd08a37... file a updated branch 2
cloudshell:~/test-git $
Может кто-то указывает мне правильное направление для достижения этого?
Спасибо,