-
git commit -am "Add comment here"
add and commit all changed files in one code by using-am
flag -
git config --global alias.ac "commit -am"
create new custom commands calledac
that adds all changed files and commits (commit -am
) -
git commit --amend -m "New message"
update comments of the last commit using--amend -m
flag -
git add .
git commit --amend --no-edit
update the last commit with new files using--amend
flag, while keeping the old comment using--no-edit
flag
NOTE: this only works if commmit has not been pushed to remote repo -
git revert {commit hash}
go back to the previous state without removing the last commit -
git stash
git stash pop
remove changes without committing to repo (stash
) and add them (changes) back when ready to continue working (stash pop
)
-
git checkout {branch name}
switch to branch
NOTE: to go to previous branch you have checked out from use-
flag instead of branch name -
git branch -M new_name
rename branch withnew_name
using-M
flag -
git fetch origin
git reset --hard origin/master
git clean -df
when local repo is messed up and want to go back to original state from remote repo:- get latest code from remote repo with
fetch
, - overwrite local code with the fetched remote code using
--hard
flag withreset
- remove untracked files or build artifacts with
clean
- get latest code from remote repo with
-
git rebase master --interactive
pull up a file with a list of commits on this branch -
git branch -d {branch}
delete a branch using-d
flag
-
git log --graph --oneline --decorate
desplay log in more concise fashion -
git log --pretty=format:"%h - %an, %ar : %s"
specify your own log output using format specifiers (e.g.%h
) Specifier Description of Output%H
- Commit hash
%h
- Abbreviated commit hash
%T
- Tree hash
%t
- Abbreviated tree hash
%P
- Parent hashes
%p
- Abbreviated parent hashes
%an
- Author name
%ae
- Author email
%ad
- Author date (format respects the --date=option)
%ar
- Author date, relative
%cn
- Committer name
%ce
- Committer email
%cd
- Committer date
%cr
- Committer date, relative
%s
- Subject
$ git push origin --delete <branch>
# Git version 1.7.0 or newer
$ git push origin :<branch>
# Git versions older than 1.7.0
$ git branch --delete <branch>
$ git branch -d <branch>
# Shorter version$ git branch -D <branch>
# Force delete un-merged branches
$ git branch --delete --remotes <remote>/<branch>
$ git branch -dr <remote>/<branch>
# Shorter
$ git fetch <remote> --prune
# Delete multiple obsolete tracking branches
$ git fetch <remote> -p
# Shorter