Git - working with branches
Table of Contents
Branching strategies
Good gamification way of learning git branching
https://learngitbranching.js.org/
How to create branch from an old commit
git branch <branchname> <commitHash>
git checkout -b <branchname> <commitHash>
(This is a combination ofgit checkout <commitHash>
andgit checkout -b <branchname>
)- checkout a branch and use reset
# Create a new branch from HEAD git checkout -b <branchname> # Remove the commits made after <commit-id> git reset --hard <commit-id> # Push the new branch to the remote repository git push --set-upstream origin <branchname>
- Using GitHub or BitBucket UI. Go to the specific commit and create a branch from there.
Listing branches
command | description |
---|---|
git branch -vv |
To find out which remote branch your local branch is tracking, command to give tracking branch |
git status -sb |
To find out which remote branch your local branch is tracking, command to give tracking branch |
git remote update |
command to update remote branches |
git remote update --prune |
command to update remote branches. This will remove all remote branches which you have a local record of, but are no longer actually present on the remote. |
git branch -m <newname> |
To rename the current local branch |
git branch (local branch name) -u (remote branch name) |
To point local branch to a different remote branch |
git branch -a |
command to list all branches |
git branch -r |
Remote branches only. |
git branch -l or git branch |
Local branches only. |
Checking out branches
command | description |
---|---|
git checkout -b feature/testFeatureBranch remotes/origin/develop |
To create a feature branch from remotes/origin/develop and checkout that feature branch |
git checkout -b feature/testFeatureBranch |
Same step as above; But you don’t have to mention develop branch in the command if you are already on the develop branch |
git checkout localBranch1 |
To find out which remote branch your local branch is tracking |
git checkout localBranch2 |
To find out which remote branch your local branch is tracking |
git status |
To see the status of the local repository |
Remove all your local branches except main
Sometimes, you may want to delete all your local branches except main
git branch | grep -ve " main$" | xargs git branch -D
You can also use an alias:
alias gbr="git branch | grep -ve " main$" | xargs git branch -D"
Deleting branches
https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely
Delete a remote branch
$ git push <remote_name> --delete <branch_name>Delete Local Branch
$ git push <remote_name> -d <branch_name>Delete Local Branch
Delete local branch
git branch -d <branch_name>
git branch -D <branch_name>
- The -d option is an alias for –delete, which only deletes the branch if it has already been fully merged in its upstream branch.
- The -D option is an alias for –delete –force, which deletes the branch “irrespective of its merged status.” [Source: man git-branch]
- As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
- You will receive an error if you try to delete the currently selected branch.
My local workflow when working with branches
- Local branch
- Keep committing changes to it consistently
- Another approach is to move changes to the
staging
area
- Another approach is to move changes to the
- Before switching to other branches, or pulling or merging or pushing to remote,
squash
.- This will keep the history a little cleaner.
- Use stash sparingly.
- When we create branches from stashes, it includes everything from the stash into that branch.
- If there are changes that I want to keep in local only (not push it to remote ever), it gets tricky.