2015年9月12日 星期六

Git tips

Syncing a fork:
Sync a fork of a repository to keep it up-to-date with the upstream repository.
  1. git fetch upstream
  2. git checkout master
  3. git merge upstream/master
  4. git push origin master 
  5. git checkout home (home is a branch)
  6. git rebase master
  7. git push origin home
ref: https://help.github.com/articles/syncing-a-fork/


Sync branch:
git fetch --prune

Update a PR:
  1. git reset --hard  (optional)
    Did my changes in code I wanted to do
  2. git add
  3. git commit --amend
  4. git push -f origin 
Cache the password in Git:
git config --global credential.helper cache

ref: https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux

Create a new branch:
git checkout -b office master

Count:
git rev-list --count HEAD --since="March 20 2020"              
git rev-list --count HEAD --since="February 20 2020" --before="March 20 2020"

Create alias:
git config --global alias.co checkout
git config --global alias.st status

Move file to untracked file:

git rm welcome.html --cached

Finish and delete branch:
  1. git branch -D fcrepo-xxxx
  2. git push origin :fcrepo-xxxx
Pull a PR:

  1. Get the PR changes into the test branch:> git fetch origin pull/1485/head:test
  2. Create a test branch to merge the PR into locally. Assuming you're on the master branch:
    > git checkout test
Change the URI (URL) for a remote Git repository

git remote set-url origin NEWURL

Show remote URI 

git config --get remote.origin.url

Delete all commit history
  1. Checkout
    git checkout --orphan latest_branch
  2. Add all the files
    git add -A
  3. Commit the changes
    git commit -am "commit message"
  4. Delete the branch
    git branch -D master
  5. Rename the current branch to master
    git branch -m master
  6. Finally, force update your repository
    git push -f origin master
Show watchers and stargazers URI 
  • https://github.com/[user]/[repo]/watchers
  • https://github.com/[user]/[repo]/stargazers
Search the latest commit
  • git log --graph --all --format=format:'%h - (%ai) %s — %cn %d' --abbrev-commit --date=relative -1

    Output: 6080b81 - (2017-11-14 21:41:39 +0000) run characterization on ingested file — Name  (branch name)
  • 6080b81 is the commit #
Find branch name by commit #
  • git branch --all --contains  6080b81
Sync a branch with master

  1. git checkout test
  2. git rebase master
  3. git push origin +test

Your branch is based on 'origin/master', but the upstream is gone.

  1. git remote add origin giturl
  2. git push -u origin master
.gitignore template: https://github.com/github/gitignore
Remove previous gitignore files:
git clean -fX

Delete a branch
git branch -D dev
git push origin :dev

Rename a branch
git branch -m old_branch new_branch         # Rename branch locally 
git push origin :old_branch                 # Delete the old branch 
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

Squash commit:

$ git rebase -i HEAD~4
or
$ git rebase -i 97c9d7d
1 r 01cc5a08 Removes open div
2 s a2b6eecf Restores old fonts
3 s 603479ff Cleans left out div

$ git push origin branch-name --force

# Edit last commit message, before pushing, by using
$ git commit --amend

Cheatsheet: https://gist.github.com/davfre/8313299