Gitlab-training

From eBabel wiki
Jump to: navigation, search

Summary

I attended some excellent gitlab.com training in Utrecht, where we went through a wide range of git operations, from beginning to advanced operations.

Most of the examples below are from a new git-scratch repository (https://gitlab.com/dgapitts/git-scratch).

I have written up some basic and immediate operations like git merge. In the git-scratch example I am using a "master branch" shopping list with updates from Dave and Nadjib branches.

I will also cover some of the more advanced operations like git rebase and git cherry-pick which we went through in the class.

Basic git command line operations

git status, git add and git commit

1) working on git-scratch repo, from master branch create a shopping-list.txt with updates from Dave and Nadjib branches

   ~/project/git-scratch $ git status
   # On branch master
   nothing to commit (working directory clean)
   ~/project/git-scratch $ vi shopping-list.txt
   ~/project/git-scratch $ git add shopping-list.txt
   ~/project/git-scratch $ git commit -m 'initial shopping list created on master branch'
   [master 21cdbf8] initial shopping list created on master branch
    1 file changed, 3 insertions(+)
    create mode 100644 shopping-list.txt

git branch and checkout

Note I have merged the branch and checkout operations with "checkout -b"

2) some updates from Nadjib branch which are committed

   ~/project/git-scratch $ git checkout -b 'Updates-by-Nadjib'           <<< note the branch name is actually case insensitive
   Switched to a new branch 'Updates-by-Nadjib'                                 better to always use lower case
   ~/project/git-scratch $ git status
   # On branch Updates-by-Nadjib
   nothing to commit (working directory clean)
   ~/project/git-scratch $ vi shopping-list.txt
   ~/project/git-scratch $ git status
   # On branch Updates-by-Nadjib
   # 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:   shopping-list.txt
   #
   no changes added to commit (use "git add" and/or "git commit -a")
   ~/project/git-scratch $ git add shopping-list.txt
   ~/project/git-scratch $ git commit -m 'updates from Nadjib - on his branch'
   [Updates-by-Nadjib 8fe1977] updates from Nadjib - on his branch
    1 file changed, 2 insertions(+)
   ~/project/git-scratch $ git status
   # On branch Updates-by-Nadjib
   nothing to commit (working directory clean)

3) some updates from Dave branch which are committed

   ~/project/git-scratch $ git checkout -b 'updates-by-dave'
   Switched to a new branch 'updates-by-dave'
   ~/project/git-scratch $ vi shopping-list.txt
   ~/project/git-scratch $ git add shopping-list.txt
   ~/project/git-scratch $ git commit -m 'updates from Dave - on his branch'
   [updates-by-dave 808e8fc] updates from Dave - on his branch
    1 file changed, 2 insertions(+), 2 deletions(-)

git merge

4) switch back to Nadjib branch and merge updates from Dave

   ~/project/git-scratch $ git checkout Updates-by-Nadjib
   Switched to branch 'Updates-by-Nadjib'
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes
   olives
   tortellini
   ~/project/git-scratch $ git merge updates-by-dave
   Updating 8fe1977..808e8fc
   Fast-forward
   shopping-list.txt | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes
   milk
   beer
   ~/project/git-scratch $ git status
   # On branch Updates-by-Nadjib
   nothing to commit (working directory clean)
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes
   milk
   beer

Slightly more git complex command line operations

Resolving git merge conflicts

1) Here are some differences between the master, dave and nadjib branches

   ~/project/git-scratch $ git checkout 'updates-by-dave'
   Switched to branch 'updates-by-dave'
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes
   milk
   beer
   zeekraal
   
   ~/project/git-scratch $ git add shopping-list.txt
   ~/project/git-scratch $ git commit -m 'more updates from Nadjib'
   [updates-by-nadjib caa060b] more updates from Nadjib
    1 file changed, 2 insertions(+), 2 deletions(-)
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes
   olives
   tortellini
   
   ~/project/git-scratch $ git checkout master
   Switched to branch 'master'
   Your branch is ahead of 'origin/master' by 1 commit.
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes


2) while merging nadjib's updates into master is ok

   ~/project/git-scratch $ git merge updates-by-nadjib
   Updating 21cdbf8..caa060b
   Fast-forward
    shopping-list.txt | 2 ++
    1 file changed, 2 insertions(+)

3) but we hit a merge conflict problem when we merge dave's update

   ~/project/git-scratch $ git merge updates-by-dave
   Auto-merging shopping-list.txt
   CONFLICT (content): Merge conflict in shopping-list.txt
   Automatic merge failed; fix conflicts and then commit the result.
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes
   <<<<<<< HEAD
   olives
   tortellini
   =======
   milk
   beer
   zeekraal
   >>>>>>> updates-by-dave

