rw334: advanced git - stellenbosch university · 5. git checkout develop and git pull to get the...

22
S.D Baker Effendi and A.B. van der Merwe RW334: Advanced Git 29 November 2019, Stellenbosch, South Africa

Upload: others

Post on 22-Aug-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

S.D Baker Effendi and A.B. van der Merwe

RW334: Advanced Git

29 November 2019, Stellenbosch, South Africa

Page 2: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Overview

Introduction

Merge Conflict

Branching

Rebase

Amend

Squash

Summary

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 2 / 22

Page 3: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

If not, let’s pretend for now

Before we start, we assume that you are familiar with the following commands:

É git cloneÉ git addÉ git rmÉ git commitÉ git pushÉ git pull

If not, grab a demi or a friend who knows these and ask them to show you!

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 3 / 22

Page 4: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Take the stage...

Before we begin, let’s define a few terms:

A commit is a saved change in Git. The commit command saves a commitobject in the local Git repository (the machine you’re working on).Staging refers to when one is adding or removing changed files before makinga commit.Unstaged changes refers to files which aren’t going to be included in the nextcommit.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 4 / 22

Page 5: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Where am I?

When lost and confused on some random branch, the first three usefulcommands are:

// Shows changed files in staged and unstaged modegit status

// Shows the history of commits on the current branchgit log

// Shows the list of ordered commits HEAD has pointed togit reflog

To exit log or reflog at any time, press q.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 5 / 22

Page 6: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Merge conflicts. . . the result of devs stepping on each other’s toes.

A merge conflict is when Git can’t merge changes between two commitsautomatically. This is usually the result of changes on the remote repository(on the server) not being in sync with your local repository.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 6 / 22

Page 7: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Conflict resolution skillsThe following is an example of a conflict in the code after a git pull,merge or rebase:

<<<<<<< HEADthis is the current branch's changes=======this is the conflicting incoming change>>>>>>> 4e35ae6c7dfb1d80b590ae85f276215a0aa3b3d9

This is how to resolve the conflict:

1. Get rid of the decorator code (the <, =, and > symbols injected by Git)and resolve the code to represent what it should look like

2. Stage the resolved file(s) (i.e. git add)

3. To conclude the resolution call git commitBaker E�endi and van der Merwe: Advanced Git, 29 November 2019 7 / 22

Page 8: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Branching

Now that we are working in groups, how do we separate concerns? This iswhere we talk about master, staging, and feature branches.

Master and staging branches (e.g. develop or release branches) stay for aslong as the project exists. These are typically protected, no force pushesallowed, all tests must be passing, etc.

Feature branches are temporary and are created with the purpose ofrepresenting a specific change. These are typically prefixed by a category.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 8 / 22

Page 9: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Check me out!Two essential commands are:

// Creates and checks out branchgit checkout -b feature/new-branch// Checks out an existing branchgit checkout feature/existing-branch

After creating a branch locally, this branch needs to be published before youcan push changes normally. You can use the -u flag on your first push:

git push -u origin feature/spanking-new-branchWant to delete a branch?

git branch -d feature/local-deletegit branch -D feature/global-delete

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 9 / 22

Page 10: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Branching: Examples

Here are some examples of naming conventions for branches:

// Generic new feature prefixfeature/add-new-button

// Bug fixesfix/null-exception-bug

// Adding to documentationdoc/add-comment-to-method

// Refactoring or style changesrefactor/moving-braces-below-function

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 10 / 22

Page 11: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Fetch!

How do I see what my team mates are working on?

If you don’t have a local copy of abranch that a team member haspublished, the following command willget these changes:

git fetch

All branches on the remote repository are now on your local repository.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 11 / 22

Page 12: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Rebase

Rebasing is moving the base of onebranch to another commit makingit appear as if you branched from adifferent commit.

This changes the history of therebased commit as it is nowcomposed of a different set ofcommits.

git rebase -i gives morecontrol over the modifications.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 12 / 22

Page 13: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Rewriting History

