you can have multiple Git branches on your computer but you can only have one “HEAD” branch or the currently “active” or “checked out” branch.
local and remote branches most of the time you are working on local branches.
#1 how to create a branch:
git branch my-new-branch
- create a branch at specific target revision:
- switching branches
you can use git switch
to switch branches, checkout has other uses that are more complicated so git switch
is a good habit going forward.
- Renaming branches
- renaming the current HEAD branch
- rename another branch than HEAD
you can rename a remote branch without deleting the old one and just creating a new one.
- “Uploading” a local branch for the first time
- Want to track the changes and start working from a remote branch?
this would bring down the remote branch to be a local branch with the name feature/downloader
& the local branch and the remote origin branch with know about each other from the --track
parameter
or you can make sure you use -u
when push to remote origin for the first time.
Pull & Push
git branch -v will show you if you are ahead or behind
Example
[ahead 1, behind 2]
this mean that on my local branch I have one commit that I haven’t pushed.
and that on my remote branch I have 2 commits that I haven’t pulled.
- deleting branches
you can’t delete your HEAD branch
if you have local commits on branch that are not saved anywhere, you have to use --force
in order to delete the local branch with untracked commits.
you can delete remote branches with:
this will delete the remote feature/downloader branch, remember that if you want to delete a branch, make sure you delete it’s counterpart branch that could be tracking it’s changes.
Merging Branches
Integrating changes from another branch into your current local HEAD branch
Rebasing Branches
instead of integrating changes from a branch, and making a merge commit, you will make the commit history just look like a straight line.
Comparing Branches
checking which commits are in branch-B, but not in Branch-A
Graphical representation of our repo