Modifying and comparing branches

Intermediate Git

George Boorman

Curriculum Manager, DataCamp

Diff recap

Command Function
git diff Show changes between all unstaged files and the latest commit
git diff report.md Show changes between an unstaged file and the latest commit
git diff --staged Show changes between all staged files and the latest commit
git diff --staged report.md Show changes between a staged file and the latest commit
git diff 35f4b4d 186398f Show changes between two commits using hashes
git diff HEAD~1 HEAD~2 Show changes between two commits using HEAD instead of commit hashes
Intermediate Git

Comparing branches

git diff main summary-statistics
Intermediate Git

git diff output

git diff main summary-statistics output, truncated

Intermediate Git

git diff output

git diff main summary-statistics output, second part

Intermediate Git

Navigating large git outputs

  • Can produce large outputs!
  • Press space to progress through and q to exit
Intermediate Git

Modifying branches

git branch
  main
* feature_dev
  • feature_dev

  • Need another branch for a second new feature being developed

  • Solution - rename feature_dev

  • Renaming a branch

git branch -m
Intermediate Git

Renaming a branch

git branch
  main
* feature_dev
  • feature_dev

  • Need another branch for a second new feature being developed

  • Solution - rename feature_dev

  • Renaming a branch
git branch -m feature_dev chatbot
Intermediate Git

Checking our branches

git branch
  main
* chatbot
Intermediate Git

Deleting a branch

  • Large projects can have many branches

  • Delete branches once we are finished with them

  • Delete the chatbot branch with -d flag

git branch -d chatbot
Deleted branch chatbot (was 3edb989).
Intermediate Git

Deleting a branch that hasn't been merged

  • If chatbot hasn't been merged to main, git branch -d chatbot will produce an error
error: The branch 'chatbot' is not fully merged.
If you are sure you want to delete it, run 'git branch -D chatbot'.
  • Delete with -D flag
git branch -D chatbot
Deleted branch chatbot (was 3edb989).
  • Difficult, but not impossible, to recover deleted branches

  • Be sure we don't need the branch any more before deleting!

Intermediate Git

Summary

Command Function
git diff main chatbot Compare the state of the main and chatbot branches
git branch List all branches
git branch -m old_name new_name Rename branch called old_name to new_name
git branch -d chatbot Delete chatbot branch, which has been merged
git branch -D chatbot Delete chatbot branch, which has not been merged
Intermediate Git

Let's practice!

Intermediate Git

Preparing Video For Download...