Bangda Sun

Practice makes perfect

Bash and Git Quick Reference

Bash and Git Cheatsheet.

This is a summary of bash and git commands that I usually need during the work. I will update it once I have new commands.

1. Bash

Bash is a Unix shell and command language. It has been distributed widely as the default login shell for most Linux distributions and Apple’s macOS. There is also a version available for Windows 10 now.

1.1 System

  • check disk usage for each file: du -h
  • check disk usage for top level directory: du -h -d1
  • check disk usage for whole directory (summary): du -sh
  • get command history: history
  • check current running jobs: top
  • check current running jobs of specific user: top -u username
  • kill a process: kill -9 pid
  • list files in current directory: ls
  • list all files (include folders start with .): ls -a
  • list files in folders (sub-folder) in current directory: ls sub-folder
  • list top files (largest or most recent, etc): ls -l | head -n 5
  • list jobs running on specific port number: lsof -i :xxxx
  • sort the file by size: ls -S -l
  • count number of files in directory: ls dir | wc -l

1.2 File manipulation

  • go to directory target-dir: cd target-dir
  • back to last directory: cd ..
  • open all files in one folder: open folder/*
  • remove file: rm file
  • remove folder: rm -r folder
  • create a new directory: mkdir dir
  • copy file to another directory target-dir: cp file target-dir
  • copy folder to another directory target-dir: cp -r folder target-dir
  • copy file from remote server: scp username@remote:file-path local-file-path
  • move file to another directory target-dir: mv file targetdir/
  • rename file: mv file new-file
  • compress file or folder (tar): tar -zcvf folder.tar.gz folder
  • compress folder (zip): zip -r filename.zip folder
  • decompress zip file: unzip filename.zip
  • edit text file or create text file use vim: vi file.txt
  • view text file in command prompt in case of large file: more file.txt
  • count number of lines written in file: wc -l file.csv
  • preview spreadsheet or other files (nth line): head -n file.csv
  • find file: find dir -name filename
  • list files in directory: find dir
  • search text in file: grep query filename

1.3 Other

  • for loop: for i in {n..m}; do [ ]; done
  • output log file when running python script: python script.py > script_log.log
  • clear console: clear

2. Git

Git is a version control system for tracking changes in files and coordinating work on those files among multiple people.

2.1 System

  • set user name and email: git config --global user.name name, git config --global user.email email
  • check git history: git log
  • clone a repo: git clone repo-location

2.2 File changes

  • check all file modifications: git status
  • check modification details: git diff
  • view a commit: git show commit-id
  • add files: git add [file]
  • add all files: git add --all
  • commit the changes: git commit -m "description"
  • don’t save file (back before code change, usually do this before make commits): git checkout filename
  • check not pushed commits: git diff origin/branch-name..HEAD

2.3 Branch

  • check branch of the repo: git branch
  • see all branch of the repo: git branch -a
  • create a new branch: git branch branch-name
  • switch to new branch: git checkout branch-name

2.4 Synchronize file changes

  • push to remote repo: git push origin branch-name
  • update commit from remote repo: git fetch
  • update commit from remote repo and merge with local changes (specific branch): git pull origin branch-name
  • save works done in current branch (switch to another branch), or pull from other branch but want to add the local changes:git stash and git stash pop
  • rollback to particular commit: git reset --hard commit-id