Advanced Use of Git Beno t Viguier DS-Lunch Talk, Nijmegen, - - PowerPoint PPT Presentation

advanced use of git
SMART_READER_LITE
LIVE PREVIEW

Advanced Use of Git Beno t Viguier DS-Lunch Talk, Nijmegen, - - PowerPoint PPT Presentation

Advanced Use of Git Beno t Viguier DS-Lunch Talk, Nijmegen, October 25th, 2019 1 Disclaimer If you dont like command line interface, this talk is not for you. 2 Minimal Git Commands git clone git@gitlab.science.ru.nl:user/repo


slide-1
SLIDE 1

Advanced Use of Git

Benoˆ ıt Viguier DS-Lunch Talk, Nijmegen, October 25th, 2019

1

slide-2
SLIDE 2

Disclaimer

If you don’t like command line interface, this talk is not for you.

2

slide-3
SLIDE 3

Minimal Git Commands

◮ git clone git@gitlab.science.ru.nl:user/repo ◮ git status ◮ git add <directory/files> ◮ git commit ◮ git push ◮ git pull

3

slide-4
SLIDE 4

Git Status

◮ git status ◮ git log

4

slide-5
SLIDE 5

Shortcuts

◮ git add --all ( -A ) Add all the updated/untracked files. ◮ git add --update ( -u ) Add all the updated files but do not add the untracked ones. ◮ git add --patch ( -p ) Similar to update. Interactively lets you decide which modifications in each file you want to save. ◮ git commit -m "commit message"

5

slide-6
SLIDE 6

branches

slide-7
SLIDE 7

Branches

◮ Develop features without breaking master. = ⇒ the master branch always compiles! ◮ Develop multiple features at the same time.

6

slide-8
SLIDE 8

Branches

7

slide-9
SLIDE 9

Branches

◮ Create a branch git branch Future-plans ◮ Switch to that branch git checkout Future-plans These two can be done in one step: git checkout -b Future-plans

8

slide-10
SLIDE 10

Branches

Work (modify, commits. . . ) on the Future-plans. In the meantime, the Master branch continues forward (other commits. . . )

9

slide-11
SLIDE 11

Branches

If nothing was done on Master while you were working on Future-plans you can directly merge. This is called fast-forward. (1) Switch to the master branch: git checkout master (2) Merge: git merge Future-plans

10

slide-12
SLIDE 12

Branches

If nothing was done on Master while you were working on Future-plans you can directly merge. This is called fast-forward. (1) Switch to the master branch: git checkout master (2) Merge: git merge Future-plans

11

slide-13
SLIDE 13

Branches

To push/update a branch on the online repository: git push origin Future-plans To delete ( -d ) a branch on the online repository: git push --delete origin Future-plans To delete ( -d ) a local branch: git branch --delete <branch-name> (Not possible while you are on that branch.)

12

slide-14
SLIDE 14

Merging

slide-15
SLIDE 15

Merging

If Master has changed while you were working on Future-plans the merge process is slightly different. (1) Switch to the master branch: git checkout master (2) Merge: git merge Future-plans (3) Edit the commit message in your editor, save and close.

13

slide-16
SLIDE 16

Merging

Master has changed while you were working on Future-plans the merge process is slightly different. (1) Switch to the master branch: git checkout master (2) Merge: git merge Future-plans (3) Edit the commit message in your editor, save and close.

14

slide-17
SLIDE 17

Rebasing

slide-18
SLIDE 18

Merging & Rebasing

Master has changed while you were working on Feature. You want to make sure your modifications do not break Master.

15

slide-19
SLIDE 19

Merging

Solution 1: (1) Switch to the Feature branch: git checkout Feature (2) Merge: git merge Master

16

slide-20
SLIDE 20

Rebasing

Solution 2: (1) Switch to the Feature branch: git checkout Feature (2) Rebase: git rebase Master

17

slide-21
SLIDE 21

Force Pushing

Once you have rebased you have a conflict between your local tree and the remote tree. What the remote repository knows: What you have:

18

slide-22
SLIDE 22

Force Pushing

Once you have rebased you have a conflict between your local tree and the remote tree. What the remote repository knows: What you have: The solution is a force-push: git push --force origin Feature

19

slide-23
SLIDE 23

Force Pushing

Force pushing is very dangerous and will break everything if not used correctly. ◮ NEVER force push on Master. ◮ ALWAYS specify the repo and the branch.

20

slide-24
SLIDE 24

Conflicts

slide-25
SLIDE 25

Conflicts

Conflicts can happen when you do a pull, merge or rebase. git merge new_branch_to_merge_later

Auto -merging merge.txt CONFLICT (content): Merge conflict in merge.txt Automatic merge failed; fix conflicts and then commit the result.

21

slide-26
SLIDE 26

Conflicts

git status

On branch master You have unmerged paths. (fix conflicts and run "git commit ") (use "git merge

  • -abort" to abort

the merge) Unmerged paths: (use "git add <file >..." to mark resolution) both modified: merge.txt

22

slide-27
SLIDE 27

Conflicts

cat merge.txt

... <<<<<<< HEAD this is some content to mess with content to append ======= totally different content to merge later >>>>>>> new_branch_to_merge_later ...

23

slide-28
SLIDE 28

Conflicts

Resolution steps: (1) Edit the file: select the part you like, erase the alternative, save. (2) git add merge.txt (3) git commit -m "Merged and resolved conflict" In the case of a rebase , instead of writting a commit message, just do git rebase --continue . In both cases, if you are not sure, you can use the --abort option.

24

slide-29
SLIDE 29

HEAD, Checking out, Reverting & Resetting

slide-30
SLIDE 30

HEAD & checkout

The pointer to the current location in Git is called the HEAD. It can be used as a reference point. E.g. if you want to go back to a previous commit, you can do either: ◮ git checkout HEAD~2 ◮ git checkout b where b is a commit id

25

slide-31
SLIDE 31

HEAD & checkout

The pointer to the current location in Git is called the HEAD. It can be used as a reference point. E.g. if you want to go back to a previous commit, you can do either: ◮ git checkout HEAD~2 ◮ git checkout b where b is a commit id From there you can start working on a new branch: git checkout -b Foo

26

slide-32
SLIDE 32

Changing the commit message

You can rewrite the message of the last commit with: git commit --amend

27

slide-33
SLIDE 33

Changing the commit message

You can rewrite the message of the last commit with: git commit --amend

27

slide-34
SLIDE 34

Reverting

If you want to undo the last commit: git revert HEAD This will create a new commit which reverts the lastest changes. If you want to undo changes from an older commit you can also do: git revert commit_id e.g. git revert a1e8bf5 or git revert HEAD~1

28

slide-35
SLIDE 35

Resetting

git reset --... commit_id comes with 2 main options: ◮

  • -soft : keep the files as is but reset the pointer to commit_id .

  • -hard : reset the files to the pointer commit_id .

29

slide-36
SLIDE 36

Reset tricks

◮ git reset --hard HEAD : remove all the changes made from the HEAD ◮ git reset --soft HEAD~4 : go back and forget 4 commits but leave the files as-is. Useful if you want to squash your history. ◮ git reset --hard commit_id : set the repository as it was in commit_id

30

slide-37
SLIDE 37

Stage, Diff, Stash, Clean

slide-38
SLIDE 38

Staged files and diff

Files have 4 states: ◮ Committed ◮ Staged ◮ Unstaged ◮ Untracked Staged files contain changes that are recorded by Git but not committed yet. git diff will show the diff between staged/committed and unstaged files.

31

slide-39
SLIDE 39

Stash

git stash comes with 4 main option: ◮ push : (optional) save your local modifications and revert to HEAD. ◮ pop : apply the modifications on top of the current working tree state. ◮ list ◮ clear During a git stash ; git stash pop , all modifications are unstaged.

32

slide-40
SLIDE 40

Clean

You modified a lot of files, you have a lot of untracked files, your repository is dirty? Don’t worry, git clean is here for you! ◮ git clean -nd : list the files to be removed ◮ git clean -fd : remove recursively ( -d ) untracked files ◮ git clean -fxd : remove recursively untracked and ignored ( -x ) files By default git clean will do nothing, it requires either: ◮

  • n for a dry-run.

  • f for force

33

slide-41
SLIDE 41

Workflow: All on Master (classic academia)

slide-42
SLIDE 42

All on Master

John works on his feature

34

slide-43
SLIDE 43

All on Master

Mary works on her feature

34

slide-44
SLIDE 44

All on Master

John publishes his feature git push origin master

34

slide-45
SLIDE 45

All on Master

Mary tries to publish her feature git push origin master

error: failed to push some refs to ’/path/to/repo.git ’ hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart . Merge the remote changes (e.g. ’git pull ’) hint: before pushing again. 34

slide-46
SLIDE 46

All on Master

Mary rebases on top of John’s commit(s) git pull --rebase origin master

34

slide-47
SLIDE 47

All on Master

Expected new tree (Mary’s POV).

34

slide-48
SLIDE 48

All on Master

But there is a conflict...

CONFLICT (content): Merge conflict in <some -file > 34

slide-49
SLIDE 49

All on Master

git status

# Unmerged paths: # (use "git reset HEAD <some -file >..." to unstage) # (use "git add/rm <some -file >..." as appropriate to mark resolution) # # both modified: <some -file > 34

slide-50
SLIDE 50

All on Master

Mary edits <some-file>

git add <some -file > git rebase

  • -continue

34

slide-51
SLIDE 51

All on Master

Mary successfully publishes her feature git push origin master

34

slide-52
SLIDE 52

Workflow: Branch Workflow

slide-53
SLIDE 53

Using Branches and Pull Requests

Mary begins a new feature git checkout -b marys-feature master git status git add <some-file> git commit

35

slide-54
SLIDE 54

Using Branches and Pull Requests

Mary goes to lunch git push origin marys-feature

35

slide-55
SLIDE 55

Using Branches and Pull Requests

Mary finishes her feature git push origin marys-feature

35

slide-56
SLIDE 56

Using Branches and Pull Requests

Bill receives the Pull Request, reviews and [requests changes/ comments/approves]

35

slide-57
SLIDE 57

Using Branches and Pull Requests

Mary makes the changes

35

slide-58
SLIDE 58

Using Branches and Pull Requests

Mary publishes her feature git checkout master git pull origin master git merge marys-feature git push origin master

35

slide-59
SLIDE 59

Gitflow Workflow

slide-60
SLIDE 60

Gitflow Workflow

Develop and Master Branches. ◮ Master branch only contains the minor and major versions. = ⇒ e.g. Debian Stable ◮ Develop branch contains all the intermediate modifications. = ⇒ e.g. Debian Unstable

36

slide-61
SLIDE 61

Gitflow Workflow

◮ Features are developped as branches of the Develop branch. = ⇒ e.g. Debian Experimental

36

slide-62
SLIDE 62

Gitflow Workflow

◮ A Release branch contains the ”frozen” features. = ⇒ e.g. Debian Testing

36

slide-63
SLIDE 63

Gitflow Workflow

◮ if an issue is detected in master a hotfix branch is created from master

36

slide-64
SLIDE 64

Bonus

slide-65
SLIDE 65

bonus

In your ⑦/.bashrc or ⑦/.zshrc: alias gtree=’git log --oneline --decorate --all --graph’

37

slide-66
SLIDE 66

Thank you.

37