Skip to content

Commit 35f637f

Browse files
committed
update git cheat sheet
1 parent 3d3ef4a commit 35f637f

2 files changed

Lines changed: 102 additions & 34 deletions

File tree

docs/posts/2019/2019-06-19-git-cheat-sheet.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ categories:
66
comments: true
77
date:
88
created: 2019-06-19
9-
updated: 2025-07-09
9+
updated: 2025-07-12
1010
---
1111

1212
# Git Cheat Sheet
@@ -296,7 +296,7 @@ git config --global http.https://github.com.proxy $internet_proxy
296296

297297
## GUI
298298

299-
GitForWindows ships with a GUI tool, very cool.
299+
GitForWindows ships with a GUI tool.
300300

301301
```bash
302302
# start git gui tool
@@ -321,8 +321,64 @@ $ cd -
321321
$ rm -rf old-repository.git
322322
```
323323

324+
## Finding a string in the git log
325+
326+
```bash
327+
git log -Spassword
328+
```
329+
330+
## Finding where a file was added
331+
332+
- all occurrences (added, modified, deleted):
333+
334+
```bash
335+
git log -- file_name
336+
```
337+
338+
- only added:
339+
340+
there're also `A`, `M`, `D`, etc. <https://git-scm.com/docs/git-log#Documentation/git-log.txt---diff-filterACDMRTUXB82308203>
341+
342+
```bash
343+
git log --diff-filter=A -- file_name
344+
```
345+
346+
## Forcing local master to the same as origin/main
347+
348+
<https://superuser.com/a/273199>
349+
350+
```bash
351+
git checkout -B main origin/main
352+
```
353+
324354
## Ignoring pre-commit hook
325355
326356
```bash
327357
git commit --no-verify
328358
```
359+
360+
## Change user.email for some repos
361+
362+
For all repos under `~/git` that match a certain pattern `repo_pattern` in the git url, change the user email to `new_email`.
363+
364+
```bash
365+
new_email="my_new_email@copdips.com"
366+
repo_pattern="company_name"
367+
cd ~/git
368+
all_folders=$(ls -d $PWD/*)
369+
echo $all_folders | tr ' ' '\n' | while read -r folder ; \
370+
do \
371+
echo ====== $folder ; cd $folder ; \
372+
url=$(git remote get-url origin 2>/dev/null) ; \
373+
if [[ -n $url ]] ; then \
374+
if [[ $url =~ $repo_pattern ]] ; then \
375+
echo ~~~need to change email ; git config user.email $new_email ; \
376+
fi ; \
377+
fi ; \
378+
url= ; cd ~/git ; \
379+
done
380+
```
381+
382+
## bash-git-prompt tweaks
383+
384+
[Some tweaks](../2024/2024-01-05-bash-git-prompt-tweaks.md) I made to [bash-git-prompt](https://github.com/magicmonty/bash-git-prompt). dynamic Python venv path, new var `GIT_MESSAGE`, etc.

docs/posts/2024/2024-01-05-bash-git-prompt-tweaks.md

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ categories:
88
comments: true
99
date:
1010
created: 2024-01-05
11+
updated: 2025-07-12
1112
description: Some personal tweaks in bash-git-prompt
1213
---
1314

@@ -21,32 +22,28 @@ Some tweaks I made to [bash-git-prompt](https://github.com/magicmonty/bash-git-p
2122

2223
bash-git-prompt displays the current Python venv folder name within the prompt, among other things. I would like it to display the full path of the current Python venv if the venv is not located in the current folder. This will help prevent me from using the wrong venv when I switch between projects.
2324

24-
To achieve this, I have to modify the `gp_add_virtualenv_to_prompt` function in the `~/.bash-git-prompt/gitprompt.sh` script, as I used the [git clone method](https://github.com/magicmonty/bash-git-prompt#via-git-clone) to install bash-git-prompt.
25-
26-
```diff title="if the venv is not .venv inside current folder, display the full path of the venv. Node and Conda venv are not modified as I don't use them."
27-
function gp_add_virtualenv_to_prompt {
28-
local ACCUMULATED_VENV_PROMPT=""
29-
local VENV=""
30-
if [[ -n "${VIRTUAL_ENV-}" && -z "${VIRTUAL_ENV_DISABLE_PROMPT+x}" ]]; then
31-
+ PYTHON_VERSION=$(python --version | cut -d' ' -f2 | cut -d'.' -f1-2)
25+
To achieve this, add following code into `function gp_add_virtualenv_to_prompt` in `~/.bash-git-prompt/gitprompt.sh` to add Python virtual environment name and version to the prompt.
26+
27+
```diff title="bash_git_prompt_venv.patch" hl_lines="5"
28+
diff --git a/gitprompt.sh b/gitprompt.sh
29+
index 978cae7..0f33e0e 100755
30+
--- a/gitprompt.sh
31+
+++ b/gitprompt.sh
32+
@@ -649,7 +649,11 @@ function gp_add_virtualenv_to_prompt {
33+
local ACCUMULATED_VENV_PROMPT=""
34+
local VENV=""
35+
if [[ -n "${VIRTUAL_ENV-}" && -z "${VIRTUAL_ENV_DISABLE_PROMPT+x}" ]]; then
36+
- VENV=$(basename "${VIRTUAL_ENV}")
3237
+ if [[ $VIRTUAL_ENV == "$(pwd)/.venv" ]]; then
3338
+ VENV=$(basename "${VIRTUAL_ENV}")
3439
+ else
3540
+ VENV=$VIRTUAL_ENV
3641
+ fi
42+
+ PYTHON_VERSION=$(python --version | cut -d' ' -f2 | cut -d'.' -f1-2)
3743
+ VENV="$VENV $PYTHON_VERSION"
38-
ACCUMULATED_VENV_PROMPT="${ACCUMULATED_VENV_PROMPT}${GIT_PROMPT_VIRTUALENV//_VIRTUALENV_/${VENV}}"
39-
fi
40-
if [[ -n "${NODE_VIRTUAL_ENV-}" && -z "${NODE_VIRTUAL_ENV_DISABLE_PROMPT+x}" ]]; then
41-
VENV=$(basename "${NODE_VIRTUAL_ENV}")
42-
ACCUMULATED_VENV_PROMPT="${ACCUMULATED_VENV_PROMPT}${GIT_PROMPT_VIRTUALENV//_VIRTUALENV_/${VENV}}"
43-
fi
44-
if [[ -n "${CONDA_DEFAULT_ENV-}" ]]; then
45-
VENV=$(basename "${CONDA_DEFAULT_ENV}")
46-
ACCUMULATED_VENV_PROMPT="${ACCUMULATED_VENV_PROMPT}${GIT_PROMPT_VIRTUALENV//_VIRTUALENV_/${VENV}}"
47-
fi
48-
echo "${ACCUMULATED_VENV_PROMPT}"
49-
}
44+
ACCUMULATED_VENV_PROMPT="${ACCUMULATED_VENV_PROMPT}${GIT_PROMPT_VIRTUALENV//_VIRTUALENV_/${VENV}}"
45+
fi
46+
if [[ -n "${NODE_VIRTUAL_ENV-}" && -z "${NODE_VIRTUAL_ENV_DISABLE_PROMPT+x}" ]]; then
5047
```
5148

5249
## New var GIT_MESSAGE
@@ -59,20 +56,35 @@ alias gitpush='git ci -am "$GIT_MESSAGE" ; git push origin $GIT_BRANCH`
5956
6057
So I can just type `gitpush` to commit and push my changes to the remote repo.
6158
62-
To achieve this, I have to add one line right after `GIT_BRANCH` in the `~/.bash-git-prompt/gitprompt.sh` script.
59+
To achieve this, declare a function `set_git_message` in `~/.bash-git-prompt/gitprompt.sh` to set the `GIT_MESSAGE` variable based on the current branch name. Then, call `set_git_message` in the `updatePrompt` function in the same file to get called upon each prompt re-computation, for e.g. right after the line `export GIT_BRANCH=$(replaceSymbols "${git_status_fields[@]:0:1}")`.
6360
64-
```diff title="add new var GIT_MESSAGE"
65-
export GIT_BRANCH=$(replaceSymbols "${git_status_fields[@]:0:1}")
66-
+ export GIT_MESSAGE=$( [[ "$GIT_BRANCH" == */* ]] && echo "${GIT_BRANCH%/*}/$(echo ${GIT_BRANCH##*/} | sed 's/-/ /g')" || echo "${GIT_BRANCH//-/ }" )
67-
```
61+
```bash title="~/.bash-git-prompt/gitprompt.sh"
62+
# given GIT_BRANCH : feature/JIRA-123/some-feature
63+
# got GIT_MESSAGE : feature/JIRA-123: some feature
64+
function set_git_message() {
65+
66+
# Check if the string contains a '/'
6867
69-
What this line does is:
68+
if [[ $GIT_BRANCH == */* ]]; then
69+
# If it does, get the substring after the last '/'
70+
MSG=${GIT_BRANCH##*/}
71+
# Get the prefix by removing the message part from the original string
72+
PREFIX=${GIT_BRANCH%/$MSG}
73+
else
74+
# If it doesn't, the whole string is the message
75+
PREFIX=""
76+
MSG=$GIT_BRANCH
77+
fi
7078

71-
- If the branch name contains `/`, `-` in the left part of the last `/` in the branch name will be converted to space.
72-
- If the branch name does not contain `/`, all `-` in the branch name will be converted to space.
79+
# Replace "-" with " " in the message part
7380

74-
For example:
81+
MSG=${MSG//-/ }
7582

76-
- branch name `feat/#111/add-new-feature_a` will be converted to message `feat/#111/add new feature_a`.
77-
- branch name `#111/add-new-feature_a` will be converted to message `#111/add new feature_a`.
78-
- branch name `add-new-feature_a` will be converted to message `add new feature_a`.
83+
# Combine prefix and message into the new variable
84+
if [[ -z $PREFIX ]]; then
85+
export GIT_MESSAGE="$MSG"
86+
else
87+
export GIT_MESSAGE="${PREFIX}: ${MSG}"
88+
fi
89+
}
90+
```

0 commit comments

Comments
 (0)