4) manually updating the shopping-list.txt file to reflect the updats from dave and nadjib

   ~/project/git-scratch $ vi shopping-list.txt
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes
   olives
   tortellini
   milk
   beer
   zeekraal

5) you need to commit the latest changes (i.e. previous step) for the git merge

   ~/project/git-scratch $ git merge updates-by-dave
   error: 'merge' is not possible because you have unmerged files.
   hint: Fix them up in the work tree,
   hint: and then use 'git add/rm <file>' as
   hint: appropriate to mark resolution and make a commit,
   hint: or use 'git commit -a'.
   fatal: Exiting because of an unresolved conflict.
   ~/project/git-scratch $ git status
   # On branch master
   # Your branch is ahead of 'origin/master' by 4 commits.
   #
   # You have unmerged paths.
   #   (fix conflicts and run "git commit")
   #
   # Unmerged paths:
   #   (use "git add <file>..." to mark resolution)
   #
   #   both modified:      shopping-list.txt
   #
   no changes added to commit (use "git add" and/or "git commit -a")
   ~/project/git-scratch $ git add shopping-list.txt
   ~/project/git-scratch $ git commit -m "manual merge of conflicts between Dave and Nadjib"
   [master 53cbd1c] manual merge of conflicts between Dave and Nadjib
   ~/project/git-scratch $ git status
   # On branch master
   # Your branch is ahead of 'origin/master' by 6 commits.
   #
   nothing to commit (working directory clean)

Viewing git log and log chart format

The basic format is a bit hard to read:

   ~/project/git-scratch $ git log
   commit 53cbd1c406d2d74b59e06ddc8800d5eef208ea2b
   Merge: caa060b 34683e4
   Author: dgapitts <dgapitts@gmail.com>
   Date:   Sat Oct 5 09:14:16 2013 +0200
   
       manual merge of conflicts between Dave and Nadjib
   
   commit caa060bf5accf978031cbde014bbd561a027fcb6
   Author: dgapitts <dgapitts@gmail.com>
   Date:   Sat Oct 5 09:08:19 2013 +0200
   
       more updates from Nadjib
   
   commit 34683e46abb8050f0465695bf07b10c791e720fe
   Author: dgapitts <dgapitts@gmail.com>
   Date:   Sat Oct 5 09:03:27 2013 +0200
   
       more changes to our shopping list
   
   commit 808e8fcf286a80e474c5c12bc158c4c611664dcc
   Author: dgapitts <dgapitts@gmail.com>
   Date:   Sat Oct 5 08:51:09 2013 +0200
   
       updates from Dave - on his branch
   
   commit 8fe1977e6f0cd95fab180dcf2db29de78d1eafff
   Author: dgapitts <dgapitts@gmail.com>
   Date:   Sat Oct 5 08:41:22 2013 +0200
   
       updates from Nadjib - on his branch
   
   commit 21cdbf8a72b31c17a8bbb4f0bbfd13b7b7912bb3
   Author: dgapitts <dgapitts@gmail.com>
   Date:   Sat Oct 5 08:22:07 2013 +0200
   
       initial shopping list created on master branch
   
   commit 2dca23d0ee4713d2a50d3dd50c651e280c801b3d
   Author: dgapitts <dgapitts@gmail.com>
   Date:   Sat Oct 5 08:05:59 2013 +0200
   
       switch README to README.md
   
   commit 4acd342a73db63fcbe743f89cf1b186b309a7f8e
   Author: dgapitts <dgapitts@gmail.com>
   Date:   Sat Oct 5 08:03:22 2013 +0200
   
       first commit

So using a git log graph format is more compact and a lot easier to read

   ~/project/git-scratch $ git log --graph --decorate --pretty=oneline --abbrev-commit --color
   *   53cbd1c (HEAD, master) manual merge of conflicts between Dave and Nadjib
   |\
   | * 34683e4 (updates-by-dave) more changes to our shopping list
   * | caa060b (updates-by-nadjib) more updates from Nadjib
   |/
   * 808e8fc updates from Dave - on his branch
   * 8fe1977 updates from Nadjib - on his branch
   * 21cdbf8 initial shopping list created on master branch
   * 2dca23d (origin/master) switch README to README.md
   * 4acd342 first commit


git tag

annotated tag

   ~/gitlab-recipes/gitlab-recipes $ git tag -a v1.4 -m 'my version 1.4'
   ~/gitlab-recipes/gitlab-recipes $ git tag
   v1.4

