Skip to content

Latest commit

 

History

History
365 lines (249 loc) · 6.95 KB

File metadata and controls

365 lines (249 loc) · 6.95 KB

git

Shortcuts

gst            status
ga             add
gcmsg          commit -m
gd             diff
gm             merge
gsta           stash
glo            log --oneline --decorate
gb  / gba      branch / branch -a
gco / gcb      checkout / checkout -b
gl  / ggpull   pull / pull origin $(git_current_branch)
gp  / ggpush   push / push origin $(git_current_branch)
gf  / gfo      fetch / fetch origin
gr  / gra      remote / remote add
:Git           run arbitrary commands
:Gwrite        `git add`, when called from a work tree file,
               `git checkout`, when called from the index or a blob in history.
:Gread         `git checkout` -- filename, that operates on buffer rather than filename.
               can use `u` to undo it
:Gstatus       bring up the output of git status with
                 - : add/reset a file's changes
                 p : add/reset --patch
:Gcommit       commit files
:Gmove         `git mv`
:Gremove       `git rm`
:Glog          loads all previous revisions of a file into the quickfix list
:Gbrowse       open the current file on GitHub, with optional line range

configs

  • make vim default editor

    $ git config --global core.editor "vim"
  • always rebase on git pull

    $ git config --global pull.rebase true

commands

git commit

  • add files and create message

    $ git commit -a -m <message>
  • fix commit message of most recent commit

    $ git commit --amend

git branch

  • show all branches

    $ git branch -a
  • delete local branch

    $ git branch -d <branch>
  • delete remote branch

    $ git push origin --delete <branch>
  • view remote branches

    $ git branch -r
  • rename the current branch to <branch>

    $ git branch -m <branch>

git log

  • shows the full diff of each commit

    $ git log -p
  • multiple lines per version for file

    $ git log --oneline -- <filename>
  • search for commits with a commit message that matches

    $ git log --grep="<pattern>"
  • --graph draws text based graph; —-decorate adds the names of branches

    $ git log --graph --decorate --oneline

git show

  • shows changes in files

    $ git show <commit>
  • --format shows additional metadata

    $ git show --format=raw <commit>

git checkout

Checking out a file affects the current state of your project.

  • check out a previous version of file

    $ git checkout <commit> <file>
  • if you decide you don’t want to keep the old version, you can check out the most recent version

    $ git checkout HEAD <file>

git diff

  • list differences between current and historical version

    $ git diff <hash> -- <file>
  • compare the working directory with local repository

    $ git diff HEAD <file>
  • compare the working directory with index

    $ git diff <file>
  • compare the index with local repository

    $ git diff --cached <file>

git fetch

  • fetched content is represented as a remote branch, no effect on local work; safe way to review commits before integrating them with your local repository.

    $ git fetch <remote>
    $ git fetch <remote> <branch>

git merge

  • undo merge before completed, e.g. to undo merge conflict

    $ git merge --abort
  • undo merge after you've completed the merge

    $ git reset --hard <commit>

git rebase

  • use rebase (instead of merge) to create a linear history

    # before                 # after
    
          C---D feature                    C'--D' feature
         /                                /
    A---B---E---F master     A---B---E---F master
    
    $ git rebase master topic
  • interactively remove, split, and alter existing commits

    $ git rebase -i <base>

git pull

  • git fetch + git merge

    $ git pull <remote>
  • prevent unnecessary merge commits

    $ git pull --rebase <remote>

git revert

  • generate a new commit that undoes all of the changes introduced in <commit>, then apply it to the current branch

    $ git revert <commit>

git reset

Undo local changes in the staging area (can only work backwards from the current commit). More dangerous way to undo changes than git revert -- it's permanent.

  • unstages all files without overwriting any changes

    $ git reset
  • unstages a file without overwriting any changes

    $ git reset <file>
  • move current branch tip backward to , reset the staging area to match, but leave the working directory alone

    $ git reset <commit>

git cherry-pick

  • bring commit to current branch

    $ git cherry-pick <commit>
    
    $ git cherry-pick C D
    # before                 # after
    
          C---D feature            C---D feature
         /                        /
    A---B---E---F master     A---B---E---F---C'---D' master

git stash

  • store uncommited changes

    $ git stash
    
    $ git stash list
      stash@{0}: WIP on master: 049d078 added the index file
      stash@{1}: WIP on master: c264051 Revert "added file_size"
      stash@{2}: WIP on master: 21d80a5 added number to log
    
    $ git show stash@{0}
  • reapply the one you just stashed

    $ git stash apply
  • apply one of the older stashes

    $ git stash apply stash@{1}
  • remove stash from stack

    $ git stash drop stash@{0}

git reflog

  • after rewriting history, the reflog contains information about the old state of branches and allows you to go back to that state if necessary

    $ git reflog

git remote

  • add remote repo

    $ git remote school me@berkeley.edu:~/my_repo
  • show remote repositories

    $ git remote -v
    $ git remote show origin

Preserve git history while copying directory from repo A to repo B

  • Get files ready for merge

    $ git clone <git@github.com:username/repo-A.git> && cd repo-A
    $ git remote rm origin
    $ git filter-branch --subdirectory-filter <repo-A/directory_1> -- --all
    $ mkdir <directory_1>
    $ mv * <directory_1>
    $ git add .
    $ git commit
  • Merge files into new repository

    $ git clone <git@github.com:username/repo-B.git>
    $ cd <repo-B>
    $ git remote add repo-A-branch </directory_1>
    $ git pull repo-A-branch master --allow-unrelated-histories
    $ git remote rm repo-A-branch