1 of 23

PROJECT MANAGEMENT (WITH GIT)

1BCSL307A

2 of 23

Experiment 2 - Creating and Managing Branches:

Create a new branch named "feature-branch." Switch to the "master" branch. Merge the "feature-branch" into "master."

2

3 of 23

Git Branching

3

4 of 23

BRANCHING IN VCS

  • Branching means you diverge from the main line of development
  • continue to do work without messing with that main line
  • Other VCS Tools – an expensive process requiring copy of source code directory
  • Git Branches – lightweight
  • Git stores data as snapshots NOT differences.

4

5 of 23

COMMIT AND ITS TREE

5

  • Git stores a commit object that contains a pointer to the snapshot of the content you staged
  • object also contains the author’s name and email address, the message that you typed, and pointers to the commit or commits that directly came before this commit
  • zero parents for the initial commit
    • one parent for a normal commit
    • Multiple parents for a commit

that results from a merge of

two or more branches

$ git add README test.rb LICENSE

$ git commit -m 'Initial commit'

6 of 23

BRANCH IN GIT

  • If you make some changes and commit again, the next commit stores a pointer to the commit that came immediately before it.
  • A branch in Git is simply a lightweight movable pointer to one of these commits
  • default branch name in Git is master or main. It gets created on git init
  • Every time you commit, the master branch pointer moves forward

automatically.

6

7 of 23

BRANCH AND ITS COMMIT HISTORY

7

8 of 23

CREATING A NEW BRANCH

  • creates a new pointer to the same commit you’re currently on.
  • special pointer called HEAD
  • In Git, this is a pointer to the local branch you’re currently on
  • Notice it is still on main
  • To switch to an existing branch, use git checkout

​

8

hp-pc@ projects/flask-git-lab (main)

$ git branch --show-current

main

​

hp-pc@projects/flask-git-lab (main)

$ git branch testing

flask-git-lab (main)

​

hp-pc@projects/flask-git-lab (main)

$ git log --oneline --decorate

3b4e9e3 (HEAD -> main, origin/main, testing) update readme

535806a Initial flask application

​

hp-pc@projects/flask-git-lab (main)

$ git checkout testing

Switched to branch 'testing’

hp-pc@projects/flask-git-lab (testing)

9 of 23

SWITCHING TO A NEW BRANCH

  • branch in Git is actually a simple file that contains the 40 character SHA-1 checksum of the commit it points to
  • branches are cheap to create and destroy
  • older VCS tools –copies everything into a second directory.
  • From Git version 2.23 onwards you can use git switch instead of git checkout
  • Switch to an existing branch: git switch testing-branch.
  • Create a new branch and switch to it:
    • git switch -c new-branch.
    • -c flag stands for create, you can also use the full flag: --create.
    • Return to previous branch : git switch -.

9

hp-pc@projects/flask-git-lab (testing)

$ vim test.txt

​

hp-pc@projects/flask-git-lab (testing)

$ git add test.txt

​

hp-pc@projects/flask-git-lab (testing)

$ git commit -a -m 'Make other changes'

[testing 5df08dd] Make other changes

1 file changed, 1 insertion(+)

create mode 100644 test.txt

​

hp-pc@projects/flask-git-lab (testing)

$ git log --oneline --decorate --graph --all

* 5df08dd (HEAD -> testing) Make other changes

* 3b4e9e3 (origin/main, main) update readme

* 535806a Initial flask application

10 of 23

BASIC BRANCHING AND MERGING

1. Do some work on a website.

2. Create a branch for a new user story you’re working on.

3. Do some work in that branch.

At this stage, you’ll receive a call that another issue is critical and you need a hotfix. You’ll do the following:

1. Switch to your production branch.

2. Create a branch to add the hotfix.

3. After it’s tested, merge the hotfix branch, and push to production.

4. Switch back to your original user story and continue working.

10

11 of 23

  • Assume some commits on master
  • Then switch to iss53
  • $ git checkout -b iss53
    • Change a file

​

​

​

​

  • git commit -a -m 'Create new footer [issue 53]'

​

​

11

BRANCHING AND MERGING

12 of 23

  • Switch back to master
  • $ git checkout master
  • if your working directory or staging area has uncommitted changes that conflict with the branch you’re checking out, Git won’t let you switch branches
  • best to have a clean working state when you switch branches
  • stashing and commit amending
  • your project working directory is exactly the way it was before you started working on issue #53
  • Git resets your working directory to look like it did the last time you committed on that branch
  • adds, removes, and modifies files automatically to make sure your working copy is what the branch looked like on your last commit to it.

