DEV Community

loading...
Cover image for OhSnap! Handy Terminal & Git Commands

OhSnap! Handy Terminal & Git Commands

gedalyakrycer profile image Gedalya Krycer ・2 min read

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
Enter fullscreen mode Exit fullscreen mode
cd 'Class Work'
Enter fullscreen mode Exit fullscreen mode
  • 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 .
Enter fullscreen mode Exit fullscreen mode

Only show directories

ls -d */
Enter fullscreen mode Exit fullscreen mode

Clear the terminal

cmd + k
Enter fullscreen mode Exit fullscreen mode
clear
Enter fullscreen mode Exit fullscreen mode

Go back to the main directory

cd ~
Enter fullscreen mode Exit fullscreen mode

Show permissions

ls -l
Enter fullscreen mode Exit fullscreen mode

Create a file

touch index.html
Enter fullscreen mode Exit fullscreen mode

Create a folder/directory

mkdir my-project
Enter fullscreen mode Exit fullscreen mode

Removes a file

rm index.html
Enter fullscreen mode Exit fullscreen mode

Remove a folder

rm -r my-project
Enter fullscreen mode Exit fullscreen mode

-r stands for “recursive”. It means to delete this thing and everything in it.


Copy a file/folder

cp img images
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The first value is the original file/folder name and the second value is the new name.


Open folder in Finder

open .
Enter fullscreen mode Exit fullscreen mode

Getting unstuck from hard-to-exit terminal screens

shift + z
shift + z
Enter fullscreen mode Exit fullscreen mode

Git Commands

Show available local branches

git branch
Enter fullscreen mode Exit fullscreen mode

Create new branch

git checkout -b feature-1
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Switch to a new branch

git checkout main
Enter fullscreen mode Exit fullscreen mode

Merge changes from one branch to another

git merge master
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the branch you want to work in
  2. After the merge keyword, specify the branch that has the changes to be merged from

Grab all remote branches without merging

git fetch --all
Enter fullscreen mode Exit fullscreen mode

Reset the local master with the remote version

git reset --hard origin/master
Enter fullscreen mode Exit fullscreen mode

Remove non-committed changes from all branches and master since

git checkout -f
Enter fullscreen mode Exit fullscreen mode

View differences between commits

git diff
Enter fullscreen mode Exit fullscreen mode

To leave this view hit Q on the keyboard


Thumbnail designed with Figma

Discussion (3)

Collapse
moopet profile image
Ben Sinclair

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 will cd on its own.

Opening the current directory in apps, like with code . and open . 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 like xdg-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) then open 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.

git diff
To leave this view hit Q on the keyboard

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!

Getting unstuck from hard-to-exit terminal screens

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.

Collapse
gedalyakrycer profile image
Gedalya Krycer Author

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.

Collapse
moopet profile image
Ben Sinclair

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;

export EDITOR=$(command -v code)
Enter fullscreen mode Exit fullscreen mode

in your shell config would let you use VS Code instead (or whatever you want)

Forem Open with the Forem app