03 Git Immersion

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

GIT/GERRIT TRAINING

GIT/Gerrit know-how
GIT training agenda

 Introduction
 Setup & First commit
 Three states & Basic workflow
 History and old revisions
 Fix mistakes
 Undoing local changes
 Undoing staged changes
 Undoing committed changes
 Amending changes
 Git internals
 Branching
 Resolving conflicts
 Merge
 Rebase
 Cherry-pick
 Rewriting history
 Multiple repositories
 Tips & tricks

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 2


GIT training – Multiple repositories

 Up to this point we have been working with a single git repository. However,
git excels at working with multiple repositories.
 These extra repositories may be stored locally, or may be accessed across
a network connection.

 Git support widespread network communication protocols (http, ssh) and


one specially designed for git (git protocol)

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 3


GIT training – Git clone (1/4)

 If a project has already been set up in a central repository, the git clone
command is the most common way for users to obtain a development
copy.
 Like git init, cloning is generally a one-time operation - once a
developer has obtained a working copy, all version control operations and
collaborations are managed through their local repository.
 Repo-To-Repo Collaboration
 It’s important to understand that Git’s idea of a ―working copy‖ is very different from the
working copy you get by checking out code from an SVN repository.
 Unlike SVN, Git makes no distinction between the working copy and the central repository—
they are all full-fledged Git repositories.
 This makes collaborating with Git fundamentally different than with SVN.
 Whereas SVN depends on the relationship between the central repository and the working
copy, Git’s collaboration model is based on repository-to-repository interaction.
 Instead of checking a working copy into SVN’s central repository, you push or pull commits
from one repository to another.

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 4


GIT training – Git clone (2/4)

 Of course, there’s nothing stopping you from giving certain Git repos
special meaning.
 For example, by simply designating one Git repo as the ―central‖ repository,
it’s possible to replicate a Centralized workflow using Git. The point is, this
is accomplished through conventions rather than being hardwired into the
VCS itself.
CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 5
GIT training – Git clone (3/4)

 Example of git clone command:


lukic@gtv02-lin-$ mkdir clone
lukic@gtv02-lin-$ cd clone/
lukic@gtv02-lin-$ git clone ../project/.git/ .
Cloning into '.'...
done.
Checking connectivity... done
lukic@gtv02-lin-$ ls -al
total 20
drwxrwxr-x 4 lukic lukic 4096 May 25 16:19 .
drwxrwxr-x 6 lukic lukic 4096 May 25 16:19 ..
drwxrwxr-x 8 lukic lukic 4096 May 25 16:19 .git
-rw-rw-r-- 1 lukic lukic 21 May 25 16:19 Readme.txt
drwxrwxr-x 2 lukic lukic 4096 May 25 16:19 src
lukic@gtv02-lin-$ git branch -a
* dev-lukic
remotes/origin/HEAD -> origin/dev-lukic
remotes/origin/dev-lukic
remotes/origin/dev-lukic2
remotes/origin/dev-lukic3
remotes/origin/master

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 6


GIT training – Git clone (4/4)

 Let’s review cloned repository:


lukic@gtv02-lin-$ git tree --all
* bf689d7 - (50 minutes ago) First commit on dev-lukic3 branch - Nemanja Lukic (HEAD, origin/dev-lukic, origin/HEAD
| * 74c17af - (50 minutes ago) First commit on dev-lukic3 branch - Nemanja Lukic (origin/dev-lukic3)
|/
| * 5567d14 - (14 hours ago) First commit on dev-lukic2 branch - Nemanja Lukic (origin/dev-lukic2)
|/
* a0ec4c3 - (15 hours ago) Second commit on dev-lukic branch - Nemanja Lukic
* 22de96b - (17 hours ago) First commit on dev-lukic branch - Nemanja Lukic
* fe6efcc - (17 hours ago) Adding Readme file - Nemanja Lukic (origin/master)
* 756aa57 - (18 hours ago) Moved main.c to src directory - Nemanja Lukic
* 459273f - (18 hours ago) My fourth commit - Nemanja Lukic
* 9492649 - (20 hours ago) My third commit - Nemanja Lukic (tag: v1)
* 6cfaf1c - (28 hours ago) My second commit - Nemanja Lukic (tag: v1-beta)
* 4ae7eb5 - (29 hours ago) My first commit - Nemanja Lukic

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 7


GIT training – What is origin?

 Run git remote


lukic@gtv02-lin-$ git remote
origin

 Then, run git remote show <remote>


lukic@gtv02-lin-$ git remote show origin
* remote origin
Fetch URL: /home/lukic/git/clone/../project/.git/
Push URL: /home/lukic/git/clone/../project/.git/
HEAD branch: dev-lukic
Remote branches:
dev-lukic tracked
dev-lukic2 tracked
dev-lukic3 tracked
master tracked
Local branch configured for 'git pull':
dev-lukic merges with remote dev-lukic
Local ref configured for 'git push':
dev-lukic pushes to dev-lukic (up to date)

 Now we see that the remote repository ―origin‖ is simply the original
repository. Remote repositories typically live on a separate machine,
possibly a centralized server. As we can see here, however, they can just
as well point to a repository on the same machine. There is nothing
particularly special about the name ―origin‖, however the convention is to
use the name ―origin‖ for the primary centralized repository (if there is one).
CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 8
GIT training – Remote branches (1/2)

 Remote branches, are branches from remote repository. Now git branch
-a command makes a lot of sense:
lukic@gtv02-lin-$ git branch -a
* dev-lukic
remotes/origin/HEAD -> origin/dev-lukic
remotes/origin/dev-lukic
remotes/origin/dev-lukic2
remotes/origin/dev-lukic3
remotes/origin/master
[~/git/clone]
lukic@gtv02-lin-$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = /home/lukic/git/clone/../project/.git/
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "dev-lukic"]
remote = origin
merge = refs/heads/dev-lukic

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 9


