GIT branches

Studymaterial

Baggrundsmateriale: Gratis e-bog her
Brug denne interaktive tutorial til at lære git branching

Gå direkte til dagens emne omkring git branching her

Workshop emner

Quick recap:

  1. Creating repository on github
  2. Add collaborators (team members)
  3. Cloning repository
  4. git commands like: add, commit, push, fetch, merge, stash

Branches

  1. Create branches
  2. Merge branches
  3. Feature branch workflow
  4. Merge branches with rebase
  5. Forking a project vs. branching
  6. Making a pull request
    • Using git issues
    • Labels and notifications

Git overview

alt text

Global settings

First repository

git clone <remote repo> or
git init inside a directory to make it a git repo.

Small exercise in basic git/github

  1. Create a repo online from github.com
    • Create github repo
    • Add collaborator
    • clone the project
    • add .gitignore
    • add netbeans project, add, commit and push
    • See result at github
    • Create some changes on the remote repo and commit
    • fetch and merge locally.
    • try merging when there is unstaged content in working area
    • then use git stash to temporarily remove the dirty files
    • make the merge
    • run git stash apply and git add . to put the stashed files back into the staged area.
  2. Starting locally (do this one at home)
    • Create netbeans maven project
    • git init
    • git add, commit
    • on github create new repo
    • git remote add origin e.g. url to github repositoyr
    • git push -u eg. `git push -u origin master` -u is short for --set-upstream

Hint: you can work with git locally without ever needing to push to remote (if you dont need to collaborate with others / or backup in cloud)

Small exercise done in pairs

Finally delete the repository both remotely and locally

Lets demo


Demonstrating how files move through the states with git status:

Small exercise

Work individually and locally

Git diff

Lets demo


Demonstrate git diff

Remove stuff from staged area with git

View the project history - git log

Undo stuff

Overwrite previous commit message

Undo staging (remove file from staged area)

If you did git add and regret it

Undo changes since last commit on a particular file

Undo changes since last commit on all files

Undo last commit entirely

Small exercise

Remote (eg. github)

Remote is a server that can host our repository like

  1. github
  2. bitbucket
  3. gitlab
  4. more..

Set up reference to remote

  1. by cloning a remote repo
  2. git remote add <some name> <url> //git remote add + a name for the remote + the url for the remote
    • You can add several remotes if you like
    • If you clone - then the name will be ‘origin’
    • see list of remtes git remote -v

Tagging

Tags in git can be used to reference a particular commit (e.g. version1 or sprint1)

Git alias (Not necessary but maybe nice to have)

Git aliasses are typically longer commands refered to with a few key strokes. This can be usefull if you find you have long commands that you are using frequently.

Modify the bash.bashrc file (windows)

You can write new commands to the git bash (teach it new tricks): Located at: C:\Program Files\Git\etc
Try adding this:

function gitp() {
    git add .
    git commit -a -m "$1"
    git push
}

Now you can open git bash and inside a git repo you can use the command: gitp “commit message” to add, commit and push in one single command.

Small exercise

Basic git homework (only if you have the time)

Your exercise for today is here AND
before tomorrow - follow this interactive tutorial here

Dagens egentlige emne: Branching and merging

Studymaterial: git book (pro git) page 89-95

What are branches?

Why would we need branches?

How should i branch

Create new branches

Push branch to upstream

branches are easy to move around and often refer to different commits as work is completed on them. Branches are easily mutated, often temporary, and always changing.

Changing branch

IMPORTANT:

Lets demo


Demonstrate creating branches and commits in each branc




-git branch development // or git checkout -b development -creates and checks out in one command.

Small individual class exercise

  1. Create 3 branches
  2. Check them out individually and add a file and commit in each branch
  3. run: git log --oneline --decorate --graph --all to see how your git branches look now
  4. create an alias so that you can run the command in this prevous line by just writing git graf.

Small exercise in pairs

Work in pairs to:

Merging

Quit merging:

Fast-Forward merge

Three way merge

image shows how iss53 branch is merged into master with three-way-merge using C5+C4 and their common ancestor C2 to create a new merge commmit: C6 which master will then point to (and now iss53 can be deleted)

Merge conflicts

When you git merge and it creates a CONFLICT do git status and it tells what to do

Lets demo


Merge conflicts




  1. create a new repo
  2. make some file - add - commit
  3. Create a new branch
  4. In each branch master and mybranch change the text file (one: deletes a line and two: change the line)
  5. Commit each branch
  6. Merge mybranch into master
  7. git status
  8. Look inside the file and see how to solve the conflict
  9. Solve the conflict and commit.

Small exercise

  1. Create a new git repo
  2. Add a text file to the repo and run: git add . and git commit -m "initial commit"
  3. checkout a new branch: feature1
  4. do some changes to the text file - save - git add - git commit
  5. checkout a new branch: feature2
  6. do some changes to the text file - save - git add - git commit
  7. merge the two branches and resolve conflict

Rebase

Changing (moving commits) existing branches (Practise this here)

Small exercise manipulating branches

  1. create a git repo
  2. make 3 commits in master
  3. make 2 new branches: development, feature1
  4. create 3 commits in both development and feature1 (files need to be created or changed in each commit)
  5. Move feature1 to the head of development

Git issues

Git milestones

Git issues - labels

Small exercise with issues, milestones and labels

  1. In the last repo you created
  2. create 3 milestones
  3. Create 2 label: ‘new feature’ and ‘java servlet’
  4. create 3 isssues and tag them with labels and milestones
  5. add Assignees to each issue

Git forking

Git pull request

pull requests are a mechanism for a developer to notify team members that they have completed a feature. Once their feature branch is ready, the developer files a pull request

Extra exercise for the dedicated

The exercise is here