惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗
小众软件
小众软件
博客园 - Franky
D
Docker
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
宝玉的分享
宝玉的分享
C
Check Point Blog
B
Blog
V
V2EX
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
The Cloudflare Blog
博客园 - 聂微东
博客园_首页
Engineering at Meta
Engineering at Meta

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Git Learning Notes (1)
2020-04-23 · via jdhao's digital space

This is my personal note on learning Git.

Uncommited changes in a file are visible in all branches?#

I checked out from master branch to branch feature-foo and made some uncommitted changes in a file foo.py. When I switch back to branch master, I am surprised to find that changes in foo.py are visible in master branch.

I have also thought that changes in a branch belongs to that branch, but it seems that I am terribly wrong. The truth is: until you commit your changes in a certain branch, it will be visible in other branches too.

References#

How to show file version in a specific commit#

We can use git show <revision>:/path/to/file to show the version of file in a specific commit.

<revision> can be a commit hash or something like HEAD or HEAD~2 or tag name that can identify a specific commit. For example, to show file readme.md in current HEAD, use the following command:

To show readme.md in commit 410d12ed0, use git show 410d12ed0:readme.md.

References#

Delete files that have been commited after adding them to gitignore#

We may have commited some files that we do not want to include in the repo to the remote repo. How do we delete these files from remote repo?

First, we need to edit .gitignore file and add the files we want git to ignore. Then use the following command to delete these files from remote repo:

git rm --cached path/to/file
git commit -m "delete unwanted files"
git push origin master

The command git rm --cached test.txt will remove file test.txt from the Git index, but not from your disk. If you omit option --cached, the file will also be removed from your disk.

References#

Show difference between working directory, index and commit tree for a certain file#

Suppose we want to know the difference bewtween working directory, index, and commit tree for file test.txt, we can use the following commands:

  • git diff test.txt: difference between working directory and index
  • git diff HEAD test.txt: difference between working directory and HEAD
  • git diff --staged test.txt or git diff --cached test.txt: difference between index and HEAD.

References#

Difference between git add -A, git add . and git add -u?#

For the commit tree, there are three types of files: modifed, deleted and new files. What these commands do to the three types of files are:

commandmodified filesdeleted filesnew filesdescription
git add -Ayesyesyesstage modified, deleted and new files
git add .yesyesyesstage modified, deleted and new files EndFragment
git add -uyesyesnostage modified and deleted files, but not new files
git add --ignore-removal .yesnoyesstage new and modified files, but not deleted files

Using git add -A and git add -u without specifying the path will act on the entire working tree, regardless of your current directory:

If no is given when -u option is used, all tracked files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).

References#

How to list all file in current repository#

We can use git ls-tree command:

git ls-tree --name-only --full-tree -r HEAD

The meaning of options:

  • --full-tree: print the whole tree under the working tree root no matter which directory you are in
  • --name-only: print file names only (do not show file type and hash etc.)
  • -r: recursively list files under the root directory.

Note that if -r is omitted and only --full-tree and --name-only are used, only files and directories under repo root are listed.

We can also use git ls-files to show all the file in current repo. By default, git ls-files will also show files that are staged but not yet committed, which is different from git ls-tree since it only shows committed files. git ls-files also has more features than that.

References#