genkio / blog

Stay hungry stay foolish
https://slashbit.github.io/blog/
0 stars 1 forks source link

Git how-to #128

Open genkio opened 7 years ago

genkio commented 7 years ago

Git how-to

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 yourBranch
$ git reset $(git merge-base master yourBranch)
$ git add -A
$ git commit -m "one commit on yourBranch"

How to tag older commit?

$ git tag -a myAwesomeTag 9fceb02 -m "message here"
$ git push --tags origin master
$ git checkout -b myAwesomeNewBranch myAwesomeTag

How to revert all changes?

$ 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

For more information, please ref

How to config a different git user to a project?

# cd into the project folder
$ git config user.name 'user'
$ git config user.email 'user@example.com'

How to git add all except certain file?

$ git add .
$ git reset -- file_name_you_would_like_to_skip

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 to remove commits on github?

$ git reset --hard commit_id
$ git push origin master --force

How to amend a previous commit?

# make some changes, for example, delete everything...
$ rm -rf *
$ git add -A
$ git commit --amend -m "initial commit"
$ git push origin master --force

How to pretty print commit log?

$ git log --pretty=oneline
# or print out the shorter commit id with:
$ git log --graph --oneline

How to fix LF will be replaced by CRLF in git?

$ git config --global core.autocrlf true

How to undo git add?

$ git reset

How to undo git commit?

$ git reset HEAD~

How to work with multiple branches with each branch on its own?

$ git checkout -b new_branch
$ git reset --hard initial_commit_id
$ git push origin new_branch -f
# now you have a clean new branch on its own

How to only commit the files previously committed?

# instead of git add .
$ git commit -am "only commit the files previously committed"

How to push to another repo?

$ git remote add new_origin https://another_repo.github.git
$ git push new_origin HEAD:<new_branch>

How to push commits to a new branch?

# once you done with working on the master branch (3 commits for example)
$ git checkout -b new_branch
$ git push origin new_branch
$ git checkout master
$ git reset --hard HEAD~3
$ git push origin master -f

How to rename a branch?

$ git checkout old_branch
# rename
$ git branch -m new_branch
# delete remote old branch
$ git push origin :old_branch
# create remote new branch
$ git push origin new_branch

How to merge one repo into another?

source

# merge project_a to project_b
$ cd project_b
$ git remote add project_a project_a_path
$ git fetch project_a
$ git merge --allow-unrelated-histories project_a/master # or whichever branch you want to merge
$ git remote remove project_a
$ git push origin project_b

How to merge commits with rebase?

# third_commit (to be merged)
# second_commit (to be merged)
# initial_commit
$ git log --pretty --oneline
$ git rebase -i HEAD~2
# pick b76d157 second_commit
# s a931ac7 third_commit
# wq to quit
$ git log --pretty --oneline
$ git push origin master -f

How to edit the very first commit?

$ git rebase -i --root

How to set vim as the default editor when doing git rebase?

$ git config --global core.editor vim

How to commit on a specific date?

$ git commit -m "dummy change" --date="2.days.ago"
$ git commit -m "dummy change" --date="yesterday"
$ git commit -m "dummy change" --date="2016-04-10"

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

How to revert the last commit

$ git revert last_commit_id
$ git push origin master

How to sync local and remote branch

$ git remote update origin --prune

How to delete a remote branch that you do not have locally

# remotes/origin/react-todo
$ git push origin --delete react-todo

How to create command alias

$ git config --global alias.cm commit

How to ignore certain file / files globally

$ 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"