Git branches

Published

Tue, 10 of December, 2024

Modified

Tue, 10 of December, 2024

Caution

Web page construction in progress…

Branches

Create & checkout a branch

“checkout” means to change the branch you are currently working on (or switch to)

# 1/2 create b
git branch page_col #create a new branch named "page_col"

# 2/2 then switch to b
git checkout page_col

# or 1+2/2 CREATE + SWITCH BRANCHES
git checkout -b page_col  

Switch to other branch

You can also use git switch other_branch which is more specific

git switch page_col
cat .git/HEAD # (confirms me I moved)

Rename a (local) branch

It’s the -m parameter !

  • you cannot rename a remote branch –> you delete it and re-upload it
# In currently checkedout 
git branch -m better_name

# in different branch (non HEAD)
git switch master
git branch test_branch # fake one 
git branch -a  # it's there
git branch -m test_branch test_branch2
git branch -a  # yep!

Merging a branch misaligned with master

E.g. I got to Github and examine branches and it tells me that wkg_branch is like this

This branch (wkg_branch) is 2 commits ahead of, 2 commits behind master.

1. Evaluate changes

Review these changes to ensure you understand what will be merged.

# check commits in wkg_branchh 
git log master..wkg_branch
  #  4 commits...  
    # qlc su wkg_branch
    # pasticci su wkg_branch
    # boh
    # testing the branch workflow 

# check commits in master
git log wkg_branch..master
  # 2 commits 
    # split git_branch + git_collab ⑂
    # removed branch new_sh

2. Bring the changes from master into your branch

git checkout wkg_branch

!!!!! ⛔️ in progress ⛔️!!!!


Push upstream a local branch

Check out explanation here https://www.geeksforgeeks.org/how-to-set-upstream-branch-on-git/

It is important to Setup an Upstream Branch in Git to make the workflow smooth and manage branches efficiently.

An upstream branch in Git refers to a branch that serves as a reference point for another branch. Typically, it’s used to track the remote branch, allowing developers to fetch updates, compare changes, and push their commits easily. Setting an upstream branch simplifies the process of keeping local and remote repositories in sync.

  1. Create local branch
  2. Switch to local branch git checkout -b <new_branch>
  3. [When the current branch i.e (‘new_branch’) has no Upstream branch set] git push –-set-upstream origin <new_branch> command (the 1st time you push)
  4. Thereafter git push -u origin <new_branch> (all subsequent git push commands automatically move local branch changes up to the remote branch.)
# 1 Create local branch 

# 2 switch to local branch 
git checkout -b <new_branch> # or
git switch <new_branch> 

# 3.a git push –set-upstream command (1st time)
git push  --set-upstream origin <new_branch>
  # check 
  git branch -a # YAY!

# 3.b git push origin (nest times )
git push -u origin <new_branch>

Check which Git branches are tracking which Upstream brances

git branch -vv

# * master       789640d [origin/master] split git_branch + git_collab ⑂
#   page_col     c86edff [origin/page_col] added color.qmd to branch
#   test_branch2 c86edff added color.qmd to branch
#   up           bb0035b [upstream/master] git stuff
#   wkg_branch   12e1e52 qlc su wkg_branch
  • The main branch (master) has a tracking branch of [origin/master]
  • up has a tracking branch of [upstream/master]

Rename a (remote) branch

You need to

  1. Publish an existing local branch on remote git push -u origin local_branch
  2. So you delete old one and push up a new one from local repository

Merge a git branch into master

  1. List All Git Branches
  2. Switch to Master
  3. Merge Branch into Master
  4. Push Changes (push the local changes to the remote repository so everyone working on the project can fetch the latest version.)

Since merging is a type of commit, it also requires a commit message. There are two ways to specify the commit message:

# 1. List
git branch
# 2. Switch
git checkout master
# 3. The merge creates a merge commit, bringing together
    # lines of development while preserving the history of the source branch.
git merge -m "Prova di merge" page_col
# 4. Push the local changes to the remote repository
git push origin

See differences b/w branches

git diff master..page_col '***.qmd'Q

Git Rebase

  1. take commits from a separat branch and replay (shift the change down to the tip of master) them at the end of another brabch
  2. integrate recent commits without merging