This content originally appeared on HackerNoon and was authored by Dmytro Sichkar
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.
git restore
The git restore
command is handy for discarding changes in the working directory.
# To discard changes in a file, use git restore <file|directory>
git restore path/to/file
# To restore a file to a specific commit, use git restore - source <commit> <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.
git reset
This command undoes the last Git commit or moves the HEAD pointer back one commit but keeps the changes in the working directory.
# Undo only the latest commit
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.
\
# Check your history befor reset
git log --oneline
# 3fad532 Last commit (HEAD)
# 3bnaj30 Commit before HEAD (HEAD~1)
# vcn3ed5 Two commits before HEAD (HEAD~2)
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.
git stash
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.
# Stash all modified files
git stash
# Stash with interactive select
git stash --patch
# Apply the latest stashed changes
git stash apply
# Apply the latest stashed changes and remove it from the stash list
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.
git worktree
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.
# Your current directory structure
# /me/projects/
# ├── my-awesome-project (repo that we are creating the worktree for)
# └── my-another-project
# Navigate to your project directory
cd /me/projects/my-awesome-project
# Create a new working directory for a specific branch
git worktree add ../my-awesome-project-hotfix-123 hotfix-123
# Navigate to the new working directory
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.
git tag
Tags are useful for marking specific points in your commit history. There are two types of tags: annotated and lightweight.
- Annotated Tag: Use to create an annotated tag. Annotated tags are stored as full objects in the Git database and include the tagger's name, email, date, and a tagging message.
- Lightweight Tag: Use to create a lightweight tag. These are basically pointers to a specific commit and don't store any extra metadata.
# Annotated Tag
git tag -a v1.0.0 -m "Version 1.0"
# Lightweight Tag
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 made a mistake.
git remote
The git remote
command lets you manage your remote repositories.
- Use
git remote add <name> <url>
to add a new remote. - Use
git remote -v
to list all configured remotes.
# Add a new remote repository
git remote add origin https://github.com/username/repo.git
# List all configured remotes
git remote -v
# Example output:
# origin https://github.com/username/repo.git (fetch)
# origin https://github.com/username/repo.git (push)
\ SSH Connection Settings: If you prefer using SSH for your remote connections, you can set up your remote URLs to use SSH.
# Typical SSH URL
git remote add origin git@github.com: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.
# Format the URL to include the port number
git remote add origin ssh://git@github.com: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!
This content originally appeared on HackerNoon and was authored by Dmytro Sichkar
Dmytro Sichkar | Sciencx (2024-08-28T01:04:54+00:00) Git Commands You’re Always Forgetting. Retrieved from https://www.scien.cx/2024/08/28/git-commands-youre-always-forgetting/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.