Friday, September 20, 2019

Git 實用操作筆記

 
Forcibly cleaning everything including the directory, to make it clean again:
強行清除所有未 commit 的內容:
git clean -fd
git clean -nd (n is for preview, you can also use i for interactive)

Update everything from server
由 Server 取得更新:
git pull (--all)

Force manually update things from server
以 server 內容取代本機的內容:
git fetch
git reset --hard HEAD

----
放一個檔案去 Staged Area
# stage a file to staged area (a.k.a. add)
git add <filename>
git add -p <filename> #support --patch

反向操作
# unstage a file from staged area
git reset <filename>
git reset -p <filename> #support --patch

----

Reverting all changes to the latest commit, a.k.a. HEAD (discard changes) (for specific file, git checkout file is fine)
強行返回上一個 commit
git reset --hard HEAD

Move HEAD to previous commit
將 HEAD 指回上一個 commit
git reset --hard HEAD~1     #delete everything not committed.
git reset --soft HEAD~1     #keep unstaged things unchanged, combine HEAD~1 things and staged things to staged area
git reset --mixed HEAD~1    #(default) put all (HEAD~1 things, staged things and unstaged things) to unstaged area

Move HEAD to specific commit 
將 HEAD 移到某一個 commit
git reset --hard 0d1d7fc32

#regret about the reset changes
git reflog                    # to list all history
git reset --hard HEAD@{5}     # to reset back to original HEAD (use HEAD 5 as example)

# reverting single file to latest commit
git checkout -- filename

----

Save temporary changes without commit, then display clean workspace.  (a.k.a. "store/hide something secretly somewhere")
暫存未 commit 的內容到 stash 中
git stash
git stash -p #support --patch

Stash only staged files
git add app/controllers/cart_controller.php
git stash --keep-index
git reset

Restore (without popping out)
git stash apply (--index <?>)

Pop out
git stash pop

-----

Check out a remote branch as a local branch
開一個 local 分支,內容是 remote 分支:
git checkout -b LocalName origin/remotebranchname

----

# rewrite history (change previous commits)
git rebase -i HEAD~3     # use last 3 commits as an example

# rewrite history (amend last commit)
git commit --amend

# go back to previous commit (with a new commit)
git revert --no-commit 0766c053..HEAD
git commit

# move HEAD to previous commit then push to server
# (note: it rewrites history, generally not good, git revert might be better)
git reset --hard 0d1d7fc32
git push --force

----

# rename local branch / remote branch
git branch -m <old_name> OR
git branch -m <old_name> <new_name> OR
git push origin :<old-name> <new-name> (delete remote old-name branch and push local new-name branch)

# remove / delete local branch
git branch -d <branch name>

# remove / delete remote branch
git push <remote_name> --delete <branch_name>
git push <remote_name> :<branch_name>

#force update the remote branch list
git remote update origin --prune OR
git pull -p

# force push the local branch to remote (ignoring the remote status)
git push origin --force

----

# show abbreviated commit hash (short hash)
git log -1 --abbrev-commit

# remove all branches named with chriz.chow
git branch -r --list "*chriz.chow*" | sed 's/origin\///' | xargs git push origin --delete

----

Sometimes if you want to checkout a branch (e.g. dpp), but you also have a file called dpp.
When you run "git checkout dpp", Git does not know what operation you want.

To switch to dpp branch, you can run:
git switch dpp

To restore the dpp file, you can run:
git restore dpp

Alternatively, you can also add double-dash to indicate it is a file path:
git checkout dpp --

No comments:

Post a Comment