GIT training – Remote branches (2/2)

 Lets create ―local‖ branch that follows ―remote‖ branch:


lukic@gtv02-lin-$ git checkout -b local_dev-lukic3 origin/dev-lukic3
Branch local_dev-lukic3 set up to track remote branch dev-lukic3 from origin.
Switched to a new branch 'local_dev-lukic3'

 It is good practice to name local branches with prefix that has ―local_‖ in
the name.
 From now on, this branch has the same functionality as any other.

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 10


GIT training – Push (1/2)

 When patch is ready on local branch, next logical step is to push it to


(central) remote repository.
 This is done using git push command:
lukic@gtv02-lin-$ vi src/main.c
lukic@gtv02-lin-$ git add src/main.c
lukic@gtv02-lin-$ git commit -m "First commit on local branch"
[local_dev-lukic3 f1fe903] First commit on local branch
1 file changed, 1 insertion(+)
lukic@gtv02-lin-$ git push origin local_dev-lukic3:dev-lukic3
Counting objects: 7, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 388 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To /home/lukic/git/clone/../project/.git/
74c17af..f1fe903 local_dev-lukic3 -> dev-lukic3

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 11


GIT training – Push (2/2)

 Always use git push in following form:


git push <repository> <local>:<remote>
 Never run just: git push (without other parameters)
 If <remote> don’t exist it will be created

 Where previous commit landed?


 Lets check in the
lukic@gtv02-lin-$ original repository:
cd ../project/
lukic@gtv02-lin-$ git checkout dev-lukic3
Switched to branch 'dev-lukic3'
lukic@gtv02-lin-$ git hist dev-lukic3
* f1fe903 2014-05-25 | First commit on local branch (HEAD, dev-lukic3) [Nemanja Lukic]
* 74c17af 2014-05-25 | First commit on dev-lukic3 branch [Nemanja Lukic]
* a0ec4c3 2014-05-25 | Second commit on dev-lukic branch [Nemanja Lukic]
* 22de96b 2014-05-24 | First commit on dev-lukic branch [Nemanja Lukic]
* fe6efcc 2014-05-24 | Adding Readme file (master) [Nemanja Lukic]
* 756aa57 2014-05-24 | Moved main.c to src directory [Nemanja Lukic]
* 459273f 2014-05-24 | My fourth commit [Nemanja Lukic]
* 9492649 2014-05-24 | My third commit (tag: v1) [Nemanja Lukic]
* 6cfaf1c 2014-05-24 | My second commit (tag: v1-beta) [Nemanja Lukic]
* 4ae7eb5 2014-05-24 | My first commit [Nemanja Lukic]

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 12


GIT training – Pull

 When patch is merged on remote branch, logical step is to update your


remote branch with this update (keep it up-to-date).
 This is done using git pull command:
lukic@gtv02-lin-$ git pull origin dev-lukic3
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From /home/lukic/git/clone/../project/
* branch dev-lukic3 -> FETCH_HEAD
Updating f1fe903..16da2cd
Fast-forward
src/main.c | 1 +
1 file changed, 1 insertion(+)

 Always use git pull in following form:


git pull <repository> <remote>

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 13


Git training – Delete remote branches

 You can delete branches on remote repository, but please be careful


because that operation is irreversible

 Deleting remote branches is done with following form of git push


command:

git push --delete <repository> <remote>

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 14


Git training – Managing remote repositories

 Beside origin repository you can work with other remote repositories

 To add new remote repository to your local track use following command:

git remote add <repository> <repository_url>

 To remove remote repository from your local track use following command:

git remote rm <repository>

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 15


Git training – Fetch (1/2)

 git pull perform update of remote references and merge them to local
