Git branching
Creating a New Branch¶
HEAD
is a pointer to the local branch you’re currently on.
The git branch command only created a new branch — it didn’t switch to that branch. So you’re still on master branch instead of the new branch.
Switching Branches¶
This moves HEAD to point to the testing branch.Creating a new branch and switching to it at the same time¶
Branch Merging¶
Merge iss53 branch into master * check out the branch you wish to merge into * run the git merge command
%%{init: { 'logLevel': 'debug', 'theme': 'base' } }%%
---
title: git merge
---
gitGraph
commit
commit
branch iss53
checkout iss53
commit
commit
checkout main
commit
merge iss53
commit
commit
Branch Management¶
Listing Current Branches¶
- To see the last commit on each branch
- To see all branch including remote branches (remote branches are in red)
Changing a branch name¶
git branch --move bad-branch-name corrected-branch-name # (1)
git push --set-upstream origin corrected-branch-name # (2)
git push origin --delete bad-branch-name # (3)
- Rename the branch locally
- Push the change
- Delete the branch with the bad name
Remote Branches¶
Remote-tracking Branch¶
- Remote-tracking branches are references to the state of remote branches. They’re local references that you can’t move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository. Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them.
- Remote-tracking branch names take the form
<remote>/<branch>
- the master branch on your origin remote:
origin/master
- a partner pushed up an iss53 branch: might have your own local iss53 branch, but the branch on the server would be represented by the remote-tracking branch
origin/iss53
- the master branch on your origin remote:
Tracking Branch¶
- Tracking branches are local branches that have a direct relationship to a remote branch.
- When you clone a repository, it generally automatically creates a master branch that tracks origin/master.
set up tracking branch for a existing local branch
git branch -u origin/serverfix
git branch --set-upstream-to origin/serverfix
list tracking branches you have set up
git fetch --all; git branch -vv
iss53 7e424c3 [origin/iss53: ahead 2] Add forgotten brackets
master 1ae2a45 [origin/master] Deploy index fix
* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] This should do it
testing 5ea463a Try something new
Deleting Remote Branches¶
- delete your serverfix branch from the server
Rebasing¶
With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch.
check out the experiment branch and then rebase it onto the master branch
git checkout experiment # (1)
git rebase master # (2)
git checkout master # (3)
git merge experiment # (4)
- check out the experiment branch
- rebase it onto the master branch
- go back to the master branch
- do a fast-forward merge
rebase the server branch onto the master branch without having to check it out first
git rebase master server # (1)
git checkout master # (2)
git merge server # (3)
git rebase <basebranch> <topicbranch>
- go to the master branch
- fast-forward the base branch (master)