12

BRANCHING AND MERGING

13 of 23

  • Make the hotfix
  • $ git checkout -b hotfix
    • Make changes to files
  • $ git commit -a -m 'Fix broken email address‘
  • Run tests, merge it back to master
  • $ git checkout master
  • $ git merge hotfix
  • This results in “fast-forward” in that merge
  • commit C4 pointed to by thebranch hotfix you merged in was directly ahead of the commit C2 you’re on, Git simply moves the pointer forward.

13

BRANCHING AND MERGING

14 of 23

  • Switch back to the work you were doing
  • $ git branch -d hotfix
  • Delete the hotfix branch
  • Now the work you did in hotfix branch will not be in the iss53 branch.
  • Now merge again
  • $ git checkout master
  • $ git merge iss53
  • In this case, your development history has diverged from some older point.
  • Git does a simple three-way merge, using the two snapshots pointed to by the branch tips and the common ancestor of the two.

14

BRANCHING AND MERGING

15 of 23

  • Instead of just moving the branch pointer forward
  • Git creates a new snapshot that results from this three-way merge and
  • automatically creates a new commit that points to it.
  • This is referred to as a merge commit, and is special in that it has more than one parent.
  • Now close the iss53 branch
  • $ git branch -d iss53

15

BRANCHING AND MERGING

16 of 23

Exp 2 - Git Branching

  1. Set up configurations - user name, email, defaultBranch - set to master
  2. Go to HOME folder, create directory, branchUSN
  3. Change to directory
  4. Create README.md app.py requirements.txt
  5. Initialize git
  6. Stage and do initial commit
  7. Perform another 2 commits for C1(update readme, C2 (update app)

​

​

16

17 of 23

Git Branching

1. Create a feature-branch

2. Switch to feature-branch

3. Create / update files

4. Stage and commit (C3 -commit)

5. Switch back to master branch

6. Merge feature-branch with master branch

7. print log, branches

8. Delete the feature-branch.

​

17

18 of 23

MERGE CONFLICTS

  • If you changed the same part of the same file differently in the two branches you’re merging, Git won’t be able to merge them cleanly
  • Run git status to check which files are unmerged
  • Git adds standard conflict-resolution markers to the files that have conflicts
  • After you’ve resolved each of these sections in each conflicted file, run git add on each file to mark it as resolved

18

  • hp-pc@myflaskapp (master)
  • $ git merge feature
  • Auto-merging README.md
  • CONFLICT (content): Merge conflict in README.md
  • Automatic merge failed; fix conflicts and then commit the result.

19 of 23

BRANCH COMMANDS

  • get a simple listing of your current branches
    • $ git branch
  • To see the last commit on each branch
    • git branch -v
  • To see which branches are already merged into the branch you’re on
    • $ git branch --merged
  • To delete branch
    • $ git branch -d testing
    • Will fail if it is not fully merged
  • To rename a branch
    • $ git branch --move bad-branch-name corrected-branch-name
  • To rename at remote
    • $ git push --set-upstream origin corrected-branch-name

​

19

20 of 23

Experiment 2 - Creating and Managing Branches:

Create a new branch named "feature-branch." Switch to the "master" branch. Merge the "feature-branch" into "master."

20

21 of 23

STEPS

  • Assume you need to add a recommendations feature using a new file named recommendations.html.
  • Ensure you’re inside your flaskapp
  • Rename your main branch to master
  • Create a new branch named feature-branch.
  • Add recommendations.html.
  • Commit the feature.
  • Switch to the master branch.
  • Merge feature-branch into master.

21

22 of 23

VIVA

1. Which command creates a new branch and switches to it?

A)git branch new-branch

B) git switch -c new-branch

C) git merge new-branch

D) git commit -b new-branch

2. Which command merges feature-branch into main?

    • git merge main
    • git branch feature-branch
    • git merge feature-branch
    • git switch feature-branch

3. The command used to create a new branch is __________.

4. The command used to switch to the main branch is __________.

5. What is the purpose of creating a separate feature branch in Git?

6. Which command is used to delete a merged branch?

​

22

23 of 23

VIVA - ANSWERS

  1. git switch -c new-branch
  2. git merge feature-branch
  3. git branch branch-name
  4. git switch main
  5. To develop features independently without directly affecting the main branch.
  6. git branch -d branch-name

23