Skip to content

Instantly share code, notes, and snippets.

@zhangzhhz
Last active February 17, 2021 02:58
Show Gist options
  • Save zhangzhhz/0eec06e4a123426bb4d4e7ece2bbdf2f to your computer and use it in GitHub Desktop.
Save zhangzhhz/0eec06e4a123426bb4d4e7ece2bbdf2f to your computer and use it in GitHub Desktop.

Notes from Practical Git for Everyday Professional Use on egghed.io.

git init

Initialize the current local directoy as a git repository locally.

git init --bare will make the local directory a bare repository which will act as a remote respository.

git clone <link>

Clone a remote repository locally. New directory will be created.

git add <file or . or -A>

  • file Add changes in file to index (staging area).
  • . Add changes in current directory to index (staging area).
  • -A Add all changes locally to index (staging area).

git diff

  • git diff [files]

Difference between working directory and index (staging area). This shows difference that has not been staged (i.e., git add is needed).

  • git diff --cached [files]

Difference between index (staging area) and last commit/HEAD.

  • git diff HEAD [files]

Difference between working directory and last commit/HEAD. This shows all changes since last commit, whether they are staged or not.

  • git diff origin/master [files]

Differences between local and remote.

git commit -m "commit message" <file(s)>

  • git commit without providing file names

Commits changes in staging to local repository. If a file has changes that are not staged yet, unstaged changes will not be committed.

  • git commit <filename>

Will commit the file with all changes to local repository. If a file has changes that are not staged yet, unstaged changes will be committed too.

git push

Push changes to remote.

git pull

Pulls changes from remote. If conflicts happen, manually update the file(s) and commit.

git log

  • git log --oneline --graph
  • git log --after="yesterday" --before="10 minutes ago"
  • git log -i <...>
  • git log --author="name1\|name2"
  • git log --grep="name"
  • git log -p -S"<search-string-in-codes>"
  • git log -p -Gcode1\|code2
  • git log --no-merges
  • git log -3
  • git log master..hot-fix
  • git log file1 file2

git checkout branch-name

Checks out the branch and work in the branch.

git branch

git branch -v shows branches with latest commit.

git branch <name> creates a new branch.

git checkout -b <name> creates a new branch and checks it out.

git branch -d <name> removed the branch.

git merge

git merge <branch> meargs changes in the branch into current branch and create a merge commit. If conflicts happen, manually update the file(s) and commit.

git stash

git stash stashes changes in the working directory (which cleans up the working directory) so that we can work on a hot fix.

git stash apply brings back stashed changes.

git blame

Lists all changes to the file in chronological order. Commit id, author, commit time and changed lines will be displayed.

git tag

Sematic versioning: major.minor.patch

git tag to list all tags

git tag <tag-name> to add a tag

git tag -a <tag-name> -m "tag message" to add a tag message

git push origin <tag-name> to push a tag to a remote repository

git rebase

Exmaple: git rebase -i origin/master

Clean up commits (roll up a few commits to one) locally before pushing to remote. This destroys commit histories.

git bisect

Find out where a bug is introduced.

git bisect bad marks the current commit as bad.

git bisect good <commit-id> does a binary search between the good and the bad commits and automatically checks out a commit between the two. Test now to see if the new current commit is bad or good.

Do the above iteratively until you find the codes are working.

git bisect reset then resets the git status.

git hooks

Add hooks

git config

  • git config --global alias.graph 'log --graph --oneline'
  • git config --list
  • git config --list --system
  • git config --list --global
  • git config --list --local
  • git config --edit --system
  • git config --edit --global
  • git config --edit --local

.gitignore

Remove git tracking

git rm --cached <file> to remove a file that is added to git before being added to .gitignore from git tracking. --cached means the file will be left in working tree.

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