Skip to content

Commit 6af8119

Browse files
committed
new notes on git
1 parent e0c01a8 commit 6af8119

File tree

7 files changed

+492
-1
lines changed

7 files changed

+492
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ DIRS_UNSORTED= rpi \
55
cpp \
66
vim \
77
windows \
8-
code_review
8+
code_review \
9+
git
910

1011
DIRS=$(sort $(DIRS_UNSORTED))
1112

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ List
88
- [c](c/c_notes.md)
99
- [code_review](code_review/code_review_notes.md)
1010
- [cpp](cpp/cpp_notes.md)
11+
- [git](git/git_notes.md)
1112
- [linux](linux/linux_notes.md)
1213
- [rpi](rpi/rpi_notes.md)
1314
- [vim](vim/vim_notes.md)

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ make NOTE_NAME=brave_browser_notes -C brave_browser -f brave_browser.mk --no-pri
33
make NOTE_NAME=c_notes -C c -f c.mk --no-print-directory
44
make NOTE_NAME=code_review_notes -C code_review -f code_review.mk --no-print-directory
55
make NOTE_NAME=cpp_notes -C cpp -f cpp.mk --no-print-directory
6+
make NOTE_NAME=git_notes -C git -f git.mk --no-print-directory
67
make NOTE_NAME=linux_notes -C linux -f linux.mk --no-print-directory
78
make NOTE_NAME=rpi_notes -C rpi -f rpi.mk --no-print-directory
89
make NOTE_NAME=vim_notes -C vim -f vim.mk --no-print-directory

git/git.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AUTOGENERATED!! DO NOT EDIT
2+
include ../constants.mk
3+
include ../common.mk

