Skip to content

Instantly share code, notes, and snippets.

@pastleo
Last active August 27, 2016 15:11
Show Gist options
  • Save pastleo/e357795b045d4e160655 to your computer and use it in GitHub Desktop.
Save pastleo/e357795b045d4e160655 to your computer and use it in GitHub Desktop.
My Git Action Sets & Tutorials

My Git Action Sets & Tutorial

Init the git repo

  • git clone <url> [path]
  • git init

Trouble-Free Actions

  • git status
  • git log
    • git log --name-status: show filename with modification status
    • git log --stat: show filename with more info
  • git diff <file>
  • git diff <commit_or_branch> <commit_or_branch>
  • git diff -–name-only <commit_or_branch> <commit_or_branch>

make progress

  1. git status first, see if you are in trouble
  2. git add . to stage all modifications
  3. git commit -m "<message>" to wrap changes and push the HEAD to new progress
  4. git push to send changes to the cloud

get progress from remote

  • git pull Grab progress from remote, auto merge when theres changes on local
  • git pull --rebase Same as git pull but use rebase when theres changes on local

4 Ways to Avoid Merge Commits in Git

Discard change on a file

  • git checkout <path>

Change location

  • git checkout <branch_or_commit>

Create a new branch

  • git checkout -b <new_branch_name>
  • git branch -u <remote>/<new_branch_name> [new_branch_name] to set (bind) the remote branch for push and pull

Config of the repo

  • git remote -v: see the remote registry of the repo
  • git remote add|rm <name> <url>
  • git config ...

Showing the branch graph beautifully

  • git log --oneline --graph --decorate --all

Apply a diff between two commit or branch to another place (copy diff):

  • git diff <old_commit_or_branch> <new_commit_or_branch> > my.patch
  • git apply my.patch

Change the last commit info (i.e. message)

  • git commit --amend

remove file and commit from history (assume tree: R–A-removal_target-B- )

  • git checkout <B> to detach head and move to A
  • git reset --soft <A> to move HEAD to the old commit, but leave the index and working tree as for the commit kept
  • git commit -C <B> to Redo the commit kept re-using the commit message, but now on top of the old commit
  • git rebase --onto HEAD <B> <branch_name> to re-apply everything from the olds onwards onto this new place
  • git push --force to overwrite remote repo

如何移除-commit-歷史紀錄
Git-Tools-Rewriting-History

About git reset

First the following things need to be defined:

  • HEAD: the history, the current branch is pointing at
  • INDEX / Stage: the cache, the added changes
  • Working directory: the code and files in your directory

Then

  • git reset --soft [target]: set HEAD only
  • git reset [--mixed] [target]: set HEAD and INDEX
  • git reset --hard [target]: set HEAD, INDEX and Working directory
  • git checkout <history_node> set HEAD, INDEX and Working directory, but leaving the original branch
    • So it is a detached state, you can go back by git checkout <original_branch>
    • Or create a branch by git checkout -b <branch_name>, I found git checkout <history_node> -b <branch_name> will do the two steps together

difference in git reset

Git submodule

  • When cloning a repo from remote, the submodule is not initualized and the submodule directory is empty. To solve this
    • add --recursive option when cloning to clone submodules in the target repo, for example: git clone --recursive <url>
    • use git submodule init and git submodule update --recursive if you already cloned and found that the submodule is empty
  • git submodule add <url> <path> to add a submodule
  • git submodule foreach --recursive git pull origin master to pull the latest code for each submodule
  • to delete the submodule, it is required to do it manually. Edit .gitmodules and .git/config to remove related information and remove the files in working directory

Git Submodule 用法筆記

Git Tutorials I like

@YushengLi
Copy link

感恩西瓜,讚嘆西瓜!

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