add an alias
1nano ~/.gitconfig
[alias]
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --branches
You can now use git lg
to see better logs
1git commit --amend
It’ll open the editor and you’ll be able to update the last commit message. Only do this for unpushed code
1git commit --amend
When you run the command it’ll automatically include the newly modified files. Only do this for unpushed code
feature
with latest changes from master
Get changes from the main development branch.
1# switch to master and get latest changes
2git checkout master
3git pull
4
5# switch to feature and pull changes from master
6git checkout feature
7
8# merge is good when you have other devs working on the code
9# and the changes have been committed and pushed
10git merge master
11
12# rebase is ok if you're the only dev and as long as you haven't pushed yet
13# beware of rebase changing commit hashes
14git rebase
git merge
is recommended when you have a shared branch and other developers are working on it as wellgit rebase
will get all changes from master
and merge it on top of your feature branch. Doing so will change the commit hashes.git rebase
will rewind head and replay our work on top of it.master
to another branch 1# Changes haven't been pushed yet, and NOT synced with remote
2# otherwise you'll mess up commit history
3
4# create a new branch from master
5# since you have pushed your changes to master the new branch will have those changes
6git checkout master
7git checkout -b newBranch
8
9# go back to master and remove those accidental changes by going back
10# to the point where those commits weren't added
11git checkout master
12git reset --hard XXXXX
1git rebase -i XXX
2
3# select the actions you want to take for every commit, close editor and continue
You’ll get a summary of all the commits from XXX
onwards
-i
is for inetractiveXXX
is the commit from where you want to start and update commits that came after this pointdiff
)1git diff # unstaged changes
2git diff --staged # staged changes
3git diff HEAD # both staged+unstaged changes
4git diff --stat # summary of what files changed, no. of line +/- etc.
5git diff --check # check for merge conflicts
Create the Git branch as an orphan. That means it’ll have no ‘parent’, hence no code. All the code that is there can be removed with git rm --cahced -r .
1git checkout --orphan FOO
2
3# multiple ways of removing files..
4git rm -r --cached .
5git reset --hard
6git clean -fd
1git add . # capture all 'Work In Progress' files
2git stash # put it away
3
4# la la la, go to other branches, make changes, commit etc.
5
6# go back to the branch you were working in
7git stash pop # get it back, remove it from stash
1git stash list
2git stash apply # get it back, keep the stashed copy as well
3git stash apply X # apply the number 'X' stash from your list (e.g. stash@{1})
4git stash save 'wip: fancy feature' # stash it with a name
pop
will take the stash out of git stash list
, apply
would not.apply
will get back the the most recent stashdiff
)1git diff HEAD myFile.js
compare two different commits
1git diff XXXX YYYY
1git reset --hard XXXX
XXXX
commit discarding all the ones that came after it1# find your commit
2git log --oneline
3
4# reset will checkout the specified commit and remove everything that came after it
5git reset --hard XXX
6
7# push your changesto remote
8git push --force
9# git push origin +develop
--hard
throws away all your uncommitted changes. to use --hard
first make sure the output of git status
is cleanrevert
)1git revert XXXX
reset
and amend
will change the commit hash, which we don’t want, as it’ll cause conflicts since the code has already been pushed to remote and other people may have pulledit already.revert
does not change the commit hash, which is why it is a good command for getting rid of changes that have already been pushed to remote1git log origin/master..HEAD
2
3# you can view diff using the same syntax
4git diff origin/master..HEAD
1git status
1git checkout master
2git merge --squash fooBranch