Skip to content

Git tricks

Jili Dong edited this page May 4, 2020 · 17 revisions
  1. Just pull one file from remote repository:

    $ git fetch community (remote repo)

    $ git checkout community/develop (repo/branch) filename


  1. git diff only show file names

    $ git diff --name-only


  1. diff one file

    $ git diff mybranch master -- myfile.cs

    or

    $ git diff mybranch..master -- myfile.cs


  1. check which files added in a commit

    $ git diff-tree --no-commit-id --name-only -r 0ea1c630


  1. merge upstream repository

    $ git fetch upstream

    $ git checkout master

    $ git merge upstream/master


  1. Add upstream repository

    $ git remote -v

    $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git


  1. Get branch from another fork

$ git remote add theirusername git@github.com:theirusername/reponame.git

$ git fetch theirusername

$ git checkout -b mynamefortheirbranch theirusername/theirbranch


  1. remove files

$ git rm files (sometimes need to add --cache to only remove the file in index---keep file locally?)

remove directories

$ git rm -r dir


  1. compare fork with remote/upstream repo

first fetch upstream repo

git diff HEAD..mainRepo/master


  1. submodule

when .gitmodule is changed, try in the sub-directory "git submodule sync", then in the same dir "git submodule update --init --recursive"

check submodule commit: "git submodule status --recursive" or in the sub-directory "git ls-tree HEAD". The first one show current commit, later show target commit

.gitmodule branch just for reference. submodule will check out "git add " at the commit time


  1. find commit from which branch

git branch --contains


  1. add/remove submodules

add: git submodule add git@mygithost:billboard lib/billboard

remove:

1.remove section in .gitmodules

  1. git rm --cached lib/billboard

  1. delete branch

local branch: git -d

remote branch: git push --delete


Clone this wiki locally