Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Wednesday, October 6, 2021

個人常用的 Git Commands

 0. 相關初始設定

git init
git config --global user.name "teyenliu"
git config --global user.email "teyen.liu@gmail.com"
git config --list

#git config --global http.sslverify false.
#git config --global log.decorate short
#git config --global color.ui true
#git config --global core.editor "vim"


git remote -v

#首次建立repository
#git remote add origin https://github.com/teyenliu/XXXX.git
#git branch -M main  // 更換master branch name to main
#git push -u origin master

#CRLF
#Linux: git config --global core.autocrlf input
#Windows: git config --global core.autocrlf true

#Editor
#git config --global core.editor vim
#Setup commit message template
#git config --global commit.template ~/git-template

#如果是 fork a repository, 則需要設定upstream repo
git remote set-url origin <my fork's git repo>
git remote add upstream <upstream's git repo>

# 查看到我們名下的遠端項目
git remote -v

#獲取upstream的最新版本
git fetch upstream

#將upstream merge到我們當前分支
git merge upstream/master

1. check out 另一個branch
git checkout <to this branch>
git checkout -b <create_new_branch> <based on this branch>

2. 建議使用這個方式避免無謂的merges
git pull --rebase <remote name> <branch name>

3. 當要switch branch,但是有file未 commit,可以使用下列方式暫存起來
git stash save 'stash1'

#查看在 stash 中的缓存
git stash list

#恢复暂存
git stash pop

4.  git checkout 時發生 Please move or remove them before you can switch branches.
git clean -d -fx

5. 拉下來最新的檔案
git pull --rebase upstream master
git pull --rebase upstream ConfigBase

6. 推code 到 remote repo.
git push origin master

7. 補充發送
git commit --amend
git push -f origin ConfigBase
git push -f origin master

8. 建立/刪除分支
#建立
git checkout -b <branch> 從目前分支再去建立(會新增)本端分支
git push -u origin <branch> PUSH並建立遠端分支
#刪除
git branch -d <branch>

### Branch from a previous commit using Git ###
# Create the branch using a commit hash:
git branch branch_name <commit-hash>

#Or by using a symbolic reference:
git branch branch_name HEAD~3

#To checkout the branch while creating it, use:
git checkout -b branch_name <commit-hash or HEAD~3>

9. 移動某個commit點
#回到 上一次的 commit
git reset --hard HEAD

#回到 上一次的前一次 commit
git reset --hard HEAD^

#裡面最近做的所有 HEAD 的改動
git reflog
git reset --hard 904e1ba #最近的commit

10. 如果想要打tag( 以某個commit為基礎 )
# 產生一個新的branch以某個commit
git checkout -b <tagname> <commit id>
git push origin <tagname>

# 要先把 branch push上去的原因是, 之後建立的tag如果跟brnach name相同 (例如: v0.4 ), 則 branch會發生如下訊息:
error: src refspec v0.4 matches more than one.
error: failed to push some refs
如果不小心先push tags, 則必須把tag移除後, 才能push branch.


# 查看目前有的tag
git tag -l

# 打tag
git tag -a <tagname> -m "My App description"

# 刪除tag
git tag -d <tagname>

# push all tags 到 remote端
git push origin --tags

11. 如果想要檢查 submodule 是否有更新可使用下列指令:
git submodule foreach --recursive git pull origin master

12. 強制推送到remote
git push -f

13. 拉submodule的程式碼
git submodule update --recursive --remote

14. 查看完整的Commit 資料
git log --pretty=oneline

15. 退回到特定 Commit
# where [revision] is the commit hash (for example: 12345678901234567890123456789012345678ab)
git checkout [revision] .

#To rollback to a specific commit:
git reset --hard commit_sha

#To rollback 10 commits back:
git reset --hard HEAD~10

Trouble Shooting

如果遇到Tag Name與Branch Name相同時, 會有其中一種無法上傳至遠端的錯誤:
src refspec XXX 匹配多個, 例如:
error: src refspec v0.3 matches more than one

解決方式:

1. 先刪除分支然後上傳Tag:
git branch -D testtag

或是

2. 先刪除tag然後上傳分支:
git tag -d testtag





Wednesday, January 20, 2016

[Git] Meld - The difftool

Install meld

sudo apt-get update
sudo apt-get install meld

Setup git default diff tool as meld

git config --global diff.tool meld
git config --global --add difftool.prompt false

Examples

cd <git repo dir>
git difftool           // display file diff one by one
git difftool file_path // display one file diff
git difftool HEAD HEAD~5 filename

Wednesday, May 20, 2015

[GIT] The git commands that I often use

I just list the git commands that I often use and keep in this document for reference.


// pull the newest commit from origin
git pull origin "your branch"

// check out to a branch
git checkout "your branch name"

// apply the diff file
git apply "your diff file"

// Check the diff
git diff [your file]

// code commit
git commit -a -m "This is bug."

// push commit to remote site,
git push "your remote site" "your branch"

// repository name
git remote rm origin
git remote add origin git@...git
git remote set-url origin git@...git

// add new remote repository name
git remote add "remote repository name" ssh://.......

//Force to go back the current newest commit
git reset --hard HEAD
HEAD^ // the previous commit
HEAD~2 // the previous two commit
git reset --hard eb2b844 // the previous a commit with your hash code
--soft // the change will be added to stage
--hard // all the change will be deleted

//cancel the modification of some files.
git checkout -- <file>

// modify the last commit
git commit --amend

// reset the change of some files
git reset HEAD <file>

// reset the merge
git reset

// clean up the changes
git cleanup -f

// checkout a branch
git checkout "branch"
// checkout a branch to new branch with track
git checkout -t -b bcm origin/bcm

// look up the commit hash
git log  
                     
// cherry pick the commit
git cherry-pick "hash code"

// push to remote repository
git push remote repository name "branch"



// use GUI to see the diff, commit and branch tree
gitk --all&

// merge the branch to this branch
git merge "branch"

// There are several mergetools, such as, kdiff3/meld/p4merge
// I suggest to use p4merge/meld
// the difftool "p4merge" to see the diff of two commit with the cc files
git difftool -t p4merge aa202d3..be15911907d9 -- *.cc
git mergetool -t p4merge myfile.cc

// keep the temp modification
git stash
// check the stash list
git stash list
// apply the stash
git stash apply --index
// to apply the stash and then immediately drop it from your stack.
git stash pop
// create a branch from a stash
git stash branch "your stash branch name"

// Undo a commit and redo
git commit ...              (1)
git reset --soft HEAD~1     (2)
<< edit files as necessary >> (3)
git add ....                (4)
git commit -c ORIG_HEAD     (5)


// Remove all .pyc or .xxx files
find . -name "*.pyc"; -exec git rm -f {} \;

// Remove a file in remote repository without deleting local file
git rm --cached your_file_here

// Set ignore file:
git config --global core.excludesfile ~/.gitignore_global

// Ignore SSL verification
env GIT_SSL_NO_VERIFY=true git command
or set to config:
git config http.sslVerify "false"

// Get sources after commit id
git checkout 336e1731fd8591c712d029681392e5982b2c7e6b src/abc/*

// *** Avoiding annoying merge commits:
git pull --rebase origin master
git pull --rebase origin

// Avoiding to merge or commit your customised configs:
git update-index --assume-unchanged [filepath]

// Putting back and tracking the files above:
git update-index --no-assume-unchanged [filepath]