Testing stuff with Git, especially branching
First, I want to solve issue when merge branches.
First commit
echo "Initial commit" > file1.txt
[master] git init
[master] git add -A
[master] git commit -m "first commit"
[master] git remote add origin https://github.com/dleurs/github-test-merging.git
[master] git push -u origin master
Easy merging, no change in master
Second commit, feature that will change
[master] git checkout -b "feature_1"
[feature_1] echo "Feature 1" >> file2.txt
[feature_1] git add -A; git commit -m "Feature 1"; git push origin feature_1;
Merging
[feature_1] git merge master
Already up to date.[feature_1] git checkout master
[master] git merge feature_1
Updating a9cd07bc..a5ac54e5
Fast-forward
file2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 file2.txt[master] git add -A; git commit -m "Merged feature_1"; git push origin master; On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)nothing to commit, working tree clean
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/dleurs/github-test-merging.git
Harder merging, changes in master
[master] git checkout -b "feature_2"
[feature_2] echo "Flavour feature 2" > file3.txt
[feature_2] git add -A; git commit -m "Flavour feature 2"; git push origin feature_2;
[feature_2] git checkout master
[master] echo "Flavour master feature 2" > file3.txt
[master] git add -A; git commit -m "Flavour master feature 2"; git push origin master;
Now trying to merge this, they will be a conflict
[master] git checkout feature_2
[feature_2] git merge master
Auto-merging file3.txt
CONFLICT (add/add): Merge conflict in file3.txt
Automatic merge failed; fix conflicts and then commit the result.[feature_2] git status
On branch feature_2
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)Unmerged paths:
(use "git add ..." to mark resolution)both added: file3.txt
no changes added to commit (use "git add" and/or "git commit -a")
To solve the conflict, use VSCode
In VSCode, file3.txt, Accept current change or incoming change, here current change and save
git status
On branch feature_2
Your branch is up to date with 'origin/feature_2'.You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)Unmerged paths:
(use "git add ..." to mark resolution)both added: file3.txt
git add -A; git commit -m "Merge conflict solved, accept current change";
[feature_2 485a42eb] Merge conflict solved, accept current changegit push origin feature_2
git checkout master
git merge feature_2
Updating c6d11e8c..485a42eb
Fast-forward
file3.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)no need to git add, git commit. Because : git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)git push origin master
If you do a mistake, delete folder, git clone
But no branches anymore ? git branch -a; git checkout remotes/> origin/feature_2; git checkout feature_2;
Go back into a commit :
[master] echo "A huge error in the code" > file3.txt
git add -A; git commit -m "Testing (you don't know but containing a bug)"; git push origin master;
"You discover the bug !!"
git log
commit 810fba75 (HEAD -> master, origin/master, origin/HEAD)
Author: Leurs Dimitri dimitri.leurs@gmail.com
Date: Tue Mar 3 10:01:42 2020 +0100Testing (you don't know but containing a bug)
commit 485a42eb (origin/feature_2, feature_2)
Merge: 9478a791 c6d11e8c
Author: Leurs Dimitri dimitri.leurs@gmail.com
Date: Tue Mar 3 09:56:11 2020 +0100vMerge conflict solved, accept current change
commit c6d11e8c
Author: Leurs Dimitri dimitri.leurs@nokia.com
Date: Mon Mar 2 16:25:59 2020 +0100Flavour master feature 2
NOT WORKING : git branch -f master 485a42eb
fatal: Cannot force update the current branch.NOT WORKING EITHER : git reset 485a42eb
OR git reset HEAD~1
Unstaged changes after reset:
M file3.txtgit status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)git push origin master
To https://github.com/dleurs/github-test-merging.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/dleurs/github-test-merging.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.The solution :
git log; Getting commit id of two last commit
git diff N N-1 > diff.txt
git diff 810fba75 485a42eb > diff.txtgit apply diff.txt
git add -A; git commit -m "Bug detected, going back one commit behind, with diff.txt"; git push origin master
rf -rf diff.txt
git add -A; git commit -m "Deleting diff.txt, code just like commit 485a42eb"; git push origin master