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:
git branch other-branch <git-commit-id(first 8 chars)>
- switching branches
git checkout <other-branch>
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
git branch -m <new-name>
- rename another branch than HEAD
git branch -m <ubuntu-version> <new-name>
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
git push -u origin <local-branch>
- Want to track the changes and start working from a remote branch?
git checkout --track origin/feature/downloader
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 pull
git push
git branch -v
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
git branch -d <branch-name>
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:
git push origin --delete feature/downloader
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
git switch <target-branch>
git merge <branch-with-desired-changes>
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.
git switch <target-branch>
git rebase <branch-with-desired-changes>
Comparing Branches
checking which commits are in branch-B, but not in Branch-A
git log main..feature/uploader
Graphical representation of our repo
git log --all --graph