words of caution: few of the tips are done with "dark forces" (like using the force parameter), try not to do that when you're working with others on the same repo, I learned it the hard way, good luck :-D
git config --global pull.rebase true
alias gf='git push --force-with-lease'
How to rebase develop (master) branch on top of your feature branch
So that the commits histories in your feature branch looks clean
$ git fetch origin
$ git checkout develop
$ git merge --ff origin/develop
$ git checkout feature/my-feature-branch
$ git rebase develop
# if runs into conflicts that can't be resolved automatically, resolve conflicts manually then either `git rebase --continue` or `git rebase --skip`
$ git push -f origin feature/my-feature-branch
How to squash all your commits in a feature branch into one commit.
$ git checkout master -f
$ git clean -df # to remove any new files created
How to revert the last local commit?
$ git reset --soft HEAD^ # use --soft if you want to keep your changes
$ git reset --hard HEAD^ # use --hard if you don't care about keeping the changes you made
How to revert the last public (github) commit?
# surround HEAD^ with "" if you run into problem
$ git reset HEAD^ --hard
$ git push origin master -f
# should not do `-f`, instead, simply `git revert last_commit_id`
How to use Git rebase to edit a previous commit?
# here's the current state
$ git log --graph --oneline
* 1bbb4e0 commit3
* 90767f1 commit2
* b684702 commit1
# the goal is to edit commit2 (not only the commit message, but also the content committed)
# step 1. travel back to commit1
$ git rebase b684702 -i
# step 2. reset head (with the "", the terminal may keep ask you More? on windows)
$ git reset "HEAD^"
# step 3. make your changes
# step 4. commit the changed content with a new commit message
$ git add .
$ git commit -m "new commit!"
# step 5. continue rebase
$ git rebase --continue
# step 6. force push
$ git push origin master --force
# here's the new look, commit2 got updated with a new commit id, and commit3 also got a new commit id
$ git log --graph --oneline
* f56ad2c commit3
* 9b7701e new commit!
* b684702 commit1
How to list all branches including the remote ones?
$ git branch -a
# -a shows all local and remote branches
How to delete a branch?
$ git branch -d the_local_branch
# to delete the remote branch
$ git push origin :the_remote_branch
How to checkout a remote branch?
$ git checkout -b remote_branch origin/remote_branch
# or first create a local branch of the same name as the remote branch
$ git checkout -b remote_branch_name
$ git pull origin remote_branch_name
How move one git repo into another as a subdirectory while keeping commit history
$ cd /home/machete/old-project
$ mkdir old-project
$ mv !(old-project) old-project
$ git commit -a -m "Preparing old project for move"
$ cd /home/machete/new-project
$ git remote add temp /home/machete/old-project
$ git fetch temp
$ git merge temp/master --allow-unrelated-histories
$ git remote rm temp
$ git push origin master
How to rename a local branch
$ git branch -m oldname newname
How to set up a global .gitignore on Mac
$ touch ~/.gitignore_global
$ git config --global core.excludesfile '~/.gitignore_global'
# for windows users
$ git config --global core.excludesfile "%USERPROFILE%\.gitignore_global"
How to stash your changes
# note: git could only stash the changes to the existing files
$ git stash save "saved for now"
# to bring it back
$ git stash list
$ git stash apply stash@{0} # the last part will vary
# another way to do it (and also to remove the stash)
$ git stash pop
# remove a specific stash
$ git stash drop stash@{0}
# diff against stash
$ git stash show -p stash@{0}
How to resolve merge conflict
# if someone get one step ahead of you, pushed their changes first, you'll then most likely getting this error when you try to push your changes:
# "updates were rejected because the tip of your current branch is behind", here's what you need to do to address this:
$ git pull origin branch_name
# git will try to do the merge automatically, it fails "automatic merge failed; fix conflicts and then commit the result."
# then what you will need to do is to resolve the conflict manually, then:
$ git add .
$ git commit -m "resolved conflict"
$ git push origin branch_name
$ git config --global core.excludesfile ~/.gitignore_global
# then put whatever needs to be ignored into .gitignore_global
.gitconfig
[user]
name = user
email = user@example.com
[color]
branch = auto
diff = auto
status = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
[color "status"]
added = yellow
changed = green
untracked = cyan
[push]
default = current
[pull]
rebase = true
default = current
[core]
excludesfile = /Users/user/.gitignore_global
How to create a PR with no diff
# empty commit allows you to do that
$ git commit --allow-empty -m "Initial empty commit"
Git how-to
How to rebase develop (master) branch on top of your feature branch
How to squash all your commits in a feature branch into one commit.
How to tag older commit?
How to revert all changes?
How to revert the last local commit?
How to revert the last public (github) commit?
How to use Git rebase to edit a previous commit?
For more information, please ref
How to config a different git user to a project?
How to git add all except certain file?
How to list all branches including the remote ones?
How to delete a branch?
How to checkout a remote branch?
How to remove commits on github?
How to amend a previous commit?
How to pretty print commit log?
How to fix LF will be replaced by CRLF in git?
How to undo git add?
How to undo git commit?
How to work with multiple branches with each branch on its own?
How to only commit the files previously committed?
How to push to another repo?
How to push commits to a new branch?
How to rename a branch?
How to merge one repo into another?
source
How to merge commits with rebase?
How to edit the very first commit?
How to set vim as the default editor when doing git rebase?
How to commit on a specific date?
How move one git repo into another as a subdirectory while keeping commit history
How to rename a local branch
How to set up a global .gitignore on Mac
How to stash your changes
How to resolve merge conflict
How to revert the last commit
How to sync local and remote branch
How to delete a remote branch that you do not have locally
How to create command alias
How to ignore certain file / files globally
.gitconfig
How to create a PR with no diff