YADB

Dave's blog.

 


dave.lobban@SOR02-LAP MINGW64 ~/src/testgit (master)
$ cd ..


##Make the directory we're going to use

dave.lobban@SOR02-LAP MINGW64 ~/src
$ mkdir testgit2


dave.lobban@SOR02-LAP MINGW64 ~/src
$ cd testgit2


##Populate a file with some data

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2
$ printf "initial value:set in master" > testfile.txt


##Stage the file

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2

$ git add testfile.txt
fatal: Not a git repository (or any of the parent directories): .git


##Oops forgot to make it a git repo. Let's do that

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2
$ git init
Initialized empty Git repository in C:/Users/dave.lobban/src/testgit2/.git/

##Stage the file


dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git add testfile.txt


##Commit the file

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git commit -m "Initial commit"
[master (root-commit) b32a1f7] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 testfile.txt


##Create a branch. Checkout -b creates the branch and moves to that branch. Notice the branch name in parentheses changes

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git checkout -b feature-b2
Switched to a new branch 'feature-b2'


##Update the file so we can track which change was made in which branch

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (feature-b2)
$ printf "Set in b2" > testfile.txt


##Commit the second change. As the file is known to git commit-am stages and commits in one command

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (feature-b2)
$ git commit -am "Second commit"
[feature-b2 a6726a7] Second commit
1 file changed, 1 insertion(+), 1 deletion(-)

##Change back to master. Omit the '-b' we used in the last 'checkout' as we are not creating the master branch


dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (feature-b2)
$ git checkout master
Switched to branch 'master'

##Let's check what's in the file now. It's the value that was set in master first time round


dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ tail testfile.txt
initial value:set in master

##Merge the second branch
dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git merge feature-b2
Updating b32a1f7..a6726a7
Fast-forward
testfile.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)


##Let's check what's in the file now. It's the value that was set in the branch (second edit)

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ tail testfile.txt
Set in b2

##Go back to the branch
dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git checkout feature-b2
Switched to branch 'feature-b2'


##Update the file

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (feature-b2)
$ printf "Set in b2(second time)" > testfile.txt


##Verify the change was made

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (feature-b2)
$ tail testfile.txt
Set in b2(second time)
dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (feature-b2)

##Commit to the branch
$ git commit -am "Third commit(in b2)"
[feature-b2 a6b1059] Third commit(in b2)
1 file changed, 1 insertion(+), 1 deletion(-)


##Show history

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (feature-b2)
$ git log
commit a6b105987e1abbb5b6eb6b9d56cd5e6f634ad6ef
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:20:00 2017 +0100


Third commit(in b2)


commit a6726a7f21f909ad0573fa6a51a669f8a7a9eff3
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:18:26 2017 +0100


Second commit


commit b32a1f78df71d4a4ff672f5b66178b260357c7b1
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:17:46 2017 +0100


Initial commit


##Back to master

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (feature-b2)
$ git checkout master
Switched to branch 'master'


##Show history. Shows only the history for master, including merges. Remember "Second commit" was committed in the branch and merged in. But we have not merged "Third commit(in b2)"

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git log
commit a6726a7f21f909ad0573fa6a51a669f8a7a9eff3
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:18:26 2017 +0100


Second commit


commit b32a1f78df71d4a4ff672f5b66178b260357c7b1
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:17:46 2017 +0100


Initial commit


##Let's set up a merge conflict by editing the file in master and then trying to merge in from the branch.

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ printf "Set in master(second edit in master; fourth edit overall)" > testfile.txt


##Verify the change was made

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ tail testfile.txt
Set in master(second edit in master; fourth edit overall)

##Check status: an uncommitted change
dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)


modified: testfile.txt


no changes added to commit (use "git add" and/or "git commit -a")

##Let's try a merge when we have an uncommitted change in master


dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git merge feature-b2
Updating a6726a7..a6b1059
error: Your local changes to the following files would be overwritten by merge:
testfile.txt
Please commit your changes or stash them before you merge.
Aborting


##What now? Either commit to master, stash or undo (git clean -f).

##Ok, let's commit the change and then merge in .

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git commit -am "Fourth commit (in master)"
[master 9141124] Fourth commit (in master)
1 file changed, 1 insertion(+), 1 deletion(-)

##We can re-try the merge now.

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git merge feature-b2
Auto-merging testfile.txt
CONFLICT (content): Merge conflict in testfile.txt
Automatic merge failed; fix conflicts and then commit the result.

##Hm, a merge conflict. You can resolve this in VS, vim or the mergetool or your choice (in my case WinMerge). Notice the branch name is (..|MERGING)

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master|MERGING)
$ git mergetool
Merging:
testfile.txt

##Launching the merge tool freezes execution of this session. At this point WinMerge loads and I edit the files as I require. When I close WinMerge the bash session unfreezes.

Normal merge conflict for 'testfile.txt':
{local}: modified file
{remote}: modified file

##Notice the branch name is still (..|MERGING). Let's check the status. Conflicts fixed but still merging. The two new files are backups created by WinMerge.

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master|MERGING)
$ git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)

Changes to be committed:

modified: testfile.txt

Untracked files:
(use "git add <file>..." to include in what will be committed)

testfile.txt.bak
testfile.txt.orig

##To indicate that I have resolved the merge conflict I have to commit the change

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master|MERGING)
$ git commit -am "Merged from b2 into master"
[master 9feb884] Merged from b2 into master

##Let's see the history

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git log
commit 9feb8841b8dd5d03276c8d0760bd7f22eb484955
Merge: 9141124 a6b1059
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:47:45 2017 +0100

Merged from b2 into master

commit 914112473b1c78225cf82ea08e65df3971b83dcd
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:37:16 2017 +0100

Fourth commit (in master)

commit a6b105987e1abbb5b6eb6b9d56cd5e6f634ad6ef
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:20:00 2017 +0100

Third commit(in b2)

commit a6726a7f21f909ad0573fa6a51a669f8a7a9eff3
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:18:26 2017 +0100

Second commit

commit b32a1f78df71d4a4ff672f5b66178b260357c7b1
Author: dave.lobban <dave.lobban@sorted.com>
Date: Fri Jun 9 14:17:46 2017 +0100

Initial commit

##Let's see the branching visually using the lg alias (see the New Tech starter checklist). This looks better in the terminal as the font is non-proportional . But you should see how the branches interact.

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git lg
* 9feb884 | (HEAD -> master) Merged from b2 into master (2 minutes ago) [dave.lobban]
|\
| * a6b1059 | (feature-b2) Third commit(in b2) (30 minutes ago) [dave.lobban]
* | 9141124 | Fourth commit (in master) (12 minutes ago) [dave.lobban]
|/
* a6726a7 | Second commit (31 minutes ago) [dave.lobban]
* b32a1f7 | Initial commit (32 minutes ago) [dave.lobban]

##What about those surplus files?

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ ls
testfile.txt testfile.txt.bak testfile.txt.orig

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

testfile.txt.bak
testfile.txt.orig

nothing added to commit but untracked files present (use "git add" to track)

##Let's clean the folder. This will remove files that have never been staged.

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git clean -f
Removing testfile.txt.bak
Removing testfile.txt.orig

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$ git status
On branch master
nothing to commit, working tree clean

dave.lobban@SOR02-LAP MINGW64 ~/src/testgit2 (master)
$