Tuesday, June 12, 2018

GIT cheat sheet

GIT shortcuts $ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit $ git config --global alias.st status $ git config --global alias.unstage 'reset HEAD --' $ git config --global alias.last 'log -1 HEAD' push a local commit to server $ git push origin master:remote_branch_name check out a remote branch to local and start work on it, and have this local copy 'linked' to the remote branch $ git checkout --track origin/gradle diff a file between two commits $ git diff -w 9655d51..c48be72 -- src/uniflow/plate.java Revert a local uncommited change to its original content as in last commit $ git checkout -- file.java // same as svn revert Undo the last commit (change the files to the previous commit state, and make a new commit) $ git revert HEAD Unstage a file (keep the working file content unchanged, including these unstaged changes) $ git reset file Remove two local commits (last two commits will be removed, don't do this to published commits) $ git reset --hard HEAD~2 $ git reset --hard // obliterates all uncommitted changes. Modify previous commit (e.g. commit additional files) or commit message $ git commit --amend Reset local branch to match exactly the remote branch $ git checkout develop // select the target branch to reset $ git fetch origin $ git reset --hard origin/develop remove untracked files $ git clean -n // dry run, just list files to be removed $ git clean -f // force remove untracked files $ git clean -df // force remove both untracked files and directories Merge multiple local commits into one commit $ git rebase -i origin/master pick 16b5fcc Code in, tests not passing squash c964dea Getting closer squash 06cf8ee Something changed squash 396b4a3 Tests pass squash 9be7fdb Better comments squash 7dba9cb All done Note: change all 'pick' to 'squash' except for the first line in the pop up editor merge multiple commits locally $ git rebase -i merge last two commits $ git rebase -i HEAD~2 Create a new local branch $ git checkout -b experimental_tilde Abort rebase $ git rebase --abort Delete a local branch $ git branch -d experimental_tilde Delete a remote branch $ git branch -d -r origin/brach_name Daily synch from remote master to local feature branch # assume you are on local feature branch # stash the local uncommited changes $ git stash # pull in latest changes from master $ git co master $ git pull # switch to your feature branch $ git co myfeature # rebase the feature branch on top of master, alternatively one could merge (see https://stackoverflow.com/questions/13006135/copy-last-commits-from-master-to-branch) $ git rebase master # reapply your previously saved local changes. $ git stash pop Add feature branch changes to master thru rebase. Simple version, see next for more options $ git checkout feature $ git checkout -b temporary-branch # create temp branch for rebaseing $ git rebase -i master # [Clean up the history] $ git checkout master $ git merge temporary-branch # put the changes back to master. $ git push Rebase feature branch onto master (https://randyfay.com/content/rebase-workflow-git) Assume you have a master branch for shared work, and a personal branch called feature. $ git checkout feature # feature branch to do local work $ # do some local commits on this branch. $ git fetch origin # pull changes from remote $ git rebase -i master # plop our branch changes onto everyone else’s. There might be conflicts, need to resolve and resume the rebase using git rebase --continue # now our branch has the latest ideal code with all the local and remote changes. Need to publish it. We will switch to master and make it the same as our rebased branch. $ git checkout master # switch to master branch # there are a few ways to push changes back to remote 1. $ git rebase feature # now master is fast-forwarded to have feature commits on it 2. $ git merge feature # same effect as option 1 rebase 3. $ git merge --squash feature # squash will merge all branch commits into one onto the master develop branch 4. $ git merge --no-ff feature # explicitly document the merge by adding a merge commit $ git push origin master # push the master change back to the remote Find out number of file changes between two tags, exclude type-change items $ git diff v6.1.0001..v6.1.0003 --name-status --diff-filter=t | wc -l Find the commits between two tags $ git log v6.1.0001..v6.1.0003 --pretty=oneline Move the last commit to a new feature branch # so you have been working on master branch although on a feature, and committed some thing locally, however it really belongs to a feature branch, and needs more work. Here is a way to create that new branch with the commit but back out the commit from the master branch. # assume you are currently on the master branch $ git checkout -b new_branch # make a new branch containing the last commit $ git checkout master $ git reset —hard HEAD~1 # move the master head back one commit, now that commit only lives in the new branch created. $ git checkout new_branch # move to the new branch to continue working on it. Git password free $ cd $ git config credential.helper store $ git fetch