Thursday, September 30, 2021

Git - branches

 #Fast forwarding

#View all branches. * is current
git branch --list
#View remotes
git branch -r
#View all (local and remote)
git branch -a

#fetch remote branches
git fetch --all

#fetch single branche
git fetch origin "feature/name"

#Create a new branch pointing to where we are called branch1
git branch branch1

git branch --list
#Go to branch1
git checkout branch1

#use preferred command switch 
git switch branch1

#To create and checkout in one step:
git checkout -c branch1

#make some EDITS

#Move to main
git switch master
#we can look at the differences
git diff master..branch1
#If happy lets merge them. Remember we already switched to main. We are going to merge into this from branch1
git merge branch1
#Done. Notice was a fast-forward. Lets look at merged branches
git branch --merged

gitgraph

#We no longer need branch1. Remember use tags if you want some long lived reference
#This would only delete locally
#Remember to ALWAYS check it has been merged first before deleting
git branch --merged
git branch -d branch1


#Keeping the history

#go back 2 commits
git reset --hard HEAD~2

git branch branch1
git switch branch1

#This time specify NOT to perform a fast forward
git switch master
git diff master..branch1
git merge --no-ff branch1
git branch --merged

gitgraph
#Notice the merge was a new commit

#We can still delete branch1 as its still merged
git branch --merged
git branch -d branch1
#History is kept there was a branch
gitgraph


#3-way merge
#going back to first commit
git reset --hard HEAD^1
gitgraph

#Make the branch1 again with two commits
git branch branch1
git switch branch1
code jl.csv
git add .
git commit -m "Added Wonder Woman"
code jl.csv
git add .
git commit -m "Added The Flash"
gitgraph
git status

#Switch to main
git switch master
code jl.csv
git add .
git commit -m "Added Cyborg"

gitgraph
#More interesting. There is now NOT a direct path from branch1 to main
git status
#We will have conflicts given the changes we made
git merge branch1
#We need to fix them by editing the file it tells us and conflicts have been marked
code jl.csv
#We are in conflict status:
git status
git add .
git commit -m "Merged with branch1"

#Rebase - don't use it in public repos


#There is another option. Rebase
#Lets rewind before the merge by going back 1
git reset --hard HEAD~1
gitgraph

#WB19
#Need to be ON the branch we are performing the action on. We are rebasing branch1
git switch branch1
#check its clean
git status
#Lets rebase off main
git rebase master
#We will get conflicts as it replays each of the changes so each time will need to address and continue
code jl.csv
git add jl.csv
git rebase --continue

gitgraph
#Cleaner path. Copy next to the rebase whiteboard to compare!

#If now merge would just be a fast-forward since now a straight line from main and NOW 3-way merge
git status
git switch master
git merge branch1
#Cleanup
git branch --merged
git branch -d branch1
#Note if the remote master has changes you don't have and want to base on can git pull --rebase


Git - tagging

Lightweight tags: 

function gitgraph {git log --oneline --graph --decorate --all}

git tag v1.0.0
gitgraph
git tag v0.9.1 <previous commit>
gitgraph
git tag --list

you can look for this tag:
git show v1.0.0

Annotated tags: 

Those enable you to add messages and creating an object that references commit

git tag -a v0.0.1 <commit hash> -m "First version"

git show v0.0.1
#we see the TAG information AND then the commit it references
git cat-file -t v0.0.1
git cat-file -t v1.0.0

Git - moving between commits




 

Git - command to move files between stages