tracking branches

 You can do only update of remote references and merge it manually with
git fetch

git fetch <repository>

 git fetch is necessary when you add new remote repository, because
adding repository don’t download remote references

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 16


Git training – Fetch (2/2)

lukic@gtv02-lin-$ git fetch origin


• remote: Counting objects: 382, done.
• remote: Compressing objects: 100% (203/203), done.
• remote: Total 278 (delta 177), reused 103 (delta 59)
• Receiving objects: 100% (278/278), 4.89 MiB | 539 KiB/s, done.
• Resolving deltas: 100% (177/177), completed with 40 local objects.
• From ssh://longair@pacific.mpi-cbg.de/srv/git/fiji
• 3036acc..9eb5e40 debian-release-20081030 -> origin/debian-release-20081030
• * [new branch] debian-release-20081112 -> origin/debian-release-20081112
• * [new branch] debian-release-20081112.1 -> origin/debian-release-20081112.1
• 3d619e7..6260626 master -> origin/master
lukic@gtv02-lin-$ git merge origin/master

 Branches advancing

 New remote branches

 git fetch may also fetch new tags if they have appeared in the remote
repository.

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 17


GIT training agenda

 Introduction
 Setup & First commit
 Three states & Basic workflow
 History and old revisions
 Fix mistakes
 Undoing local changes
 Undoing staged changes
 Undoing committed changes
 Amending changes
 Git internals
 Branching
 Resolving conflicts
 Merge
 Rebase
 Cherry-pick
 Rewriting history
 Multiple repositories
 Tips & tricks

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 18


GIT training – Other useful commands

 Deleting branches:
git branch -D <branch name>

 Dry run - Allows you to run command, in ―try‖ mode, before actually doing
it. It can be added to some git commands:
git push origin dev:remote --dry-run

 git blame <file> command - Can be used to see who and when
(which commit) changed the file in the repository (per line)
f2af00bf (Ian Rickards 2009-04-21 17:32:36 -0400 29) #ifdef HAVE_CONFIG_H
f2af00bf (Ian Rickards 2009-04-21 17:32:36 -0400 30) #include <config.h>
f2af00bf (Ian Rickards 2009-04-21 17:32:36 -0400 31) #endif
f2af00bf (Ian Rickards 2009-04-21 17:32:36 -0400 32)
78faaa58 (Jonathan Morton 2009-06-03 10:43:41 -0400 33) #include <string.h>
10aa3231 (Siarhei Siamashka 2009-06-27 11:56:38 +0300 34) #include "pixman-private.h"
c1e8d453 (Siarhei Siamashka 2010-03-22 18:51:54 +0200 35) #include "pixman-arm-common.h"
f2af00bf (Ian Rickards 2009-04-21 17:32:36 -0400 36)
c1e8d453 (Siarhei Siamashka 2010-03-22 18:51:54 +0200 37) PIXMAN_ARM_BIND_FAST_PATH_SRC_DST (neon, src_8888_
c1e8d453 (Siarhei Siamashka 2010-03-22 18:51:54 +0200 38) uint32_t, 1, ui

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 19


GIT training – Commit message rules (1/2)

 Very important for open source community is to have consistent


repositories, with consistent commits, easily readable and formatted well
(especially in command line environment).

 Following rules are implied:


 Commit message must look like this:
ID: blah blah blah ......

Description here. Description here. Description here.


Description here. Description here. Description here.
Description here. Description here. Description here.
Description here. Description here. Description here.
 Where the ID is something unique to the commit (e.g. architecture type for optimization
based commits, module, file name, …)
 First line in commit message must be less than 80 characters and has a blank line after it.

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 20


GIT training – Commit message rules (2/2)

 Example (from Pixman repository):


commit 89662adf77c69c3f71ded9cd8818ac5626b68451
Author: Søren Sandmann Pedersen <ssp@redhat.com>
Date: Fri Oct 18 16:39:38 2013 -0400

pixman-combine32.c: Fix bugs related to integer promotion

In the component alpha part of the PDF_SEPARABLE_BLEND_MODE macro, the


expression ~RED_8 (m) is used. Because RED_8(m) gets promoted to int
before ~ is applied, the whole expression typically becomes some
negative value rather than (255 - RED_8(m)) as desired.

Fix this by using unsigned temporary variables.

This reduces the number of failures in pixel-test to 363.

 This makes traversing through repository, with one-line log, much more
efficient:
git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 21


GIT training – Git on Windows

 http://msysgit.github.io/

CONFIDENTIAL – Reproduction prohibited without the prior permission of RT-RK 22


Contact us

RT-RK Institute for Computer Based Systems


Narodnog fronta 23a
21000 Novi Sad
Serbia

www.rt-rk.com
info@rt-rk.com

You might also like