The "OhSnap!" series explores bite-sized tips that you can apply today.
Here is a list of some helpful terminal and git commands for your viewing pleasure and my own future reference. 😉
Terminal Commands
Working With Spaces
cd Class\ Work
cd 'Class Work'
Use a backslash (
\
) to make "Class\ Work" equal "Class Work”.Encapsulate the phrase with quote marks to preserve spaces between words.
Open project folder in VS Code
cd my-project-folder-name
code .
Only show directories
ls -d */
Clear the terminal
cmd + k
clear
Go back to the main directory
cd ~
Show permissions
ls -l
Create a file
touch index.html
Create a folder/directory
mkdir my-project
Removes a file
rm index.html
Remove a folder
rm -r my-project
-r
stands for “recursive”. It means to delete this thing and everything in it.
Copy a file/folder
cp img images
The first value is the original file/folder and the second value is what it is copied as.
Rename a file/folder name
mv old-name.html new-name.html
The first value is the original file/folder name and the second value is the new name.
Open folder in Finder
open .
Getting unstuck from hard-to-exit terminal screens
shift + z
shift + z
Git Commands
Show available local branches
git branch
Create new branch
git checkout -b feature-1
-
checkout
moves to the new branch -
-b
creates a new branch -
feature-1
name for branch
Push local branch to remote
git push origin feature-1
Switch to a new branch
git checkout main
Merge changes from one branch to another
git merge master
- Navigate to the branch you want to work in
- After the
merge
keyword, specify the branch that has the changes to be merged from
Grab all remote branches without merging
git fetch --all
Reset the local master with the remote version
git reset --hard origin/master
Remove non-committed changes from all branches and master since
git checkout -f
View differences between commits
git diff
To leave this view hit Q
on the keyboard
Thumbnail designed with Figma
Top comments (3)
Couple of tips:
cmd + k
is going to be specific to MacOS.ctrl + l
will work on pretty much any terminal emulator and is also often bound to things like "redraw" in apps as well.cd ~
will take you to your home directory, but also so willcd
on its own.Opening the current directory in apps, like with
code .
andopen .
is the same as passing the complete current path.open
is a Mac-specific (in this case) program which dispatches the path parameter to whatever application is associated with "folder" (again, in this case). On Linux you'd do something likexdg-open
if you have a GUI, or even something else depending on your configuration. On Windows I suspect you're SOL.This means if you have something associated with
.js
files in your GUI (e.g. Finder) thenopen foo.js
will launch that application, regardless of what it is. In that case,open
is a tiny bit easier because you can share code using it with others and it'll work even if they use something else like Sublime.I have a whole rant about using touch to create files.
I'm guessing you have
less
as your pager for git diffs. It doesn't have to be that way though, and even if you do there's a good chance it's not called if there's nothing to diff!What do you mean by this? What terminal screens are getting stuck? The only time I know ZZ as a shortcut it's an alternative way to save and exit Vim.
Thanks for leaving feedback and additional explanations. Regarding your question at the end, I do believe the screen was a VIM editor. It sometimes comes up when I am working with git.
Gotcha.
If you're using Vim to edit git commit messages and don't want to, I think there's a git config setting. Failing that, I imagine setting something like;
in your shell config would let you use VS Code instead (or whatever you want)