top of page

Advanced Git Commands

  • Writer: Sairam Penjarla
    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

  1. git branch

  • What it does: Lists all branches in your repository.

  • Usage: git branch

  1. git branch [branch-name]

  • What it does: Creates a new branch.

  • Usage: git branch new-feature

  1. git checkout [branch-name]

  • What it does: Switches to the specified branch.

  • Usage: git checkout new-feature

  1. git merge [branch-name]

  • What it does: Merges the specified branch into the current branch.

  • Usage: git merge new-feature

  1. git branch -d [branch-name]

  • What it does: Deletes the specified branch.

  • Usage: git branch -d new-feature

Stashing

  1. git stash

  • What it does: Stashes your current changes and reverts your working directory to the last commit.

  • Usage: git stash

  1. git stash list

  • What it does: Lists all stashed changes.

  • Usage: git stash list

  1. git stash apply

  • What it does: Applies the latest stashed changes.

  • Usage: git stash apply

  1. git stash drop

  • What it does: Deletes the latest stashed changes.

  • Usage: git stash drop

Rebasing

  1. git rebase [branch-name]

  • What it does: Reapplies commits on top of another base tip.

  • Usage: git rebase main

  1. git rebase --continue

  • What it does: Continues the rebasing process after resolving conflicts.

  • Usage: git rebase --continue

  1. git rebase --abort

  • What it does: Aborts the rebase operation and returns to the original branch.

  • Usage: git rebase --abort

Resetting

  1. git reset [file]

  • What it does: Unstages the specified file but retains its changes in the working directory.

  • Usage: git reset sample.py

  1. git reset --soft [commit]

  • What it does: Moves the HEAD to the specified commit and keeps changes staged.

  • Usage: git reset --soft HEAD~1

  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

  1. git remote -v

  • What it does: Lists all configured remote repositories.

  • Usage: git remote -v

  1. git remote add [name] [url]

  1. git remote remove [name]

  • What it does: Removes the specified remote repository.

  • Usage: git remote remove origin

Viewing and Comparing

  1. git log --oneline

  • What it does: Shows a simplified view of the commit history.

  • Usage: git log --oneline

  1. git diff

  • What it does: Shows the differences between your working directory and the index.

  • Usage: git diff

  1. git diff --staged

  • What it does: Shows the differences between the index and the last commit.

  • Usage: git diff --staged

  1. git show [commit]

  • What it does: Shows the changes in a specific commit.

  • Usage: git show HEAD

Tags

  1. git tag

  • What it does: Lists all tags in the repository.

  • Usage: git tag

  1. git tag [tag-name]

  • What it does: Creates a new tag.

  • Usage: git tag v1.0.0

  1. git push origin [tag-name]

  • What it does: Pushes a tag to the remote repository.

  • Usage: git push origin v1.0.0

  1. git push origin --tags

  • What it does: Pushes all tags to the remote repository.

  • Usage: git push origin --tags

Undoing Changes

  1. git revert [commit]

  • What it does: Creates a new commit that undoes the changes in a specified commit.

  • Usage: git revert HEAD

  1. 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!

 
 

Sign up for more like this.

Thanks for submitting!

bottom of page