Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de Leinelab Workshop July 28, 2015
Motivation - Why use version control? Versions in file names: does this look familiar? $ ls file file.2 file. keep file. old.2 file ~ file.20090803 file. new file. save file.1 file. bak file. old This is better than nothing, however what happened between the different versions? Which file is actually the most current? Version control is a way to keep a backup of changing files store a history of those changes and manage merging of changes in versions with different change sets. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 2
Use of version control Who uses version control? Everyone who would like to access an old version of a document. Or everyone to whom such things have happened: It would be nice to have the version from 2 hours ago... I wrote that really well three days ago. How did that go again? Oh no! I deleted the file! Where is version control used? Software development Text and document processing/writing Graphic design System administration Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 3
Use of version control (cont.) What kinds of files should be kept under version control? Any kind of file which will be changed Mainly text files Program code, documentation Theses, dissertations Configuration files Binary files also possible Graphics files:.png,.tiff Documents:.odt, (.pdf) What shouldn t be kept under version control? Automatically generated files, e.g.:.o,.log,.pdf Editor backup files, e.g.: file, file.bak Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 4
Agenda 1 Git history and design 2 Warm up (configuration and getting help) 3 Basic commands (init, status, add, commit) 4 Explore history and changes (log, diff, show) 5 Going back in time (checkout, reset) 6 Ignore, move and remove files 7 Clone a project 8 References, Part II and Thank You Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 5
Git history and concept Originally written for Linux kernel development. All Linux kernel developers used to be able to use the proprietary Bitkeeper version control system for free. In 2005 there were further restrictions put on Bitkeeper so that it wasn t as free as it used to be. Linus Torvalds was uneasy with the situation and decided to write his own tool. Basically a versioned file system The version control system is developed on top of this More than 130 commands porcelain and plumbing commands Distributed repository model can be used centrally Very fast! Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 6
Git design Distributed development Scalable up to thousands of developers Fast and efficient Maintain integrity and privacy Enforced responsibility Immutable objects Atomic operations Support and promote development with branches Complete repositories Clean design Free as in freedom Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 7
Get help git --help Also possible: man git, git help $ git -- help usage : git [-- version ] [--exec - path [= < path >]] [--html - path ] [--man - path ] [--info - path ] [-p -- paginate --no - pager ] [--no - replace - objects ] [-- bare ] [--git - dir =<path >] [--work - tree =<path >] [-- namespace =<name >] [-c name = value ] [-- help ] <command > [<args >] The most commonly used git commands are : add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List, create, or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository diff Show changes between commits, commit and working tree, etc fetch Download objects and refs from another repository grep Print lines matching a pattern init Create an empty git repository or reinitialize an existing one log Show commit logs merge Join two or more development histories together mv Move or rename a file, a directory, or a symlink [...] Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 8
Configure Git git config Important for the history of a file: Who made a change and when? git config -- global user. name " Jon Doe " git config -- global user. email " jon. doe@email. com " git config -- global core. editor " vim " git config -- global color. ui True Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 9
Create a (local) repository git init Initialize an empty Git repository $ mkdir mydir $ cd mydir / $ git init Initialized empty Git repository in / home / dog / mydir /. git / $ ls -la total 36 drwxr - xr - x 3 dog dog 4096 Jan 30 02: 27. drwx ------ 57 dog dog 24576 Jan 30 02:27.. drwxr - xr - x 7 dog dog 4096 Jan 30 02: 27. git $ git status # On branch master # # Initial commit # nothing to commit ( create / copy files and use " git add " to track ) $ Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 10
Exercise: help, config, init (10 minutes) Explore git help. Configure Git with your name and e-mail. Create a folder named git_intro in your $HOME. Initialize a Git repository in git_intro. Explore the.git subdirectory. Tell me some interesting thing you find. What happens when you rename git_intro to something else? What happens when you delete the.git subdirectory? What happens when you call git init a second time in a Git directory? Type git config --list in your $HOME directory! Type git config --list in your Git directory! Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 11
Good to know Git help works also for specific operations: git help add = man git-add git help config = man git-config git help init = man git-init... You can either use git config <parameter> or edit the.gitconfig or./.git/config text files. The same is true for many other operations Not every configuration option has (yet) a command line tool to change it Git is still being developed. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 12
How Git views content Staging area / Index Repository untracked git add staged git commit committed modified staged = indexed = cached Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 13
State of a repository git status Shows if you have untracked, modified or staged (= to be committed) files. In case of a cloned repository git status also shows if you are ahead of your remote (compared to last sync). See also: git help status Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 14
Add files to the next commit git add Adds files to the staging area. If a file is added once, Git is aware of it and will show it as modified instead of untracked if you have made a modification. A modified file is not automatically staged. You have to call git add every time you want to commit the changes. In Git add has a different meaning compared to Subversion. Here it means: Mark this file to be committed in my next snapshot. In Subversion add means: Add this file to version control. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 15
Make a snapshot git commit Put files from staging area into the repository. This command does not care about the state of the working directory! You could delete all files and still commit them as long as they were staged. In Git commit is a local operation. In Subversion commit alias ci always connects to the (central) server. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 16
Exercise: status, add, commit (10 minutes) Create two files in your Git repository: echo Red is a nice color. > red.txt echo Blue is a nice color. > blue.txt Add red.txt to the staging area Make an initial commit that contains content of red.txt. Now modify red.txt again (add a second line: It is the color of a rose. ) Call git status (one file should be modified, the other one still untracked) Now: Run git commit -a without staging anything first! What happens? Add blue.txt to the staging area. Can you unstage a file that was added with git add? Stage and commit blue.txt using: git add blue.txt git commit -m Adds blue.txt to the project. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 17
Recommendations for commits A commit should contain a single, self contained idea. The commit message should contain a short description of the idea or change being made in this commit. A one line subject line An optional text describing more details of the change. The Why of the change is important. Remember: this is a communication exercise. Commits should be as small as possible (atomic commits). After the git commit command The first line of the commit message is shown There are no revision numbers. Commits are specified via SHA1 hashes. The first part of the SHA1 hash is also shown. This can be used as a reference to the commit. What else is shown? Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 18
Good to know Extensions to bash make life easier export VISUAL = vim GIT_ PS1_ SHOWDIRTYSTATE =" true " GIT_ PS1_ SHOWSTASHSTATE =" true " GIT_ PS1_ SHOWUNTRACKEDFILES =" true " if [ -e / etc / bash_completion. d/ git ] then. / etc / bash_completion.d/ git export PS1 = '\[\033[01;32 m \]\ u@\h \[\033[00 m \]:\[\033[01;34 m \]\ w \[\033[0;31 m\]$( git_ps1 "\[\033[0;31 m\] (%s) \[\033[0;31 m \]") \[\033[00 m \]\ $ ' else export PS1 = '\[\033[01;32 m \]\ u@\h \[\033[00 m \]:\[\033[01;34 m \]\ w \[\033[00 m \]\ $ ' fi Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 19
Browse the history git log Shows previous commits (message, author, date) There is also a tig command that provides a nice ncurses interface (has to be installed separately). Summary view with git log -- oneline See diffs for a specific file: git log -p < file > Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 20
Compare versions git diff Compares different versions of (text) files. Default: Version in current working directory vs. last committed version. (other modes possible, see git help diff) Show the diff between staging area and last commit: git diff -- cached [<path >...] In other words: git diff --cached shows you what will be committed next. Show the diff between two arbitrary commits: git diff <commit > <commit > [<path >...] Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 21
Show versions from the past git show Shows Git objects (not only commits) SYNOPSIS git show [ options ] < object >... Example: git show 9 f529a : myfile. txt Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 22
Exercise: log, diff, show (5 minutes) Create green.txt and black.txt similar to the first two files. Add and commit them. Add a file README that describes the content of the directory. Make changes to red.txt (remove the second line) and show the difference with git diff. Add red.txt to the staging area and use git diff --cached to see what will be committed. Commit the change and and use the -v switch to see what s to be committed in your editor. Use git log to view the history. Use git log -p red.txt to see differences in history for that particular file. Use git show to inspect a version of red.txt from a previous commit. Use git show to get the old version back. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 23
Get older versions of a file back git checkout Checkout a branch or paths to the working tree Example 1: You have unstaged changes and want to discard them: git checkout -- myfile. txt Example 2: (only in emergency cases) Change the whole repository back to a certain commit git checkout 9 f529a Warning: The command above leaves repo in detached HEAD state! More on git checkout in Part II when we discuss branching. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 24
Revert changes from a single commit git revert Revert an existing commit and make a new commit for this. SYNOPSIS git revert < commit > git revert undoes a single commit it does not go back to the previous state of a project by removing all subsequent commits. In Git, this is actually called a reset (see next slide), not a revert. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 25
Reset the history git reset Reset current HEAD to the specified state SYNOPSIS git reset [-q] [< commit >] [--] <paths >... Example 1: You have staged changes and want to unstage them: git reset < file > Example 2: You want the state of your repository back, i.e. from yesterday evening: git reset [-- hard ] 9 f529a Warning: When you do a reset like this, your history is lost (all commit messages). You can rewrite history by this. When you use the (optional) --hard switch your working tree is also reset! Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 26
Exercise: checkout, reset, revert (5 minutes) Step 1: Create two more files in your directory: orange.txt and purple.txt Step 2: Commit the files to the repository Step 3: Add second lines in orange.txt and green.txt (think about things that have these colors). Step 4: Undo the changes in step 3 using git reset. Step 5: Print out the last entry in the log. Alternative: Redo step 3. Use git revert to undo the changes instead of git reset. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 27
Ignore files The.gitignore file Can be in the root or any sub directory of your Git repository. Example: $ cat. gitignore *. aux *. log *. nav *. out *. snm *. toc *. vrb git_intro. pdf $ We have already seen another file that we could use! (./.git/info/exclude) Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 28
Move files within your repository git mv Put git in front of the mv command to make Git aware of the name change. Example: $ git mv file3 file5 $ git status # On branch master # Changes to be committed : # ( use " git reset HEAD <file >..." to unstage ) # # renamed : file3 - > file5 # $ git commit - m " Renamed the file, because..." Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 29
Remove files from version control git rm Put git in front of the rm command to make Git aware of the removal. The command will remove the file from the working directory! For --cached option, see: git help rm Example: $ git rm file5 rm 'file5 ' $ git status # On branch master # Changes to be committed : # ( use " git reset HEAD <file >..." to unstage ) # # deleted : file5 # $ git commit - m " Deleted old file. Not needed by the project any more." Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 30
Exercise: ignore, rename and remove (5 minutes) Add a file colors.log. Do not stage it. Add a.gitignore file to ignore logfiles. Git will not care about colors.log any more (check with git status ). The content of.gitignore may change over time. Why not put it into version control, too? git add.gitignore; git commit -m Ignore logfiles. Rename and remove some of your (color) files. Do it with and without git in front of the mv and rm commands. What difference does git status show? Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 31
Clone a project git clone Clones a repository from some other location. Many protocols are supported. Despite to git init the cloned repo may not be empty. Use git log to see the history. Example: From Github git clone https :// github. com / purepitch / git_course. git Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 32
Pull changes from a remote git pull Fetch from and integrate with another repository or a local branch. Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge. If you have not much experience with branches (see Part II of this course) and just work on one branch (default: master) with one remote (default: origin), you can use git pull for your convenience. Sometimes after a pull Git will throw you into an editor with a merge commit message already prepared. Just save this one and commit the change locally. If there is a conflict Git cannot resolve itself, it will inform you about it and tell you what to do. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 33
Push changes to a remote git push Update the remote server with changes you have made to your local repository. You should always do a git pull first, before you try to push. Make this a habit! At some point you might experience that your push gets rejected by the server. Don t worry! Try a git pull and read what Git tries to tell you. When all conflicts are resolved, the push should work. If not: Contact your Git server administrator to check if you have indeed write permission to the repository. More on git pull and git push in Part II. Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 34
Git commands covered today git help config init status add commit log diff show clone pull push checkout reset revert mv rm Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 35
For further study Git reference site: http://gitref.org Website of the Git project: http://git-scm.com Here you can find the very good Pro Git book (PDF): http://git-scm.com/book Git introduction (Katy Huff) http://www.youtube.com/watch?v=t0be9apiegc More advanced Git introduction (Scott Chacon) http://www.youtube.com/watch?v=zdr433b0hjy (Scott explains the SHA1 hashes in more detail. Furthermore he shows how Git thinks about version control compared to Subversion. He introduces branching very early - a must see talk!) Tech Talk: Linus Torvalds on Git (very funny) http://www.youtube.com/watch?v=4xpnkhjaok8 Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 36
Part II: Advanced Git knowledge Git objects and hashes in detail Stage only parts of a file: patch mode for git add git add -p Work with (local) branches: create, delete and merge Resolve (merge) conflicts More on remotes: What happens when you clone? Fetch and merge remote branches, git stash git tag git rebase git pull Workflows in version control: distributed vs. central Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 37
Thank you Comic source: http://geek-and-poke.com Introduction to Git Markus Kötter koetter@rrzn.uni-hannover.de 38