Useful terminal commands.
Control W - remove previous word
Control C - Cancel the line
Control U - clear line.
ls -tr - sort by time in reverse where recent at bottom. Otherwise just ls -t
ls -ltr - more specific with time stamp and everything.
mkdir (make directory)
rmdir file(if within file it’s not empty)
rm -rf file(remove everything in file) - rf stand for recursive force
git checkout -f (go back to previous version and undo the changes)
git mv (to change name)
git commit -am (a = all changes, m = messages)
Move to the start of line. Ctrl + a. 2. Move to the end of line. Ctrl + e. 3. Move forward a word. Meta + f (a word contains alphabets and digits, no symbols) 4. Move backward a word. Meta + b. 1. ctr + w delete previous word, ctr + u delete whole line. 5. Clear the screen. Ctrl + l. 6. Rails commands 1. rails s 1. ctrl c to shut down
Git Commands:
GitBook Review and Commands:
Chapter 1: Basics
- Clone repo and rename : git clone
- Determine which files are in which state: git status.
- Use git status -s for a shorter and concise version of status.
- To add a file, use command : echo <“whatever needs to contain in file”> > . Eg. echo “Anh” > README
- To open the file and modify, use command: vim
- To track new files and move it to stage, use: git add . If directory, includes all files within that directory.
- Can also do git add * , which will staged everything.
- Don’t want git to add automatically, use: cat .gitignore, *.[oa], *~ - ignore all files ending in .o or .a (matching pattern). Also ignore files ending with ~.
- Want to know exactly what you changed, not just which files have been changed, use git diff instead of status: git diff
- Ready to commit your changes, use: git commit. This command will pop up default text editor so you can enter the messages of the changes. Otherwise if short message can do: git commit -m
- If want to skip staging area (aka skipping git add), use git commit -a -m
- To remove file from git: git rm
- Use rm to remove file from working directory, it’ll show up under “Changes not staged for commit”. Run git rm, it’ll then staged the file’s removal.
- To force removal, add -f option (If you modified file and added it to staging area already and want to remove)
- Keep file on working tree but remove from staging: git rm --cached
- Rename a file name, do: git mv <file_from> <file_to>
- To check out all the commits you’ve made, do: git log
- Add, -p or -patch to show differences (patch output) introduced in each commit
- Add -2 to show only 2 entries or whatever number of entires you want.
- Add --stat to see some abbreviated stats for each commit
- Add --pretty changes log output to formats other than default
- e.g. git log -pretty=oneline, or use short, full, and fuller options
- Most useful is format: git log —pretty=format:”%h - %an, %ar : %s”
- h = abbreviated commit hash, an = author name, ar = author date, s = subject. (look at list for more options)
- Can also do git log —since=2.weeks (to get last 2 weeks)
- Undoing things, if you want to redo that commit, make additional changes you forgot, stage them and commit again using: git commit --amend
- To unstage a file, use: git reset HEAD
- To unmodified a file and go back to the original working directory, do: git checkout --
- To see which remote servers you have configured, run: git remote
- Add option -v, which shows URL that git has stored for the shortname to be used when read/write to that remote.
- To add a new remote Git repo, do : git remote add
- To get data from remote projects, run: git fetch
- Use git pull to automatically fetch and then merge remote branch to current branch.
- Ready to share and push it upstream, do: git push
- To inspect a remote, do: git remote show
- Renaming and Removing remotes, do git remote rename and git remote remove
- List all the tags (or version Numbers), do git tag, add option -l for more detail.
- Creating annotated tags: git tag -a -m . E.g. git tag -a v1.0 -m “First version”
- Tagging doesn’t impact remote servers, to do so, you have to do: git push . Do git push --tags to push all tags to remote.
- Creating aliases for git commands: instead of typing git commit, u can type git ci if you do git config --global alias.ci commit
Chapter 2: Git Branch Knowledge.
- See current branches, do: git branch
- Create new branch: git branch
- To create new branch and switch to it at same time, add -b option, e.g. git checkout -b
- Switch branch: git checkout
- Merge branch into master -> git checkout master, then git merge
- To delete the branch (e.g. after you fix and deploy the work already), do: git branch -d
- To see last commit on each branch, add option -v, eg. git branch -v
- To filter list to branches that you have or have not yet merged into branch currently on, do: git branch --merged or --no-merged
- If you want to delete unmerged branch, it will fail, have to do : git branch -D , capitalize D.
- If you want to check from another branch, do git branch --merged
- To synchronize someone’s else push, you do git fetch <name (e.g. origin)>
- To add a remote branch, do git remote add
- If you want to push to remote, you do git push e.g. git push origin serverfix
- If you push to an HTTPS url, you will be ask for username/pass, to skip this for few mins, do git config —global credential.helper cache
- If you don’t want the remote to be name same as local, do: git push localBranch:newName
- When doing git fetch, you don’t have a working copy yet, to get one, do: git merge <currentBranch/remoteBranch>
- shorthand for tracking e.g. git checkout -b / to git checkout —track /
- An even shorter shortcut: git checkout
- Set up local branch with different name than remote, do: git checkout -b /
- Branch serverfix set up to track remote branch serverfix from origin.
- Set local branch to remote branch that just pulled down, or change upstream branch , do git branch -u /
- Upstream Shorthands: git merge @{u} instead of git merge origin/master
- To see what tracking branches you have set up, do -vv option to git branch such as git branch -vv
- These are just output from the last time you fetched from each server, to fully be update, do git fetch --all; git branch -vv
- To see what tracking branches you have set up, do -vv option to git branch such as git branch -vv
- To delete a remote branch, do git push origin --delete serverfix (deleting serverfix branch).
- You can take patch of one change and replay on top of another branch, this is call rebasing (similar to merge, but not), you do git rebase . eg. git checkout experiment, then git rebase master where experiment and master are branches.