regular tag

   v1.4b
   ~/gitlab-recipes/gitlab-recipes $ git log
   commit e039834ecb0bca066d3224d0acf81f6a40ca0fff
   Author: Niels Basjes <github@basjes.nl>
   Date:   Fri May 17 23:22:41 2013 +0200
   
       Minor cleanups of the CentOS_6 install guide
   
   commit b2c004158a7a7bc171f097ab20dac745af8cfbba
   Author: Niels Basjes <github@basjes.nl>
   Date:   Fri May 17 00:48:33 2013 +0200
   
       Minor textual improvements
   
   ...
   
   commit 24c43c3c20342a0d8d2a600cfb7426a2b92e2d3a
   Merge: 2f36f6d d745bf9
   Author: Nihad Abbasov <mail@narkoz.me>
   Date:   Sat May 4 05:43:07 2013 -0700
   
       Merge pull request #61 from gefd/f-https-only-redirect
   
       Fixes http redirects on https only vhost
   
   commit 2f36f6d69025251bbf8153b985e9c455e0b00d94
   Merge: 28d4905 94f0f8e
   Author: Nihad Abbasov <mail@narkoz.me>
   Date:   Sat Apr 27 05:07:35 2013 -0700
   
       Merge pull request #83 from sagikazarmark/patch-1

tagging by SHA

   ~/gitlab-recipes/gitlab-recipes $ git tag -a v1.5 -m 'with SHA' 24c43c3c203
   ~/gitlab-recipes/gitlab-recipes $ git tag
   v1.4
   v1.4b
   v1.5

pushing tags

   ~/gitlab-recipes/gitlab-recipes $ git push origin --tags
   Counting objects: 2, done.
   Delta compression using up to 8 threads.
   Compressing objects: 100% (2/2), done.
   Writing objects: 100% (2/2), 280 bytes, done.
   Total 2 (delta 0), reused 0 (delta 0)
   To https://gitlab.com/dgapitts/gitlab-recipes.git
    * [new tag]         v1.4 -> v1.4
    * [new tag]         v1.4b -> v1.4b
    * [new tag]         v1.5 -> v1.5

More advanced git command line operations

git rebase operation

git rebase abort

Continuing with the shopping list example

1) My first attempt was not quite what I want so I used the --abort option

   ~/project/git-scratch $ git rebase -i
   pick 8fe1977 updates from Nadjib - on his branch
   error: could not apply 34683e4... more changes to our shopping list
   
   When you have resolved this problem, run "git rebase --continue".
   If you prefer to skip this patch, run "git rebase --skip" instead.
   To check out the original branch and stop rebasing, run "git rebase --abort".
   Could not apply 34683e4... more changes to our shopping list
   ~/project/git-scratch $ git rebase -i  21cdbf8    
   It seems that there is already a rebase-merge directory, and
   I wonder if you are in the middle of another rebase.  If that is the
   case, please try
       git rebase (--continue | --abort | --skip)
   If that is not the case, please
       rm -fr "/Users/david.pitts/project/git-scratch/.git/rebase-merge"
   and run me again.  I am stopping in case you still have something
   valuable there.
   ~/project/git-scratch $ git status
   # Not currently on any branch.
   # You are currently rebasing.
   #   (fix conflicts and then run "git rebase --continue")
   #   (use "git rebase --skip" to skip this patch)
   #   (use "git rebase --abort" to check out the original branch)
   #
   # Unmerged paths:
   #   (use "git reset HEAD <file>..." to unstage)
   #   (use "git add <file>..." to mark resolution)
   #
   #   both modified:      shopping-list.txt
   #
   no changes added to commit (use "git add" and/or "git commit -a")
   ~/project/git-scratch $ git rebase --abort

git rebase and specific <commit number>

2) starting the rebase operation again

   ~/project/git-scratch $ git log --graph --decorate --pretty=oneline --abbrev-commit --color
   *   53cbd1c (HEAD, master) manual merge of conflicts between Dave and Nadjib
   |\
   | * 34683e4 (updates-by-dave) more changes to our shopping list
   * | caa060b (updates-by-nadjib) more updates from Nadjib
   |/
   * 808e8fc updates from Dave - on his branch
   * 8fe1977 updates from Nadjib - on his branch
   * 21cdbf8 initial shopping list created on master branch
   * 2dca23d (origin/master) switch README to README.md
   tea
   * 4acd342 first commit
   ~/project/git-scratch $ git rebase -i 21cdbf8
   error: could not apply 34683e4... more changes to our shopping list
   
   When you have resolved this problem, run "git rebase --continue".
   If you prefer to skip this patch, run "git rebase --skip" instead.
   To check out the original branch and stop rebasing, run "git rebase --abort".
   
   Could not apply 34683e4... more changes to our shopping list
   ~/project/git-scratch $ git status
   # Not currently on any branch.
   # You are currently rebasing.
   #   (fix conflicts and then run "git rebase --continue")
   #   (use "git rebase --skip" to skip this patch)
   #   (use "git rebase --abort" to check out the original branch)
   #
   # Unmerged paths:
   #   (use "git reset HEAD <file>..." to unstage)
   #   (use "git add <file>..." to mark resolution)
   #
   #   both modified:      shopping-list.txt
   #
   no changes added to commit (use "git add" and/or "git commit -a")

