Advanced Git Commands
- Sairam Penjarla
- Jul 19, 2024
- 3 min read
Once you're comfortable with the basics of Git, you might want to explore some of the more advanced commands that can help you manage your projects more effectively. Here’s a list of additional Git commands and their uses:
Branching and Merging
git branch
What it does: Lists all branches in your repository.
Usage: git branch
git branch [branch-name]
What it does: Creates a new branch.
Usage: git branch new-feature
git checkout [branch-name]
What it does: Switches to the specified branch.
Usage: git checkout new-feature
git merge [branch-name]
What it does: Merges the specified branch into the current branch.
Usage: git merge new-feature
git branch -d [branch-name]
What it does: Deletes the specified branch.
Usage: git branch -d new-feature
Stashing
git stash
What it does: Stashes your current changes and reverts your working directory to the last commit.
Usage: git stash
git stash list
What it does: Lists all stashed changes.
Usage: git stash list
git stash apply
What it does: Applies the latest stashed changes.
Usage: git stash apply
git stash drop
What it does: Deletes the latest stashed changes.
Usage: git stash drop
Rebasing
git rebase [branch-name]
What it does: Reapplies commits on top of another base tip.
Usage: git rebase main
git rebase --continue
What it does: Continues the rebasing process after resolving conflicts.
Usage: git rebase --continue
git rebase --abort
What it does: Aborts the rebase operation and returns to the original branch.
Usage: git rebase --abort
Resetting
git reset [file]
What it does: Unstages the specified file but retains its changes in the working directory.
Usage: git reset sample.py
git reset --soft [commit]
What it does: Moves the HEAD to the specified commit and keeps changes staged.
Usage: git reset --soft HEAD~1
git reset --hard [commit]
What it does: Moves the HEAD to the specified commit and discards all changes in the working directory.
Usage: git reset --hard HEAD~1
Remotes
git remote -v
What it does: Lists all configured remote repositories.
Usage: git remote -v
git remote add [name] [url]
What it does: Adds a new remote repository.
Usage: git remote add origin <https://github.com/your-username/my-first-repo.git>
git remote remove [name]
What it does: Removes the specified remote repository.
Usage: git remote remove origin
Viewing and Comparing
git log --oneline
What it does: Shows a simplified view of the commit history.
Usage: git log --oneline
git diff
What it does: Shows the differences between your working directory and the index.
Usage: git diff
git diff --staged
What it does: Shows the differences between the index and the last commit.
Usage: git diff --staged
git show [commit]
What it does: Shows the changes in a specific commit.
Usage: git show HEAD
Tags
git tag
What it does: Lists all tags in the repository.
Usage: git tag
git tag [tag-name]
What it does: Creates a new tag.
Usage: git tag v1.0.0
git push origin [tag-name]
What it does: Pushes a tag to the remote repository.
Usage: git push origin v1.0.0
git push origin --tags
What it does: Pushes all tags to the remote repository.
Usage: git push origin --tags
Undoing Changes
git revert [commit]
What it does: Creates a new commit that undoes the changes in a specified commit.
Usage: git revert HEAD
git checkout -- [file]
What it does: Discards changes in the working directory for the specified file.
Usage: git checkout -- sample.py
Conclusion
These advanced Git commands offer more control and flexibility in managing your code and collaboration processes. As you become more comfortable with Git, integrating these commands into your workflow can help you handle more complex scenarios with ease. Happy coding!