contributed by <asahsieh>
SCM
tools: stands for Source Code Management$git reset HEAD^ --hard
$git $command -help
$git remote show <origin>
: list detail, include URL of the repo$git init
locallyTo remember your credentials
I chose a method which has less limitation: Git's built-in credential cache
The credential cache
space
git add -p
一個檔案有很多修改時,為了讓每次commit都是獨立的功能;patch -p
可以讓user各別標記這次要commit的部份。
git commit [-p]
利用
VIM
會利用“文字變色”來提醒
git log
filter by authorgit log --raw
show source with diff
git show --source
--no-ff
(no fast-forwarding) to keep commits from merged branchIt’s best to have a clean working state when you switch branches. There are ways to get around this (namely, stashing and commit amending) that we’ll cover later on, in Stashing and Cleaning.
A hotfix to make
hotfix
branch on which to work until it’s completed:
Basic Merging
git merge --squash bugfix
git merge --squash
does it all on the command line in one shot and you just hope it works. git rebase -i
brings up an editor and lets you fine-tune the rebase. It's slower, but you can see what you're doing.
Rebase <new_base>
Rebase --onto <new base> <ref_commit> <branch>
$ Rebase --continue
until end$ Rebase --abort
if out of controlgit rebase -i <the-commit-to-be-based-on>
-i
: interactiveEdit rebase todo file and save:
operations you can make:
tbm
on front of message to show the commit is just fixing a bug, it's to-be-merged.When do 3-ways merging
incoming
, and other revision is HEAD
When rebasing MyBranch onto master , "incoming" is the branch you have checked out, which is MyBranch , and "current" is master.
https://stackoverflow.com/questions/71332146/incoming-and-current-in-a-rebase
Default remote name is origin
ref.: https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
branch -a
What does '–set-upstream' do?
–> sets the default remote branch for the current local branch.
$git fetch origin
$git checkout master
$git rebase remote/master
git fetch origin pull/ID/head:BRANCH_NAME
Make commits on local repository and pull the commits per day.
若 push 失敗的話,Git 會建議用 git pull
與 remote branch 做 merge,此時會將多個 commits 合併成一個 commit;若此 merge 有誤,要 reset 回 merge 前的 commit 的話,當時被 merged 進來的 commits 都會不見。
故建議做完 $git fetch
更新 remote stuff 到 local branch 後,將新的 commit rebase
到最新的 commit 上,再做 push
set-url
git remote set-url origin git@github.com:asahsieh/lab0-c
ref.: Caching your GitHub credentials in Git
因不想再安裝一套CLI, GitHub CLI;故改用Git Credential Manager
Credential stores: 選擇跨平台且較其他方法來得不受限的方式,Git's built-in credential cache
If you used git init to make a fresh repo, you'll have no remote repo to push changes to. A common pattern when initializing a new repo is to go to a hosted Git service like Bitbucket and create a repo there. The service will provide a Git URL that you can then add to your local Git repository and git push to the hosted repo. Once you have created a remote repo with your service of choice you will need to update your local repo with a mapping. We discuss this process in the Configuration & Set Up guide below.
Steps:
git pull --rebase
git push -f
將做到一半的code丟在暫存區, and reverts the working directory to match the HEAD
commit.
Usage
Options
git stash (push)
:
(-m | --message) <message>
git apply --reject <patch>
git add the file
, then git am --continue
--no-commit
option:
git cherry-pick
.
gitg
gitg
:
multipass launch --name firefox
$ multipass mount /tmp/.X11-unix your_vm:/tmp/.X11-unix
$ multipass mount ~/.Xauthority foo:/home/ubuntu/.Xauthority
Source path "/Users/david/.Xauthority" is not a directory
$ multipass transfer ~/.Xauthority foo:/home/ubuntu/.Xauthority
netstat -nr
:ifconfig
:xhost
command with the IP address of OS on the machine you want to display on it (in red box on the below diagram)gitg
with DISPLAY parameter in the IP address on VM (in red box of below diagram) and we are done****.References:
lima
supports port fowarding
after v0.7.0libslirp
after v4.6.1Multipass
Issues I encountered
When I progress to the last step, executing gitg
with setting display to Host IP, but an error returned:
I tried to ping
the address and the IP pong
successfully:
Then I tried to use permission of root
, and it worked!
Which version of the git file will be finally used: LOCAL, BASE or REMOTE?
It's the one in the middle : BASE.
In fact, BASE is not the common ancestor, but the half-finished merge where conflicts are marked with >>>> and <<<<.
You can see the file names on the top of meld editing window.
You can edit the BASE file as you want with or without using meld commands.
You can also get rid of meld and just edit the file with your favorite text editor.
git log
on Linux: $tig
WIP
: work in progressgit clone
、git fetch
跟 git pull
這三個指令有什麼不同?git clone
clone 指令會把線上的專案,「整個」複製一份到你的電腦裡,並且在你的電腦裡建立相對應的標案及目錄(包括 .git
目錄),通常這個指令只會在一開始的時候使用,clone 之後要再更新的話,通常是執行 git fetch
或 git pull
指令。
git fetch
假設遠端節點叫做 origin
,當執行 git fetch
指令的時候,Git 會比對本機與遠端(在這邊就是 origin
)專案的差別,會「下載 origin
上面有但我本機目前還沒有」的內容下來,並且在本地端形成相對應的分支。
不過,fetch 指令只做下載,並不會進行合併。
git pull
pull 指令其實做的事情跟 fetch 是一樣的,差別只在於 fetch 只有把檔案抓下來,但 pull 不只抓下來,還會順便進行合併。也就是說,本質上,git pull 其實就等於 git fetch 加上 merge 指令。
Reset
, Rebase
, Revert
這三個命令的差別是?指令 | 改變歷史紀錄 | 說明 |
---|---|---|
Reset | 是 |
It often happens that while working on one project, you need to use another project from within it.
reference the answer for How do I revert my changes to a git submodule?
If you want to do this for all submodules, without having to change directories, you can perform
git submodule foreach git reset --hard
You can also use the recursive flag to apply to all submodules:
git submodule foreach --recursive git reset --hard
illustration (the branch in green)
Method 1: by git merge
I followed the basic merge
section introduced on the above further study on Merge, but a message of Automatic merge failed
was returned:
I checked the file to be merged and found that the merge is on HEAD
, not on the start point of the tested branch.
Git Merge from Atlassian:
However, a fast-forward merge is not possible if the branches have diverged. When there is not a linear path to the target branch, Git has no choice but to combine them via a 3-way merge:
So, We make a new commit from two branches;
BUT not merging the branch onto old commits of Main
by git cherry-pick
ref.: How to merge only specific commits from a pull request with git cherry-pick
The tested branch
is still merged to the latest commit of main
branch, not merged to the started commit of tested branch.
The tested branch can not be merged back to its started commit?
git rebase
ref.: 另一種合併方式(使用 rebase)
Steps:
git rebase <the_branch_to_be_base>
【狀況題】怎麼取消 rebase?
git merge
git reset HEAD^ --hard
git rebase
Reflog
git reset <sha_val_of_the_commit_before_doing_rebase> --hard
ORIG_HEAD
HEAD
pointer before doing danger operationgit reset ORIG_HEAD --hard
I got a conclusion that commits on other branches can only be merged onto the lastest commit of master
branch
All of the above methods can achieve our goal, I used git merge
and result is as follows:
301 redirect
From Hi everyone! I want to shorten the URL to a Github repo and use it for
git clone
.
Use "git restore <file>..."
The command:
branch
be renamed?Ref.:
https://poanchen.github.io/blog/2020/09/19/what-to-do-when-git-branch-has-diverged
git rebase origin/master
or git merge origin/master
I chose the method in the final for easily adopt.
keep revert history
from pal:Detail command
Check commit id of A
Create a commit (or patch) with reverting some file
…