As an IT professional, Git is an essential tool in your day-to-day work. It allows you to collaborate with your team members, keep track of code changes, and manage your projects effectively. While Git has a lot of commands, some of them are more useful than others. In this blog post, we’ll cover the five most useful Git commands beyond basics that every IT professional should know.
Prerequisites
Before diving into the commands, there are a few prerequisites that you should know. You should have a basic understanding of Git, including how to create a repository, commit changes, and merge branches.
1. Git Stash
git stash
is a useful command when you need to temporarily save changes without committing them. For example, if you’re working on a feature and your boss asks you to fix a critical bug, you can use git stash
to save your changes and switch to the bug fix branch. Once you’ve fixed the bug, you can switch back to your feature branch and apply the saved changes with git stash pop
.
Command | Description |
---|---|
git stash save “message” | Stashes changes with a message |
git stash list | Lists all stashes |
git stash apply | Applies the most recent stash |
git stash pop | Applies the most recent stash and removes it from the stash list |
2. Git Rebase
git rebase
is a powerful command that allows you to merge changes from one branch to another. Unlike git merge
, which creates a new commit to merge changes, git rebase
applies the changes on top of the target branch, resulting in a linear history. This command can be especially useful when working on feature branches that need to be kept up-to-date with the main branch.
Command | Description |
---|---|
git checkout feature-branch | Switches to the feature branch |
git rebase main-branch | Applies changes from main-branch to feature-branch |
3. Git Cherry-pick
git cherry-pick
is a command that allows you to apply a specific commit to a branch. This command can be useful when you need to apply a hotfix or a bug fix from a different branch to your current branch.
Command | Description |
---|---|
git cherry-pick <commit> | Applies a specific commit to the current branch |
4. Git Reset
git reset
is a command that allows you to undo changes made to a branch. There are three modes of git reset
– soft, mixed, and hard. The default mode is mixed, which resets the index and working directory but leaves the changes in the stash. This command can be especially useful when you need to undo a commit that caused issues.
Command | Description |
---|---|
git reset –soft HEAD~1 | Undoes the last commit and keeps changes in the staging area |
git reset –mixed HEAD~1 | Undoes the last commit and resets the staging area |
git reset –hard HEAD~1 | Undoes the last commit and discards changes |
5. Git Bisect
git bisect
is a command that allows you to find the commit that introduced a bug. This command can be useful when you have a large codebase and need to identify the problematic commit that caused the bug.
Command | Description |
---|---|
git bisect start | Starts the bisect process |
git bisect good <commit> | Marks a commit as good |
git bisect bad <commit> | Marks a commit as bad |
git bisect reset | Resets the bisect process |
# Example 1: Start git bisect
git bisect start
# Example 2: Specify a known bad commit
git bisect bad abc123
# Example 3: Specify a known good commit
git bisect good def456
# Example 4: Automatically run a script at each step
git bisect run ./test_script.sh
# Example 5: Skip a commit
git bisect skip
# Example 6: Reset the bisect session
git bisect reset