DEV Community

Cover image for Git Essential Commands: fetch, rebase, amend, cherry-pick
Rusydy
Rusydy

Posted on

Git Essential Commands: fetch, rebase, amend, cherry-pick

Git is a powerful version control system that allows developers to collaborate and manage their code effectively. In this article, we will take a closer look at four essential git commands: git fetch, git rebase, git amend, and git cherry-pick.

Git Fetch

The git fetch command is used to retrieve commits and other information from a remote repository. It brings the remote branches and commits into your local repository, but it does not merge them with your current branch.

Flags

-p, --prune: This flag will remove any remote branches that have been deleted from the remote repository.

-v, --verbose: This flag will provide more detailed output for the fetch operation.

-a, --all: This flag will fetch all the remote branches.

-t, --tags: This flag will fetch all the tags from the remote repository.

Examples



git fetch origin

git fetch --all --tags


Enter fullscreen mode Exit fullscreen mode

Use Case

When working on a team project, you can use git fetch to retrieve the latest changes made by other team members before you start working on new changes.

Git Rebase

The git rebase command is used to apply commits from one branch onto another branch. It allows you to take the commits from one branch and apply them on top of another branch, effectively rewriting the branch's history.

Flags

-i, --interactive: This flag will open an interactive rebase session.

-p, --preserve-merges: This flag will preserve the merge commits.

-s, --strategy: This flag will specify the merge strategy.

-X, --strategy-option: This flag will specify the merge strategy option.

--autosquash: This flag will automatically squash fixup commits.

Examples



git rebase main

git rebase -i --autosquash origin/main


Enter fullscreen mode Exit fullscreen mode

Use Case

When working on a feature branch, you can use git rebase to update your branch with the latest changes made to the master branch before merging it back.

Git Amend

The git amend command is used to modify the most recent commit. It allows you to add new changes to the most recent commit without creating a new commit.

Flags

--no-edit: This flag will use the existing commit message.

--reset-author: This flag will reset the author of the commit.

--no-verify: This flag will skip the pre-commit and commit-msg hooks.

Examples



git commit --amend

git commit --amend --no-edit


Enter fullscreen mode Exit fullscreen mode

Use Case

When working on a feature branch, you can use git amend to add new changes to the most recent commit before pushing it to the remote repository.

Git Cherry-Pick

The git cherry-pick command is used to apply the changes introduced by some existing commits. It allows you to pick specific commits from one branch and apply them on top of another branch.

Flags

-n, --no-commit: This flag will apply the changes without creating a new commit.

-e, --edit: This flag will open an editor to edit the commit message.

-x, --no-commit: This flag will add a Signed-off-by line to the commit message.

Examples



git cherry-pick 1234

git cherry-pick -n 1234


Enter fullscreen mode Exit fullscreen mode

Use Case

When working on a feature branch, you can use git cherry-pick to pick specific commits from the master branch and apply them on top of your feature branch.

Top comments (0)