IOriens / ioriens.github.io

https://Junjie.xyz
12 stars 2 forks source link

Git Tips #1

Open IOriens opened 6 years ago

IOriens commented 6 years ago

Git message matters

The message you wrote will help collaborators understand what you've done with your last commit, using prefix will help users distinguish which type of action your commit has performed, for example:

image

Here are two types of git message standards:

General

image

More specific

Use alias

If you are using oh-my-zsh, you can use git plugin to add alias for your git commands. You can also create your own alias like this:

alias gs='git status'
alias als='alias | grep'

Then, use gs to check local modifications rather than git status. One can't remember all the aliases, now you can use als to search from them. For example:

image

Commit with -v option

Using this option, git will show all your modifications before you commit, recheck carefully before you commit !!!

git commit -v

The first time you use this commamd may throw this error: There was a problem with the editor 'vi'. You can solve it by setting default editor to vim:

git config --global core.editor /usr/bin/vim

Use rebase if you can

Rebase

Apply feat changes on master, cause one linear commit log.

# Way 1
git checkout feat
git rebase master

# Way 2
git rebase master feat

# Final
git checkout master
git merge feat

Merge

Diff feat and master, apply patch on feat.

# Way 1
git checkout feat
git merge master

# Way 2
git merge master feat

# Final
git checkout master
git merge feat

Reset your modifications

Here are some reset commands, only --hard option will destroy your data, be careful!

# reset unstaged(not added but modified) file (!!!)
git checkout -- <file>

# reset staged(added) file
git reset HEAD <file>

# drop commits without deleting modifed content
git reset --soft HEAD~1

# drop latest commit (!!!)
git reset --hard HEAD^

Other tips

Show all the branches

git branch -r

Comparison

# Compare one commit with the previous commit
git diff 96755a9~ 96755a9

# Show diff in a specific commit
git show 96755a9 

How to see the changes in a commit?

Name your branch wisely

git branch naming best practices [closed]

Set Proxy

# set http proxy
git config --global http.proxy http://localhost:7777
git config --global https.proxy http://localhost:7777

# check settings
git config --global --get http.proxy

# unset proxy
git config --global --unset http.proxy

GIt Ignore

Git ignore's precedence. You can use it to ignore files locally.

Forget tracked file in gitignore

https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore

Commit Ignored files

git add --force my/ignore/file.foo

Commit with no verify

--no-verify: This option bypasses the pre-commit and commit-msg hooks.

Force Push

## Danger, this will delete your previous commit(s) and push your current one.
git push --force

Lifecycle of your edits

image