Notes
Git in Production
git commands i use at work, a pretty log alias, amending commits, merge vs rebase for feature branches and more
Git & Github
5 minutes
Pretty and concise logs
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
Change commit message of last commit
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
Add a file i forgot in last commit
1git commit --amend
When you run the command it’ll automatically include the newly modified files. Only do this for unpushed code
Update 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 mergeis recommended when you have a shared branch and other developers are working on it as wellgit rebasewill get all changes frommasterand merge it on top of your feature branch. Doing so will change the commit hashes.git rebasewill rewind head and replay our work on top of it.
Move accidental commits from 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
Combine multiple commits into one
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
-iis for inetractiveXXXis the commit from where you want to start and update commits that came after this point
See the changes in local commits (with diff)
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 empty branches
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
Save dirty copy of your work in progress
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
popwill take the stash out ofgit stash list,applywould not.- by default
applywill get back the the most recent stash
See changes between your file and remote file (with diff)
1git diff HEAD myFile.js
compare two different commits
1git diff XXXX YYYY
Wipe a commit from local
1git reset --hard XXXX
- will jump back to the
XXXXcommit discarding all the ones that came after it - make sure you’re getting rid of only local commits and that the cahnges haven’t already been pushed
Delete a commit from remote
1# 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
- keep in mind that
--hardthrows away all your uncommitted changes. to use--hardfirst make sure the output ofgit statusis clean
Undo commits that have already been pushed to remote (with revert)
1git revert XXXX
resetandamendwill 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.revertdoes not change the commit hash, which is why it is a good command for getting rid of changes that have already been pushed to remote
See only local unpushed changes in current branch
1git log origin/master..HEAD
2
3# you can view diff using the same syntax
4git diff origin/master..HEAD
See all local merge conflicts
1git status
Squash all commits into one and merge to master
1git checkout master
2git merge --squash fooBranch