Git is a powerful version control system that provides a wide range of advanced commands beyond the basic ones like init, add, commit, and push. These advanced commands allow you to perform more complex operations and efficiently manage your codebase. Here are some essential advanced Git commands along with brief explanations:
The git clone command creates a copy of a remote repository on your local machine. It’s the first step when you want to start working on an existing project.
git clone <remote_repository_url>
This command lists all the branches in your repository or creates a new branch if a branch name is provided as an argument.
git branch // List all branches git branch new-feature // Create a new branch named "new-feature"
This command allows you to switch between branches or restore files from a specific commit.
git checkout master // Switch to the "master" branch git checkout my-file.txt // Restore "my-file.txt" to its last committed state
The git merge command merges changes from one branch into the current branch.
git checkout main // Switch to the branch you want to merge into (e.g., "main") git merge feature-branch // Merge changes from "feature-branch" into "main"
The git rebase command moves or combines a sequence of commits to a new base commit. It’s a way to integrate changes from one branch into another, making the commit history cleaner.
git checkout feature-branch git rebase main // Rebase "feature-branch" on top of "main"
This command allows you to undo changes in your working directory or unstage changes.
git reset HEAD~1 // Undo the last commit and keep changes in the working directory git reset --hard HEAD~1 // Undo the last commit and discard changes
Enables you to pick a specific commit from one branch and apply it to another branch.
git checkout target-branch git cherry-pick <commit-hash> // Apply the specific commit to the target branch
Temporarily saves your uncommitted changes, allowing you to switch branches without committing them.
git stash // Save changes to the stash git stash pop // Apply changes back to the working directory
Displays the commit history, showing the author, date, and commit message.
git log // Show the commit history
These are just a few of the many advanced Git commands available. It’s important to explore and understand them thoroughly before using them, as some actions can modify your repository history and affect your project and collaboration with other team members. Always ensure you have a backup or use these commands with caution. Additionally, the official Git documentation is a great resource to learn more about these advanced commands.
Note: Please note that we have provided a Git cheatsheet for your reference.