Yekun's Note

Machine learning notes and writeup.

Fork me on GitHub

Tricks of Git

Some useful tricks of git.

upload successful

Git tricks

Error

  • Too large git file pull error!
    1
    Git fatal: pack has bad object at offset XXX: inflate returned -5
    Solution:
    1
    2
    3
    4
    git clone --depth=1 "<url>"
    $ git remote set-branches origin "<remote_branch_name>"
    $ git fetch --depth 1 origin "<remote_branch_name>"
    $ git checkout "<remote_branch_name>"

ignore file after pushing to remote respository

1
git rm -r --cached .

Git large file removal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# find large files
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"

# delete the log in the cache
git filter-branch -f --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch <file-name>' --tag-name-filter cat -- --all

# reflog
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git fsck --full --unreachable
git repack -A -d
git gc --aggressive --prune=now

# force push
git push --force

Overwrite

1
error: Untracked working tree file 'xxx/xxx/xxx' would be overwritten by merge.
  • Force git pull to overwrite with remote version
    1. regardless of the local changes.
      1
      2
      3
      4
      5
      6
      7
      git fetch --all
      # or (recommend. No need to fetch all branches)
      git fetch origin master

      git reset --hard origin/master
      # or
      git reset --hard origin/"<branch_name>"
    2. maintain current local commits
      1
      2
      3
      4
      5
      6
      7
      git checkout master
      git branch "<new-branch—to-save-current-commits>"

      git fetch --all
      git reset --hard origin/master
      # or
      git reset --hard origin/"<branch_name>"
  • Uncommitted changes
    1
    2
    3
    4
    git stash # stash uncommitted changes
    # ...
    # reapply uncommitted changes
    git stash pop
  • Git reset
    1
    2
    git reset --hard HEAD
    git pull
  • Git clean
    1
    2
    git clean
    git pull

Branch

Create new branch and pull to remote

1
2
git checkout -b "<new_branch>"
git push origin "<new_branch>""<remote_new_branch>"

Delete remote branch

  1. Push an empty branch to remote

    1
    git push origin :"<remote_branch>"
  2. delete remote

    1
    git push origin --delete "<remote_branch>"

Git pull remote branch

1
git pull origin "<remote_branch>":"<local_branch>"

Check

  • git diff: check the local changes
  • git log: check all committed history
  • git blame <my_file>: who changes what in
  • git reflog: shows the changes of log in local repository, used for looking for losing jobs.

Reset

git reset vs git checkout vs git revert

  • git reset and git checkout can apply on the commit stage or the single file, whilst git revert is only suitable for commit stage.
1
2
3
4
5
6
7
8
# Discard commits in a private branch or throw away uncommitted changes
git reset --hard HEAD

# discard changes in the working directory
git checkout <my commit>

# Undo commits in a public branch
git revert <my commit>
  • git clean -n : delete local untracked files.

Clean all logs

1
2
3
4
5
6
7
8
9
10
11
# create a tmp branch
git checkout --orphan tmp
# add & commit
git add -A
git commit -m 'Clean log'
# delete master branch
git branch -D master
# rename branch tmp to master
git branch -m master
# push
git push -f origin master

Git cheat list

Create repository

1
2
$ git clone <url> # clone remote repository
$ git init # init local repository

Change and commit

1
2
3
4
5
6
7
8
$ git status # check the status
$ git diff # check changes
$ git add . # track all changed files
$ git add <file> # track the specified file
$ git mv <old> <new> # rename file
$ git rm <file> # delete file
$ git commit -m "commit msg" # commit all changed files
$ git commit --amend # amend the last commit

Check commit history

1
2
3
$ git log # show commit logs
$ git log -p <file> # show commit logs of <file>
$ git blame <file> # show what revision and author last modified each line of a file

Undo

1
2
3
$ git reset --hard HEAD # undo all uncommitted contents in the work directory
$ git checkout HEAD <file> # undo the changed contents of an uncommitted file
$ git revert <commit> # revert specified commit

Branch and Tag

1
2
3
4
5
6
7
$ git branch 	# show all local branches
$ git checkout <branch/tag> # git checkout to the branch/tag
$ git branch <new-branch> # create a new branch
$ git branch -d <branch> # delete the local branch
$ git tag # show all local tags
$ git tag <tag-name> # create tag based on the latest commit
$ git tag -d <tag-name> # delete tag

Merge and rebase

1
2
$ git merge <branch> 	# merge <branch> to the current branch
$ git rebase <branch> # rebase <branch> to the current branch

Remote

1
2
3
4
5
6
7
8
9
$ git remote -v		# check remote repository information
$ git remote show <remote> # show specified remote repository information
$ git remote add <remote> <url> # add remote repository

$ get fetch <remote> # fetch code from the remote repository
$ git pull <remote> <branch> # pull and quick merge
$ git push <remote> <branch> # push and quick merge
$ git push <remote> :<branch/tag-name> # delete remote branch or tag
$ git push --tags # push all tags

Change remote repository

1
2
3
4
5
6
7
8
9
10
git remote set-url origin <new-url>

# or
git remote rm origin
git remote add origin <new-url>

# or
vi .git/config
# modify the url of [remote "origin"]