Skip to content

Instantly share code, notes, and snippets.

@yanghu
Last active July 10, 2016 15:34
Show Gist options
  • Save yanghu/6274008 to your computer and use it in GitHub Desktop.
Save yanghu/6274008 to your computer and use it in GitHub Desktop.
Useful Git commands

##Useful Git Commmands and Tips

Ignore files locally###

add the files you want to ignore to .git/info/exclude, this file will not be shared with other remotes, so only the local repo will ignore those files.

###Show information###

  • git log/show command
git log --graph --oneline       #show history
git log --oneline dev   #show the history of specific branch
git log --pretty=format:'%h %s' --graph
                        #formatted log
git log -p -2          #show last 2 commits, generate patch report
                        #patch shows what has been changed.
git log -U1 --word-diff #show word-level changes
# "-U1" means 1 line contexts in report, making it more compact.

git show branchname     #show the most recent commit on the branch
git show HEAD^          #the parent of HEAD

git reflog              #show the reflog (where HEAD have been)
git show HEAD@{5}       #show the HEAD information listed in reflog.
git show master@{yesterday}

git status

###Basic operations###

  • Clone
    git clone only clones the master branch by default. you'll need to create local branch to track remote branches.
git clone [repository_link]
git checkout -b dev origin/dev
  • Staging/commit
git add filename       #add a file to stage it
git commit -am "comments"      #apply all changes, commit them.
git commit --amend     #apply something more to the latest commit
git reset HEAD filename#unstange a staged file
git checkout -- filename #unmodify(restore) the (uncommited)file from most recent commit
  • git reset
    A really good article can be found here
    A good way to go is to reset soft (therefore the stage is still the newest), and then check out single files/paths using git checkout -- path/to/file, if only want to revert some files, but not the whole thing. and then add them to stage, commit again using git commit --amend
git reset --soft HEAD~2        #more HEAD back two commits. don't change index/work directory
git reset --mixed 8udc765      #reset HEAD to the SHA-1 specified commit, and update index(stage)
git reset --hard       #reset the HEAD, index, and working directory to the commit (default is last one)
  • Diff
git diff                #show diff of unstaged changes
git diff --cached      #show diff of staged changes
git diff HEAD          #show diff of both staged and unstaged changes
git diff --stat        #show only status of changes(how many lines added,etc.)
git diff [--stat] master..HEAD  #show diff between two branches
git log(shortlog) master..HEAD   #show list of commits that HEAD has but master doesn't
git log origin/master..HEAD    #can also compare with remotes
git diff SHA-1..SHA-2  #diff between two commits
git diff HEAD^^..HEAD ./main.c #diff a single file between now and two commits earlier
git diff <revision_1>:./<file_1> <revision_2>:./<file_2>
                       #compare two files in different commits

git log -U1 --word-diff master..HEAD  #show word level difference between two branches
  • Stash
git stash                      #save working directory and index state
git stash list                 #list the stashes you've stored
git stash apply                #apply the most recent stash
git stash pop                  #apply the stash and delete it
git stash apply --index        #apply the stash and stage the staged files
git stash drop stash@{0}       #delete the most recent stash

git stash show -p stash@{0} | git apply -R     #"un-apply" a stash
git stash branch testbranch    #start a new branch from stash

###Work with remote

  • Fetch/Pull/merge
git fetch origin       #fetch the remote, get the data, but not merge with local
git pull (origin) (maseter)             #fetch the remote data and merge with local files
git pull --rebase (origin) (maseter)  #fetch the remote, and rebase local work on top of it.
  • Push
git push --mirror   
//force remote to be exactly the same as local.Delete/create branches,tags,etc.

git push -u #setup tracking so in the future "git push" will push this branch automatically
  • Check info/manage
git remote add new_remote *[_remotelink_]*   #add the remote
git remote rm origin          #delete the remote 
git remote rename origin og    #rename the remote
git remote show origin   #show status of remote and local git
git remote -v          #show remotes with urls

###Branches

  • Creates/delete/manage:
git checkout -b mybranch       #create a new branch based on current one and chcekout
git branch -d mybranch         #delete the branch
git branch -a      #show all branches, both local and remote
git branch --merged    #check all branches that have merged with current branch
git branch -v          #show most recent commit of each branch
git push (remote-name)  (local-branch):(remote-branch) 
git push (remote-name)  :(remote branch)       #delete the remote branch(push empty)
  • Merge two branches (assuming you are at "master", and wants to merge the "dev")
git merge dev  #normal merge
git merge -s ours dev  #merge with strategy "ours". will override branch "dev" with 
                       #current branch

git merge-base A B     #find out the common ancestor commit of two branches.

To merge a branch into master, and comined the history in that branch to one single commit. (like, add a hotfix to master) Following snippet can be used

git checkout master
git merge --squash dev #'merge' dev to master, but do not commit yet.
git commit -v          #write a single detailed commit message for the merge.  
  • Rebase
    Rebase current work to master, and then merge them. Finally, delete the small branch
git rebase master      #apply previous commits of current branch onto "master"
git checkout master
git merge dev
git branch -d dev

Using interactive rebase to rewrite history.

git rebase -i HEAD~4   #interactively rebase the previous 4 commits of current branch
git rebase --continue  #when a rebase is paused due to some reason

useful options for merging

###Tagging

git tag                #list tags
git tag -l 'v1.4.*'    #list particular patterned tags

git tag -a v1.2 -m 'My Version 1.2'    #add a new tag with comment
git show v1.2          #show the tagged commit
git tag -a v1.3 -m 'tag later' 9fceb02 #add tag to the SHA-1 commit
git push origin --tags #push tags to remote
@sanmaowang
Copy link

cool~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment