Skip to content

Latest commit

 

History

History
120 lines (97 loc) · 2.09 KB

04_Branch_Rebase.md

File metadata and controls

120 lines (97 loc) · 2.09 KB

Create a new branch:

$ git branch {branch-name}

Switch to an existing branch:

$ git checkout {branch-name}
$ git switch {branch-name}    # new

Switch to previous branch:

git checkout -

Create a new branch and switch to it:

$ git checkout -b {branch-name}

Rename branch:

$ git branch -m {branch-name} {new-branch-name}
$ git branch --move {old-branch-name} {new-branch-name}

Show all branches that are merged into current branch:

$ git branch --merged

Delete a branch:

$ git branch -d {branch-name}

List all branches:

$ git branch

View all branches - local & remote:

$ git branch -a

Remove remote branch:

$ git push --delete origin {branch-name}

Delete unmerged branch:

$ git branch -D {branch-name-to-delete}

HEAD is where we right now in the repository. The HEAD points to the last commit in the branch we are currently on.

Merge

There are two types of merging:

  1. Fast-forward-merge: It happens when current branch has no extra commits compared to the branch we're merging.
  2. No-fast-forward-merge: If the master branch has new changes which other branch doesn't have, git will commit no-fast-forward merge.

First checkout to master branch:

$ git checkout master

Merging:

$ git merge {branch-name}

Merge on master branch (only if fast forward):

$ git merge --ff-only {branch-name}

Merging on master branch (forcing a new commit):

$ git merge --no-ff {branch-name}

Stop merge (in case of conflicts):

$ git merge --abort
$ git reset --merge        # prior to v1.7.4

Undo local merge:

$ git reset  --hard {branch-name}

Merge a specific commit:

$ git cherry-pick {commit-ID}

Rebase:

$ git checkout {branch-name} >> git rebase main

Cancel rebase:

$ git rebase --abort

Squash multiple commit to one commit:

$ git rebase -i HEAD~3

Squash merge in one commit:

$ git merge --squash {branch-name}  # (and commit afterwards)