git rebase pick and squash operations ... more conflicts to manually resolve

3) as part of the rebase operation, I have 'picked' one commit and 'squashed' three other, this has left me with conflicts again

   ~/project/git-scratch $ git log --graph --decorate --pretty=oneline --abbrev-commit --color
   final shopping list agreed
   * 19a1d09 (HEAD) # This is a combination of 3 commits. # The first commit's message is: updates from Nadjib - on his branch
   * 21cdbf8 initial shopping list created on master branch
   * 2dca23d (origin/master) switch README to README.md
   * 4acd342 first commit
   ~/project/git-scratch $ cat shopping-list.txt
   tea
   coffee
   bran flakes
   <<<<<<< HEAD
   olives
   tortellini
   =======
   milk
   beer
   zeekraal
   >>>>>>> 34683e4... more changes to our shopping list
   ~/project/git-scratch $ vi shopping-list.txt

4) after resolving conflicts (by updating shopping-list.txt file) and the changes to the rebase operation and complete by running "rebase --continue":

   ~/project/git-scratch $ git status
   # Not currently on any branch.
   # You are currently rebasing.
   #   (fix conflicts and then run "git rebase --continue")
   #   (use "git rebase --skip" to skip this patch)
   #   (use "git rebase --abort" to check out the original branch)
   #
   # Unmerged paths:
   #   (use "git reset HEAD <file>..." to unstage)
   #   (use "git add <file>..." to mark resolution)
   #
   #   both modified:      shopping-list.txt
   #
   no changes added to commit (use "git add" and/or "git commit -a")
   ~/project/git-scratch $ git add shopping-list.txt
   ~/project/git-scratch $ git status
   # Not currently on any branch.
   # You are currently rebasing.
   #   (all conflicts fixed: run "git rebase --continue")
   #
   # Changes to be committed:
   #   (use "git reset HEAD <file>..." to unstage)
   #
   #   modified:   shopping-list.txt
   #
   ~/project/git-scratch $ git rebase --continue
   [detached HEAD 4d698c3] final shopping list agreed
    1 file changed, 5 insertions(+)
   Successfully rebased and updated refs/heads/master.

5) review new git log, with all the minor tweaks for the shopping list file being removed

   ~/project/git-scratch $ git log --graph --decorate --pretty=oneline --abbrev-commit --color
   * 4d698c3 (HEAD, master) final shopping list agreed
   * 21cdbf8 initial shopping list created on master branch
   * 2dca23d (origin/master) switch README to README.md
   * 4acd342 first commit
   ~/project/git-scratch $ git status
   # On branch master
   # Your branch is ahead of 'origin/master' by 2 commits.
   #
   nothing to commit (working directory clean)


git cherry-pick example

This is from the gitrecipe training at gitlab.com and not the git-scratch shopping list example.

Starting on the production branch:

   ~/gitlab-recipes/gitlab-recipes $ git status
   # On branch production
   # Untracked files:
   #   (use "git add <file>..." to include in what will be committed)
   #
   #    test3
   nothing added to commit but untracked files present (use "git add" to track)

switch to master branch:

   ~/gitlab-recipes/gitlab-recipes $ git checkout master
   Switched to branch 'master'
   ~/gitlab-recipes/gitlab-recipes $ git status
   # On branch master
   # Untracked files:
   #   (use "git add <file>..." to include in what will be committed)
   #
   #    test3
   nothing added to commit but untracked files present (use "git add" to track)
   ~/gitlab-recipes/gitlab-recipes $ git add test3
   ~/gitlab-recipes/gitlab-recipes $ git commit -m 'cherry pick'
   [master 235f3fa] cherry pick
    0 files changed
    create mode 100644 test3

now back on the production branch use git cherry-pick and select by SHA

   ~/gitlab-recipes/gitlab-recipes $ git checkout production
   Switched to branch 'production'
   ~/gitlab-recipes/gitlab-recipes $ git cherry-pick 235f3fa
   [production 42418a1] cherry pick
    0 files changed
    create mode 100644 test3