<<

Overview basics Git expertise

Git A GNU Alternative to Bitkeeper

Mohamed Barakat

University of Kaiserslautern

ITWM Kaiserslautern, January 2010

Mohamed Barakat Git Overview Git basics Git expertise

1 Git basics The Git configuration file Create a Git-repository Using a Git-repository

2 Git expertise Branching Pulling Merging and Cherry-Picking

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Overview

1 Git basics The Git configuration file Create a Git-repository Using a Git-repository

2 Git expertise Branching Pulling Merging and Cherry-Picking

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Git working copy and Git-repsoitory

Git is a distributed SCM Git is a distributed Management system (SCM), i.e. each Git working copy sits on top of its own local Git repository located in a single top-level subdirectory .git. SVN is in contrast to Git a centralized SCM system, i.e. the SVN-repository is on exactly one server. Git allows you to , checkout, reset, etc. without contacting a server!

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository The configuration file

˜/.gitconfig Create a Git configuration file vi ˜/.gitconfig [svn] authorsfile = .git/info/svn-authors [color] = auto status = auto branch = auto [user] name = Firstname Lastname email = [email protected]

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Create a Git-repository (quick version)

init, add, commit Create a Git-repository in the directory XY: 1 cd XY 2 git init 3 git add . 4 git commit -a

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Create a Git-repository (elaborate version)

init, status, add, commit Create a Git-repository in the directory XY: 1 cd XY 2 git init 3 git status Specify files and directories to ignore (understands wildcards): 4 vi .gitignore 5 git add . 6 git commit -a Give a name to the repository: 7 vi .git/description

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Clone a local Git-repository

clone Clone a local Git-repo of the project abc in the directory git.repos: 1 cd git.repos 2 git clone /path_to_local_directory/abc The Git-repo is now under abc.

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Clone a remote Git-repository (using the Git-Protokoll)

clone git:// Clone a remote Git-repo of the project abc in the directory git.repos: 1 cd git.repos 2 git clone git://remote.host/path/abc The Git-repo is now under abc.

Requires a running Git-server on the remote host. Git supports HTTP in addition to its own protocol. CAUTION: Git over HTTP is slow for huge projects.

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Clone a remote Git-repository (via SSH)

clone git+ssh:// Clone a remote Git-repo of the project abc in the directory git.repos: 1 cd git.repos 2 git clone git+ssh://[email protected]/path/abc The Git-repo is now under abc.

Requires an SSH-account on the remote host.

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Clone a remote SVN-repository (via SSH)

clone svn+ssh:// Clone a remote SVN-repo of the project cba in the directory git.repos: 1 cd git.repos 2 git svn clone svn+ssh://[email protected]/path/cba The Git-repo is now under cba.

Requires an SSH-account on the remote host.

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Working copy, index, repo

The notions working copy and repository are known to SVN users.The index is a powerful new concept in Git, although for newcomers rather confusing. It allows a sort of “pre-commit”. add The Git-option add is used to pre-commit in the index. There are several ways to use the option add: git add file1 subdirectory1/*. subdirectory2/file2 git add . git add -a or the cool mode git add -p file1 subdirectory1/*.c subdirectory2/file2 git add -p

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Getting help

To learn more about a Git-option xyz invoke:

git xyz -h or man git-xyz

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Status of working tree, index, repo

To learn about the current status of your working copy, index, and repository invoke the following command inside any subdirectory of your repo: status git status

# On branch master nothing to commit (working directory clean)

By repository we sometimes mean the HEAD of the repository.

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Status of working copy, index, repo

How things look like when working copy, index, and repo all differ: git status # On branch master # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # modified: basic.h # modified: basic.c # # Changed but not updated: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working dir) # # modified: basic.c # modified: core.c # # Untracked files: # (use "git add ..." to include in what will be committed) # # modified: advanced.c

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Working tree, index, HEAD of repo

“git status” suggested two further Git-options: reset file git reset HEAD basic.h or simply git reset basic.h Resets the index to the HEAD of the repository, leaving the working copy untouched! So: git reset basic.h = (git add basic.h)−1

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Working tree, index, HEAD of repo

checkout file git checkout basic.c Resets the working copy of the file basic.c to the index (and not to the HEAD of the repository!), discarding all changes since the last “git add”.

In order to reset the working copy to the HEAD invoke checkout HEAD file git checkout HEAD basic.c Resets the working copy of the file basic.c and the index entry to the HEAD of the repository, discarding all changes since the last “git commit”.

Mohamed Barakat Git Overview The Git configuration file Git basics Create a Git-repository Git expertise Using a Git-repository Working tree, index, HEAD of repo

Another difference to SVN: commit git commit commits the index only, and not the working copy! The index becomes the new HEAD. while commit -a git commit -a is the classical commit of the working copy. This is the same as: 1 git add -a 2 git commit

Mohamed Barakat Git Overview Branching Git basics Pulling Git expertise Merging and Cherry-Picking Overview

1 Git basics The Git configuration file Create a Git-repository Using a Git-repository

2 Git expertise Branching Pulling Merging and Cherry-Picking

Mohamed Barakat Git Overview Branching Git basics Pulling Git expertise Merging and Cherry-Picking The tree

Each Git working copy lives over a Git repository. A Git repository is a tree with an initial point. A vertex of this tree is called a commit. A Git repository has no , i.e. even if the user distinguishes a branch (e.g. by calling it “master”) Git handles all branches of its tree on an equal footing! The different Git-repositories of the different users worldwide can be regarded as a single super tree, provided they share the same initial commit. In this super-tree, branches are distinguished not by their names, but by the commit-id’s of their commits. A commit-id of a commit is a SHA string which encodes the commit and all preceding commits in the branch.

Mohamed Barakat Git Overview Branching Git basics Pulling Git expertise Merging and Cherry-Picking Branching

branch, checkout git branch devel creates a new branch named “devel” (without “switching to it”). The branch is forked from HEAD. The command git checkout devel is used to “switch” to the branch devel (at any time), provided the index is clean, i.e. no files were added to the index since the last “git commit”.

checkout -b The following combines the two previous commands in one step: git checkout -b devel

Mohamed Barakat Git Overview Branching Git basics Pulling Git expertise Merging and Cherry-Picking View branch

log To view the current branch. git log To view the branch “experiment” git log experiment

Mohamed Barakat Git Overview Branching Git basics Pulling Git expertise Merging and Cherry-Picking Pulling

To pull Frank’s “devel”-branch of the abc project from the remote computer other.host invoke pull 1 Ensure a clean working copy, i.e. no further changes since the last “git commit -a”. create a new branch for pulling Frank’s “devel”-branch, unless you already have one 2 git branch frank 3 git checkout frank 4 git pull [email protected]/˜frank/repos.git/abc devel

Mohamed Barakat Git Overview Branching Git basics Pulling Git expertise Merging and Cherry-Picking Merging

If you like Frank’s work and want to it with your “devel”-branch do merge 1 git checkout devel 2 git merge frank

Mohamed Barakat Git Overview Branching Git basics Pulling Git expertise Merging and Cherry-Picking Cherry-Picking

If you want a certain commit from Frank’s branch do cherry-pick 1 git checkout devel 2 git cherry-pick ecc68 where ecc68 is the (unique) beginning of the SHA commit-id of Frank’s desired commit.

Mohamed Barakat Git