Did you find yourself cursing at your terminal because you forgot that one Git command? Yeah, me too. So, I decided to put down these Git commands I always forget. Maybe this note-to-self will save you some frustration too.
The git restore
command is handy for discarding changes in the working directory.
git restore path/to/file
git restore --source b982ca3 path/to/file
Use Case: If you've accidentally made changes to a file (e.g., by a code formatter) but then decide you don't want to keep them (like reformatting the entire project), git restore <file|directory>
will revert the file (or full directory) back to the state of the last commit. But be careful because there is no revert for this action.
This command undoes the last Git commit or moves the HEAD pointer back one commit but keeps the changes in the working directory.
git reset --soft HEAD~1
If you are not familiar with this notation, HEAD~1
means that you want to reset the HEAD (the last commit) to one commit before in the log history.
git log --oneline
Use Case: You can use this when you've committed too early and want to add more changes to your last commit. After running the command, your changes will be unstaged, and you can stage additional changes before committing again.
Stashing saves your local changes so you can work on something else without committing the changes. The - patch
flag allows you to select which changes to stash interactively.
git stash
git stash --patch
git stash apply
git stash pop
Use Case: If you're in the middle of a task and suddenly need to switch contexts, git stash
lets you stash specific changes interactively, so you don't have to stash everything at once.
The command allows you to have multiple working directories with different branches checked out. This is an alternative to using git stash
when you want to work on another branch without losing your current changes. Use git worktree add <path> <branch>
to create a new working directory with a specific branch.
cd /me/projects/my-awesome-project
git worktree add ../my-awesome-project-hotfix-123 hotfix-123
cd ../my-awesome-project-hotfix-123
Use Case: If you need to work on multiple branches simultaneously, git worktree
is perfect. It prevents you from having to constantly checkout branches back and forth.
Tags are useful for marking specific points in your commit history. There are two types of tags: annotated and lightweight.
git tag -a v1.0.0 -m "Version 1.0"
git tag v1.0.0
Use Case: Tags are perfect for marking releases. I prefer using lightweight tags because you can easily remove one if you make a mistake.
The git remote
command lets you manage your remote repositories.
git remote add <name> <url>
to add a new remote.git remote -v
to list all configured remotes.git remote add origin https://github.com/username/repo.git
git remote -v
SSH Connection Settings: If you prefer using SSH for your remote connections, you can set up your remote URLs to use SSH.
git remote add origin [email protected]:username/repo.git
Setting Up Non-Default SSH Port in the URL:
To use a non-default SSH port in your Git remote URL, you can specify the port directly in the URL.
git remote add origin ssh://[email protected]:2222/username/repo.git
Use Case: Setting up SSH ensures secure connections to your remote repositories. Configuring a non-default SSH port can enhance security and adapt to specific server configurations. I hope this little cheat sheet helps the next time you're stuck. Keep coding, and happy Git-ing!