introduction into git

27
Introduction into Git Serhii Kartashov December 2015 Softjourn Internship

Upload: serhii-kartashov

Post on 15-Apr-2017

372 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Introduction into Git

Introduction into Git

Serhii KartashovDecember 2015SoftjournInternship

Page 2: Introduction into Git

Agenda

Fundamental Concepts of VCS

Git

Development Model

Advanced Tips

Page 3: Introduction into Git

Agenda

Fundamental Concepts of VCS

Git

Development Model

Advanced Tips

Page 4: Introduction into Git

Types of Version Control Systems (VCS)

Distributed Version ControlCentralized Version Control

server

client client

repo

repo repo

Page 5: Introduction into Git

Fundamental Concepts• repository • working copy• conflicts• merging changes• development (branching) model

tag dev feature

origingit repo

git repo git repo

commit

branch

repo

git repo git repo

Page 6: Introduction into Git

Agenda

Fundamental Concepts of VCS

Git

Development Model

Advanced Tips

Page 7: Introduction into Git

How to start working with Git• Install Git client (see useful links at the end)

– git --version• Config you client

– git config --global user.name “Serhii Kartashov“– git config --global user.email [email protected]– git config --list (check your settings)

• Clone a branch from a repository– git clone https://myrepo.com/repo workdir

• Initialize your local repo and link to remote repo– git init– git remote add <my_fork> <url>

• Use git help command– git help clone (launches browser to display HTML with instructions on how to use this

clone command)• You may add .gitignore file to ignore some files, i.e. <*.jar>

Page 8: Introduction into Git

Life cycle of files in your repo

Page 9: Introduction into Git

Saving Changes

• Adding files to stage area:– git add <file>– git add <directory>– git add --all

• Commit the staged snapshot to the project history:– git commit -m “<message>”– git commit –a (will open text editor - vi)

Page 10: Introduction into Git

Inspecting a Repository

• Display a status of working directory and the staging are:– git status

• Display committed snapshots:– git log– git log --oneline– git log -p <file>– git log --stat – git log –graph --decorate –oneline– git log –author=“Serhii Kartashov”

Page 11: Introduction into Git

Undoing Changes. revert

• Undo a committed snapshot. – git revert <commit>

But, instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content.

Page 12: Introduction into Git

Undoing Changes, reset

• edit two files, stage one• run git reset HEAD to undo the stage• re-stage the file and commit• run git reset --soft HEAD~ to undo the commit but

keep the staged files; commit again• run git reset HEAD~ to undo the commit and all

staging; commit again• run git reset --hard HEAD~ to lose the commit and

all that work

Page 13: Introduction into Git

Undoing Changes, clean

• Remove untracked files from your working directory– git clean –n (just shows you what may be

removed)– git clean -f (files only)– git clean –df (files and directories)

Page 14: Introduction into Git

Rewriting history

The git commit --amend command is a convenient way to fix up the most recent commit. It lets you combine staged changes with the previous commit instead of committing it as an entirely new snapshot.

Page 15: Introduction into Git

Rewriting history, rebase

• Rebasing is the process of moving a branch to a new base commit. – git rebase <base>

Page 16: Introduction into Git

Agenda

Fundamental Concepts of VCS

Git

Development Model

Advanced Tips

Page 17: Introduction into Git

Development (branching) model

• Branch convention: – master– develop– release-* (tags)– hotfix-*

Page 18: Introduction into Git

Using Branches, branch

The git branch command lets you create, list, rename, and delete branches. It doesn’t let you switch between branches or put a forked history back together again.– git branch (list branches)– git branch <branch> (create new branch)– git branch -d <branch> (delete the specified branch)– git branch -m <branch> (rename the current branch

to <branch>)

Page 19: Introduction into Git

Using branches, checkout

The git checkout command lets you navigate between the branches created by git branch.– git checkout <existing-branch>– git checkout -b <new-branch>

Page 20: Introduction into Git

Using Branches, merge

The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.– git merge <branch>– git merge --no-ff <branch> (merge the specified

branch into the current branch, but always generate a merge commit)

Page 21: Introduction into Git

Git Work Flow Cycle

clone repo create,

checkout branch

make changes

update branchadd files

to stage

commit staged

files

push commits

to repo/fork

create pull

request

Clone repository• git clone• git init• git remoteCreate/Checkout branch• git branch• git checkoutMake changes• git add• git resetUpdate branch• git diff• git fetch• git rebaseCommit changes• git commitPush changes to repo• git push

Page 22: Introduction into Git

Agenda

Fundamental Concepts of VCS

Git

Development Model

Advanced Tips

Page 23: Introduction into Git

Stashing

• Stash the changes in a dirty working directory away – git stash– git stash list – git stash apply– git stash show <stash_name>– git stash drop <stash_name>– git stash branch testchanges– git stash clear

Page 24: Introduction into Git

Filtering and Formatting Logs

• git shortlog• git log --graph --oneline --decorate• git log -3 (display only the 3 most recent

commits)• git log --after="2014-7-1“ (by date)• git log --author="John\|Mary“• git log --grep="JRA-228:“ (by name)• git log -S"Hello, World!" (by content)

Page 25: Introduction into Git

Useful links• Git clients

– http://git-scm.com/downloads (required)– https://code.google.com/p/tortoisegit/wiki/Download

• Useful Git resources– https://www.atlassian.com/git/tutorials/setting-up-a-repository/#!

overview – http://ndpsoftware.com/git-cheatsheet.html#loc=workspace – https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf – Squash several Git commits into a single commit

• Git books and docs– http://git-scm.com/book– Command’s list http://git-scm.com/docs – http://git-scm.com/documentation

Page 26: Introduction into Git

Home Work. Git1. Work Cycle:

1. create and init remote repository (set-url);2. checkout branch and create new one;3. rename branch;4. create new files and add it to git;5. commit changes and push them to remote repo;6. use reset command (stage, commit, etc.)7. use stash command (list, drop, save, pop, branch)

2. Branches: 8. copy new branch from created before (task 1);9. apply changes in new branch and commit it (create possible conflict

between them);10. merge changes in the second branch into first one.

Page 27: Introduction into Git