In this article, we’ll dive into some of the most powerful and essential Git commands. Whether it’s managing changes, working with tags, rebasing commits, or debugging, these commands will elevate your version control skills and make complex operations a breeze. Let’s get started!
1. Stashing Changes
Stashing allows you to temporarily save your uncommitted changes without committing them. This is useful when you need to switch branches or perform other tasks without losing your current work.
Commands
-
git stash
Description:
Temporarily saves your uncommitted changes (both staged and unstaged) and reverts your working directory to match the HEAD commit.Usage:
git stash
-
git stash list
Description:
Lists all stashed changes along with their identifiers.Usage:
git stash list
Example Output:.
stash@{0}: WIP on main: 1234567 Commit message stash@{1}: WIP on feature: 89abcde Another commit message.
-
git stash apply <stash>
Description:
Applies the specified stash to your working directory without removing it from the stash list.Usage:
git stash apply stash@{0}
-
git stash pop
Description:
Applies the most recent stash and removes it from the stash list.Usage:
git stash pop
-
git stash drop <stash>
Description:
Deletes a specific stash from the stash list.Usage:
git stash drop stash@{0}
Example:
git stash drop stash@{1}
Practical Example
1. Stashing Changes
Scenario: You are working on a feature and realize you need to quickly fix a bug in another branch. You want to save your changes temporarily without committing them.
Steps:
-
Save Changes Temporarily:
git stash
This command saves your changes (both staged and unstaged) and reverts your working directory to match the latest commit.
-
Verify Stashed Changes:
git stash list
You’ll see a list of stashes like:
stash@{0}: WIP on feature-branch: Update README
-
Switch to Another Branch:
git checkout main
-
Fix the Bug and Commit:
# Make necessary changes git add . git commit -m "Fix bug in main branch"
-
Return to Your Original Branch:
git checkout feature-branch
-
Apply the Stashed Changes:
git stash apply stash@{0}
Your saved changes will be reapplied to your working directory without being removed from the stash list.
-
Remove the Stash:
git stash drop stash@{0}
2. Working with Tags
Scenario: You’re preparing for a release and need to mark a specific commit as a versioned release.
Steps:
-
List Existing Tags:
git tag
Example output:
v1.0 v1.1
-
Create a New Tag:
git tag -a v2.0 -m "Version 2.0 release"
Adds an annotated tag with the message “Version 2.0 release.”
-
Push the Tag to the Remote:
git push origin v2.0
-
Push All Tags to the Remote:
git push --tags
-
Delete a Tag Locally:
git tag -d v1.1
-
Delete a Tag Remotely:
git push origin --delete v1.1
3. Interactive Rebase
Scenario: Your branch history has multiple commits, and you want to clean it up by squashing redundant commits and rewording messages.
Steps:
-
Start an Interactive Rebase for the Last 3 Commits:
git rebase -i HEAD~3
-
Modify Commit History:
-
The editor will open with a list of commits:
pick abc1234 Add feature X pick def5678 Fix typo pick ghi9012 Improve feature X
-
Change the actions:
pick abc1234 Add feature X squash def5678 Fix typo reword ghi9012 Improve feature X
-
Save and close the editor.
-
-
Resolve Any Conflicts (If Necessary):
-
If there are conflicts, Git will pause the rebase and notify you.
-
Resolve the conflicts in files, stage them, and continue:
git add resolved_file git rebase --continue
-
-
Abort Rebase (If Needed):
-
If the rebase process is too complex, you can cancel it:
git rebase --abort
-
4. Cherry-Picking
Scenario: You’ve identified a useful commit in another branch and want to apply it to your current branch without merging the entire branch.
Steps:
-
Identify the Commit Hash:
git log
Example output:
commit a1b2c3d: Add new feature
-
Cherry-Pick the Commit:
git cherry-pick a1b2c3d
-
Handle Conflicts (If Any):
-
If conflicts occur, resolve them, stage the changes, and continue:
git add resolved_file git cherry-pick --continue
-
-
Abort Cherry-Pick (If Necessary):
git cherry-pick --abort
5. Working with Remotes
Scenario: You’ve cloned a repository and want to connect it to a new remote.
Steps:
-
Add a New Remote:
git remote add origin https://github.com/user/repo.git
-
List Remote URLs:
git remote -v
Example output:
origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push)
-
Fetch Updates from the Remote:
git fetch origin
-
Pull Updates with Rebase:
git pull origin main --rebase
-
Remove an Unused Remote:
git remote remove origin
6. Git Aliases
Scenario: You frequently type long Git commands and want to create shortcuts for convenience.
Steps:
-
Create Aliases:
git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch
-
Use the Aliases:
git st # Shortcut for 'git status' git co feature # Shortcut for 'git checkout feature' git br # Shortcut for 'git branch'
7. Bisecting to Find Bugs
Scenario: A recent commit introduced a bug, and you want to identify which one caused it.
Steps:
-
Start Bisecting:
git bisect start
-
Mark the Current Commit as Bad:
git bisect bad
-
Mark a Known Good Commit:
git bisect good a1b2c3d
-
Test Commits Suggested by Git:
-
Compile your code or run tests.
-
Mark the commit as bad or good:
git bisect bad git bisect good
-
-
Finish Bisecting:
git bisect reset
8. Cleaning Untracked Files
Scenario: Your working directory is cluttered with untracked files you no longer need.
Steps:
-
Preview Files to Be Deleted:
git clean -n
Example output:
Would remove untracked_file.txt Would remove build/
-
Remove Untracked Files:
git clean -f
-
Remove Untracked Files and Directories:
git clean -fd
Note: It’s a good practice to preview the changes using the -n flag before executing commands that remove files or directories, to ensure no unintended data is deleted.
Enjoy Coding !!!