git commit -am “message”
git commit -am "message
Adds all the files in current working directory
Alias
git config --global alias.ac "commit -am"
# How to use it: > git ac "message"
Reword the last commit message
git commit --amend -m "new message"
Also useful when you forgot to include or stage a couple of files with last commit.
git commit --amend --no-edit #if want no change in prev commit message
This only works until before you havent alreday pushed your code to a remote repo.
Git push with –force (living dangerously)
git push origin master --force
This will overwrite the remote commits with state of your local code. (example if you wanted to correct the wording to be updated at remote too, because you earlier pushed the incorrect message - but beware, doing this will result in following:
- if you have commits on the remote branch that you dont have locally, they will be lost on remote! and your coworkeres wony be too happy about it.
When pushed some garbage code to repo remote, and realized it should never have been on remote
git revert <commit>
If want to work on a repo, but not have local machine
GitHub > open a repo > press .
Opens a code editor.
Can do almost everything, cant use terminal
Github codespace to use terminal
creates terminal in cloud.
Git Stash
to temp ‘stash’ (not ready to commit yet)
git stash
later when ready
git pop
Better Usage
git stash save name1
now you have a name for your stash
you can see all stashed by:
git stash list
git stash apply 0
Better reading git log
git log --graph --oneline --decorate
Git bisect
– didnt get fully
Squashing commits
git rebase master --interactive
opens an editor:
pick asdfasdf
squash qwerqsdfa
squash etiuryuir
squash fdkmfkmpd
Then set the new commit message
If you can use fixup instead of squash when doing rebase.
Git hooks
Whenever you perform an operation with Git, like a commit for e.g., it will create an event
hooks allow you to run code either before or after the event
see .git > hooks
- find many hooks there
For JS devs, there is a package Husky that makes it easier to configure hooks
example: you can configure to validate / lint your code before every commit.
Go back to original state of remote repo.
Because you made some changes in local, and things are not going well, and you want to start again.
git fetch origin
git reset --hard origin/master
Be careful! your local changes will be lost forever.
go back to the previous branch, if dont remember it after changing branch
git checkout -