Git Info

Frequent Git Commands I Use On Daily Basis


02 Aug 2016 View Comments
#repository #git #computer

git_info

GIT Info

First, here are two links that came handy for me to visualize on GIT:

Please note below commands are really meant for the reference for me, not everything may make sense. The commands are what I use often on daily basis.

Config

1. git config --global user.name "First Last"
2. git config --global user.email "Email address"
3. git config --global push.default simple

Merge

1. git checkout master
2. git pull
3. git checkout branch-name
4. git pull
5. git merge master

Similarly for the other way,

1. git checkout branch-name
2. git pull
3. git checkout master
4. git pull
5. git merge branch-name

Clone

1. git clone <url>

Create a branch

1. git branch <name>
2. git checkout -b <name>

Rebase - interactive mode

More info on rebase interactive mode

1. git rebase -i HEAD~#
2. option to squash, fixup, pick, reword

To move the entire feature branch to begin on the tip of the master branch,

1. git checkout feature
2. git rebase master

Commit

1. add the files you would like to commit.
2. git commit -m "message"
3. git log (check your commit)
4. git push origin <branch name>

Show

1. git show <commit-id>
2. git show will just show the last commit

Pull

1. git pull rebase - this does the fetch step, as well as updating your working tree by running rebase. Without rebase, it performs a merge instead.

Git pull is basically of fetch + merge/rebase. Fetch won’t update your working tree, but merge/rebase do.

Cherry-Pick

1. git cherry-pick -x -s aefc21dsafcdwascx5729

Which will include “Signed-off-by: " in commit message

Patch

1. git diff <base branch>..<current branch with changes> > out.patch
2. git diff <base branch>..<current branch with changes> --binary > out.patch (with binary files
3. On a base branch, run: git pull
4. git checkout -b <branch name>
5. git apply out.patch

Aliases

1. git config --global alias.s 'git status'

Rebase

1. git rebase master
2. git checkout master
3. git merge <branch>
4. git rebase -i HEAD~5 (combining)

Combining the commits, or merging the branch into master

Stash

git stash

will save your work into local disk somewhere to be pulled out later. It is most useful when you would like to save your work to work on something else.

git stash pop

this will pull out the most recent work you saved using git stash. It is important to match the branch name same as the time you stashed. Otherwise, there would be a bunch of conflicts as in most of the time, your code will be very different.

git stash list

It will list what has been stashed so far. Like the example below:

stash@{0}: WIP on <stashed_branch_name> d70f7fa <GIT Comment>
stash@{1}: WIP on <stashed_branch_name> a058e86 <GIT Comment>
stash@{2}: WIP on <stashed_branch_name> cc286cf <GIT Comment>
stash@{3}: WIP on <stashed_branch_name> a058e86 <GIT Comment>
stash@{4}: WIP on <stashed_branch_name> c3610ef <GIT Comment>
stash@{5}: WIP on <stashed_branch_name> cef3e19 <GIT Comment>

Now above first location such as “stash@{3}” indicates the id of stash reference you can pull out. For example, you can run a command such as git stash pop stash@{3} which will pop out the 3rd commits out of the list into your current branch. Make sure you change your branch to what branch it used to stash.

You can also specify a name to a stashed object rather than “stash@{3}” or there are other commands you can do with the stash. Please refer to this website for detail.

Bash Aliases

1. alias gitclean="git clean -df & git checkout -- ."
2. alias gittbc="git diff HEAD --name-only"
3. alias gittbp='git show --pretty="format:" --name-only'
4. alias gitshoworigin='git remote show origin';
5. alias githead='cat .git/HEAD';
6. alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
7. source ~/.git-prompt.sh; export PS1='`if [ $? = 0 ]; then echo "\[\033[01;32m\]✔"; else echo "\[\033[01;31m\]✘"; fi` \w\[\033[35m\] ($(__git_ps1 " %s") )\e[m $ '
Share this post

Me

I am a passionate software developer working in Downtown, Vancouver. I strongly believe in art of algorithms and together with it to write clean and efficient software to build awesome products. If you would like to connect with me, choose one from below options :) You can also send me an email at