To change the last commit, thecommand:

git commit --amend

will allow you to add or removechanges associated with that commit.To change the message, add the -m flagfollowed by the new message.

After committing, to Git, this is a brandnew commit object and will hold a newhash ID.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 13 / 22

Page 14: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Keeping it clean

During an interactive rebase, we have the option to pick and squash commits.To rebase the last 2 commits from the current HEAD, the git rebase -iHEAD~2 command will produce the following in an editor:

pick ba014c4 Second commitpick 7fe6d1b Third commit

To squash these with HEAD, change pick to squash or s for short.

s ba014c4 Second commits 7fe6d1b Third commit

Close the editor, after which you will be presented by another editor to edit thenew commit message.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 14 / 22

Page 15: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Time to squash!All commit messages will be presented:

# This is a combination of 3 commits# This is the 1st commit message:

Head commit

# This is the commit message #2:

Second commit

# This is the commit message #3:

Third commitAll uncommented lines will be added to the new commit message.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 15 / 22

Page 16: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Bringing it altogether

How does one bring this all together? Let’s give a scenario:You are working on a project that has two branches; master anddevelop. develop is used to add new features and fix bugs whereasmaster is used to hold all stable versions (which is merged back intomaster every couple of features).

You have cloned the repo and want to add a feature. What do you do?

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 16 / 22

Page 17: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Let me at em!

1. git fetch: Let’s update the local repository incase the team hascontributed recently.

2. git status: Where are we? Do we have unstaged changes?

3. git checkout develop: Since we are using a staging branch, wewant to branch off of it.

4. git checkout -b feature/my-cool-feature: This will create alocal branch to work on.

5. Now hack away! Occasionally committing changing on your branch asyou go along.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 17 / 22

Page 18: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

I’m done, now what?

Once you’re done with your feature, you want to merge it back into develop.How do I do that? First step is to prepare for merge and clean up your branch:

1. git fetch, git status: Let’s find out what has been happening. . .

2. You notice that your branch is a few commits behind develop. You need torebase!

3. git checkout feature/my-cool-feature: Incase you’re notthere already.

4. git rebase -i HEAD~N: First, let’s clean up our branch. During this,squash all your commits into 1 or 2 meaningful commits with appropriatemessages. e.g. “FEAT: Added cool feature” or “FIX: Fixed null pointererror.”.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 18 / 22

Page 19: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Let’s merge!

Now that our branch is clean, let’s rebase so that we are up to date withdevelop:

5. git checkout develop and git pull to get the upstream changes.

6. git merge feature/my-cool-feature to merge your branch intothe target branch.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 19 / 22

Page 20: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Merge conflict!?

Due to the changes on develop, you have now encountered a merge conflict.This will need to be fixed.

7. If there was a conflict and your local branch has now diverged, fix theconflicting files and git add them once they’re resolved. Once done, callgit commit.

8. Once you’re happy, push your merge to develop!

9. If everything is successful, go ahead and delete your old branch gitbranch -D feature/my-cool-feature.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 20 / 22

Page 21: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

And if it all goes to hell...

If you would ever like to reset your changes to the current HEAD:git reset --hard HEAD

You can also replace HEAD with the hash of a commit you would like to revertto. You will lose all changes on your local repo.

If you want to temporary store your uncommitted changes for some reason(e.g. started coding on the wrong branch and want to transfer your changes, orwould like to revert) you can use:

git stash

You are now free to switch branches, etc. and get the changes back by calling:git stash pop

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 21 / 22

Page 22: RW334: Advanced Git - Stellenbosch University · 5. git checkout develop and git pull to get the upstream changes. 6. git merge feature/my-cool-feature to merge your branch into the

Resources

É For any further questions, comments, or ridicule, you can contact me [email protected]É For more Git tutorials check out AtlassianÉ For the cute images I used Offerzen’s Swag Hall of FameÉ Willem Bester for the Beamer template.

Baker E�endi and van der Merwe: Advanced Git, 29 November 2019 22 / 22