git/git_notes.html

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1" />
6+
<meta name="author" content="devpogi" />
7+
<link rel="stylesheet" href="../style.css" />
8+
<title>devpogi notes</title>
9+
</head>
10+
<body>
11+
<header>
12+
<a href="../index.html">Home</a>
13+
</header>
14+
15+
<h1 id="git-notes">Git Notes</h1>
16+
17+
<h2 id="common-commands">Common commands</h2>
18+
19+
<pre><code class="language-git">git help &#60;command&#62;
20+
git help branch
21+
22+
git config --global core.autocrlf input
23+
24+
git show HEAD
25+
git show HEAD^
26+
git show HEAD~
27+
git show HEAD~3
28+
git show ed23ab
29+
30+
git ls-tree HEAD
31+
git ls-tree HEAD^
32+
git ls-tree HEAD mydir&#47;
33+
34+
git log --author="prabal"
35+
git log --grep="notes"
36+
git log small-linklist&#47;src&#47;
37+
git log 3ed12f..HEAD
38+
39+
git log --oneline --decorate --graph --all
40+
41+
git diff master..other_branch
42+
git diff other_branch..master
43+
git diff 63d2344..52acfe2
44+
git diff
45+
</code></pre>
46+
47+
<p><code>git log -p</code><br/>
48+
shows all (diff) what was changed in each of the commits, <code>p</code> (patch) </p>
49+
50+
<p><code>git log --stat</code><br/>
51+
show only which files changed in all commits </p>
52+
53+
<p><code>git log -3 --stat</code><br/>
54+
shows only which files changed in the previous 3 commits </p>
55+
56+
<p><code>git show 1efdff small-linklist&#47;src&#47;node.c</code><br/>
57+
shows what was changed in this file in this commit id </p>
58+
59+
<p><code>git branch</code><br/>
60+
list of branches </p>
61+
62+
<p><code>git branch &#60;new_branch_name&#62;</code><br/>
63+
creates a new branch from the tip of the current branch, but does not switch to it </p>
64+
65+
<p><code>git checkout &#60;pre_existing_branch_name&#62;</code><br/>
66+
switches to that branch </p>
67+
68+
<p><code>git checkout -b &#60;new_branch_name&#62;</code><br/>
69+
creates a new branch (from the tip of the current branch) and switches to it. </p>
70+
71+
<p>The <strong>HEAD</strong> is a pointer pointing to the place (commit) from where new commits will be made. </p>
72+
73+
<p><code>cat .git&#47;HEAD</code> can be used to show the branch which the HEAD points to now. </p>
74+
75+
<p><code>ls -la .git&#47;refs&#47;heads</code> can be used to show what all branches the <strong>HEAD</strong> has the reference of.<br/>
76+
The directory contains a listing of all te branches - </p>
77+
78+
<pre><code class="language-bash">devp@IdeaPad:&#47;mnt&#47;d&#47;github&#47;small-linklist$ ls -la .git&#47;refs&#47;heads
79+
80+
total 0
81+
drwxrwxrwx 1 devp devp 4096 Apr 30 13:35 .
82+
drwxrwxrwx 1 devp devp 4096 Apr 30 13:35 ..
83+
-rwxrwxrwx 1 devp devp 41 Apr 30 13:35 main
84+
</code></pre>
85+
86+
<p>The contents of each of these files is the commit to which the <strong>HEAD</strong> would point when switched to that branch. </p>
87+
88+
<pre><code class="language-bash">devp@IdeaPad:&#47;mnt&#47;d&#47;github&#47;small-linklist$ cat .git&#47;refs&#47;heads&#47;main
89+
90+
89810c8b078dfa35872a6617bc5a2e22a6ee5deb
91+
92+
devp@IdeaPad:&#47;mnt&#47;d&#47;github&#47;small-linklist$ git log -1
93+
94+
commit 89810c8b078dfa35872a6617bc5a2e22a6ee5deb (HEAD -&#62; main, origin&#47;main, origin&#47;HEAD)
95+
Author: devprabal &#60;[email protected]&#62;
96+
Date: Fri Apr 29 22:45:37 2022 +0530
97+
98+
init gcov, lcov support
99+
</code></pre>
100+
101+
<p>If there are uncommit changes in a branch and we are trying to switch to another branch, then we can switch if the changes are untracked or if the changes do not conflict. Else, we will need to either commit changes, discard the changes (<code>git checkout --</code> those files), or <code>git stash</code> those files. </p>
102+
103+
<p><code>git branch --merged</code><br/>
104+
shows that this branch already has the commits of which other branches </p>
105+
106+
<p><code>git branch --no-merged</code> </p>
107+
108+
<p><code>git branch -m &#60;new_branch_name&#62;</code><br/>
109+
changes (or moves) the currently checked-out branch to a new name.<br/>
110+
CAUTION: may cause extra workarounds when other users are already working on the old name of branch. </p>
111+
112+
<p><code>git branch -d &#60;branch_to_delete&#62;</code><br/>
113+
First, checkout to another branch before deleting this branch. This command will complain if the changes are not merged in any other branch (<code>-D</code> to force delete).<br/>
114+
Returns the HEAD commit-id of the deleted branch. </p>
115+
116+
<h2 id="configure-ps1">configure PS1</h2>
117+
118+
<pre><code class="language-bash">export PS1=&#39;\w $(tput setaf 3)$(__git_ps1 "(%s)") $(tput sgr0)&#62; &#39;
119+
</code></pre>
120+
121+
<p><code>__git_ps1</code> is a fuction provided by <code>git-prompt.sh</code> file which can be found at <a href="https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh">github.com&#47;git</a> </p>
122+
123+
<p><a href="https://github.com/git/git/blob/master/contrib/completion/git-completion.bash">git-completion.bash</a> file can also be installed similarly. </p>
124+
125+
<h2 id="create-a-branch-using-git-command-line">Create a branch using git command line</h2>
126+
127+
<p>First, you must create your branch locally </p>
128+
129+
<pre><code class="language-git">git checkout -b your_branch
130+
</code></pre>
131+
132+
<p>After that, you can work locally in your branch. When you are ready to share the branch, push it. The next command pushes the branch to the remote repository origin and tracks it. </p>
133+
134+
<pre><code class="language-git">git push -u origin your_branch
135+
</code></pre>
136+
137+
<p>Teammates can reach your branch by doing: </p>
138+
139+
<pre><code class="language-git">git fetch
140+
git chekcout origin&#47;your_branch
141+
</code></pre>
142+
143+
<p>You can continue working in the branch and pushing whenever you want without passing arguments to git push (argument-less git push will push the local <code>master</code> to remote <code>master</code>, local <code>your_branch</code> to remote <code>your_branch</code>, etc.) </p>
144+
145+
<p><a href="https://stackoverflow.com/questions/1519006/how-do-i-create-a-remote-git-branch">stackoverflow: how-do-i-create-a-remote-git-branch</a> </p>
146+
147+
<h2 id="section"><code>bash \r not found</code></h2>
148+
149+
<p>Change git core autocrlf to input and re-clone the repo. </p>
150+
151+
<p><a href="https://stackoverflow.com/questions/29045140/env-bash-r-no-such-file-or-directory">stackoverflow: env-bash-r-no-such-file-or-directory</a> </p>
152+
153+
<h2 id="line-endings-crlf-and-lf">Line endings CRLF and LF</h2>
154+
155+
<p>If the project is cloned on linux (LF) server and we open it to edit in VSCode on Windows (CRLF), then we might do - </p>
156+
157+
<pre><code class="language-git">git config --global core.autocrlf true
158+
</code></pre>
159+
160+
<p>in both git bash (Windows) and on linux build server. </p>
161+
162+
<p>If you want to know what file this config is saved in, you can run the command: <code>git config --global --edit</code> </p>
163+
164+
<p><a href="https://stackoverflow.com/questions/10418975/how-to-change-line-ending-settings">stackoverflow: how-to-change-line-ending-settings</a> </p>
165+
166+
<h2 id="checkout-a-github-pr-locally-to-testeditresolve-merge-conflicts">Checkout a github PR locally to test&#47;edit&#47;resolve-merge-conflicts</h2>
167+
168+
<pre><code class="language-git">git fetch origin pull&#47;&#60;PR_ID&#62;&#47;head:&#60;new_branch_name&#62;
169+
</code></pre>
170+
171+
<p>Then checkout to <code>new_branch_name</code> </p>
172+
173+
<p><a href="https://stackoverflow.com/questions/27567846/how-can-i-check-out-a-github-pull-request-with-git">stackoverflow: how-can-i-check-out-a-github-pull-request-with-git</a> </p>
174+
175+
<h2 id="pusing-a-local-branch-to-remote">Pusing a local branch to remote</h2>
176+
177+
<pre><code class="language-git">git push -u origin &#60;new_branch_name&#62;
178+
</code></pre>
179+
180+
<h2 id="moving-histories-from-one-repo-to-another">Moving histories from one repo to another</h2>
181+
182+
<p>Problem:<br/>
183+
There is a new repo B and an old repo A. I want to have all commits from A into B preserving the commit history (<code>git log</code> should show me commit history) </p>
184+
185+
<p>Solution:<br/>
186+
- Go into B (&#8216;git clone <a href="mailto:[email protected]">[email protected]</a>: devpogi&#47;B.git<code>)
187+
-</code>git checkout B<em>new</em>branchname<code>
188+
- Add remote for repo A (</code>git remote add repAorigin <a href="mailto:[email protected]">[email protected]</a>:devpogi&#47;A.git<code>)
189+
-</code>git fetch repoAorigin<code>
190+
-</code>git merge repoAorigin&#47;master &#8211;allow-unrelated-histories<code>
191+
- Might introduce a merge conflict
192+
- Resolve the conflict and make a commit
193+
-</code>git push` </p>
194+
195+
<p><a href="https://stackoverflow.com/questions/17371150/moving-git-repository-content-to-another-repository-preserving-history">stackoverflow: moving-git-repository-content-to-another-repository-preserving-history</a> </p>
196+
197+
<p><a href="https://stackoverflow.com/questions/19475387/how-to-handle-fix-git-add-add-conflicts">stackoverflow: how-to-handle-fix-git-add-add-conflicts</a> </p>
198+
199+
<h2 id="section-1"><code>git patch</code></h2>
200+
201+
<p>If you haven&#8217;t yet committed the changed, then: </p>
202+
203+
<pre><code class="language-git">git diff &#62; mychanges.patch
204+
</code></pre>
205+
206+
<p>But it might be possible that there are several untracked (new) files which won&#8217;t be in your git diff output. So, one way to do a patch is to stage everything for a new commit (<code>git add</code> each file, or just <code>git add .</code>) but don&#8217;t do the commit yet, and then run: </p>
207+
208+
<pre><code class="language-git">git diff --cached &#62; mychanges.patch
209+
</code></pre>
210+
211+
<p>You can later apply the patch as: </p>
212+
213+
<pre><code class="language-git">git paply mychanges.patch
214+
</code></pre>
215+
216+
<p><a href="https://newbedev.com/create-a-git-patch-from-the-uncommitted-changes-in-the-current-working-directory">blogpost: create-a-git-patch-from-the-uncommitted-changes-in-the-current-working-directory</a> </p>
217+
218+
<h2 id="patch-files">Patch files</h2>
219+
220+
<p>If you need to send the patch file via mail, there is a better built-in way to do this in git using <code>git format-patch</code> command. </p>
221+
222+
<p>The patch file generated will include subject, author, etc, additional information traditionally used in UNIX-like mail-patching. </p>
223+
224+
<p>If you need top <code>n</code> commits from a specific commit-sha in the patch file - <code>git format-patch -n commit-sha</code> </p>
225+
226+
<p>example -<br/>
227+
<code>git
228+
git format-patch -1 HEAD
229+
</code></p>
230+
231+
<p>This will generate a file like <code>0001-commit-message.patch</code> </p>
232+
233+
<p>To apply this patch, use a series of steps -<br/>
234+
- <code>git apply --stat 0001-commit-message.patch</code> (<em>shows stats</em>)<br/>
235+
- <code>git apply --check 0001-commit-message.patch</code> (<em>checks for error before applying</em>)<br/>
236+
- <code>git am &#60; 0001-commit-message.patch</code> (<em>applies the patch, you will see the commit appear in <code>git log</code></em>) </p>
237+
</body>
238+
</html>

0 commit comments

Comments
 (0)