Commit 2253a8bd authored by Vincent Tam's avatar Vincent Tam

Page Git added

parent 7bef736b
Pipeline #40836303 passed with stages
in 36 seconds
......@@ -115,6 +115,12 @@ enableGitInfo = true
url = "page/sublime/"
weight = 2
[[menu.main]]
parent = "samples"
name = "Advanced Git"
url = "page/advanced-git/"
weight = 3
[[menu.main]]
name = "Categories"
url = "categories"
......
---
title: "Advanced Git"
subtitle: "More delicate Git commands"
type: "page"
date: 2018-12-20T08:29:17+01:00
toc: true
draft: false
---
Here goes more advanced [Git] commands _not_ found among
[the basic ones][cmd_list].
### for-each-ref
Display info in `.git/refs`.
```sh
#!/bin/sh
git for-each-ref --shell --format="ref=%(refname)" refs/tags | \
while read entry
do
eval "$entry"
echo `dirname $ref`
done
```
This modified example taken from [the one in the official manual][foreg] pipes
ref=refs/tags/181116
ref=refs/tags/copyBtn0
ref=refs/tags/fa531
ref=refs/tags/solarized
to a `while` loop. Each line represents an `entry`. In each iteration, `eval`
set the shell variable `ref` to the output Git reference name. `dirname` chops
off the tag names (`181116`, `fa531`, etc) and returns `refs/tags`.
### ls-files, ls-tree
List files (resp. files in tree).
#### Examples
$ git ls-files
$ git ls-tree -r -names-only # "-r" means recursive
#### Applications
For iteration over cached files only if the file name is "regular enough".
These two commands are synonymous to each other.
### merge-base
Return the SHA1 hash of the youngest common ancestor of two branches.
$ git merge-base master dev
#### Applications
### merge --squash, rebase -i
`merge --squash <src>` condenses commits into one on the current branch
_without_ touching `src`.
`rebase`, meaning "re-base", is analogous to a pot transplant. The `-i` flag
opens an interactive session for that.
#### Applications
Both `merge --squash` and `rebase` can be used for cleaning the commit history.
The form can be used if `<src>` branch is to be thrown away. In `rebase -i`,
one can also squash Git commits.
### reflog
Track the SHA1 hash that `HEAD` represents.
#### Applications
Find lost commit.
### rev-parse
Return the SHA1 hash that Git `ref` represents.
$ rev-parse FETCH_HEAD # head of fetched commits
#### Applications
Verify repo status.
[Git]: https//git-scm.com
[cmd_list]: ../bash-commands/
[foreg]: https://git-scm.com/docs/git-for-each-ref
......@@ -90,7 +90,7 @@ duplicating efforts.
I use it to extract column(s). I _don't_ know why double quotes `"` _doesn't_
work.
$ ls -dl * | awk '{print $9, $6}'
$ ls -dl * | awk '{print $9, $5}'
archetypes 4096
config.toml 2861
content 4096
......@@ -99,6 +99,13 @@ work.
static 4096
themes 4096
It can be used to extract Git remote URL from
[`git remote -v`](#working-with-others). They stand for fetch and push URLs.
$ git remote -v | awk '{print $2}'
git@gitlab.com:VincentTam/vincenttam.gitlab.io.git
git@gitlab.com:VincentTam/vincenttam.gitlab.io.git
### bash
GNU's **B**ourne-**a**gain **sh**ell. Use this with `-c [CMD]` to execute a
......@@ -265,6 +272,9 @@ See also: [`wget`](#wget)
#### Shorten GitHub URL
The online version of [GitHub's URL shortener][git.io] _doesn't_ allow
user-supplied short name.
Sucessful outcome
$ curl -i https://git.io -F "url=https://vincenttam.github.io/beautiful-jekyll" \
......@@ -314,6 +324,29 @@ References:
2. [curl tutorial][curl_haxx]
3. [curl POST examples][curl_post]
### cut
I discovered this new way of extracting columns with custom delimiters.
$ git --version
git version 2.17.1
$ git --version -v | cut -d' ' -f1 # returns git
$ git --version -v | cut -d' ' -f2 # returns version
$ git --version -v | cut -d' ' -f3 # returns 2.17.1
However, this _can't_ be used for extracting the Git remote URL from
[`git remote`](#working-with-others) due to tabs `\t` separating the first two
columns.
$ git remote -v | head -1 | od -An -c
o r i g i n \t g i t @ g i t l a
b . c o m : V i n c e n t T a m
/ v i n c e n t t a m . g i t l
a b . i o . g i t ( f e t c h
) \n
In this case, [`awk`](#awk) has to be used.
### date
Display or adjust system date. Default to current time (zone).
......@@ -755,7 +788,8 @@ ago. `n` should be an integer.
### git
The most popular VCS (version control system). Here's a _minimal_ collection of
<i class="fab fa-git" aria-hidden></i> commands needed for starters.
<i class="fab fa-git" aria-hidden></i> commands needed for starters. I've put
[more advanced Git commands][adv_git] in a separate page.
#### getting started
......@@ -857,7 +891,19 @@ core stuff
+ `-v`: show the SHA-1 hash at the tip of branch
- switch betwween branches: `git checkout <branch-name>`
- delete branch: `git branch -d <branch-name>`
- compare branches: `git diff <branch1>..<branch2>`
- compare branches/commits: `git diff <branch1>..<branch2>`
- two dots: usual `diff -u`
$ git merge
$ git diff HEAD^..HEAD # view changes introduced by previous merge
- three dots: function like two dots, but compare their common ancestor with
`branch2` instead, useful for checking merge conflicts.
The two arguments for `git diff` can technically be any Git references
(commits, branches, tags).
- merge changes from other local branch: `git merge <branch-name>`
#### work with others
......@@ -888,6 +934,8 @@ repo to an SSH remote for sharing.
branch.
- `git pull <remote-name> <branch-name>`: perform `git fetch` _and_ `git
merge`.
- See [my GitHub pull request tutorial][pr_tut] to get the _latest_ features
in development from [GitHub].
### git-annex
......@@ -1747,6 +1795,9 @@ Ubuntu&nbsp;18.04)
[curl_post]: https://gist.github.com/subfuzion/08c5d85437d5d4f00e58
[tldr]: https://tldr.sh/
[so_kill]: https://unix.stackexchange.com/a/2112
[pr_tut]: /post/2018-11-18-merge-github-pull-requests.md
[GitHub]: https://github.com/
[git.io]: https://git.io/
[git-annex-tut1]: https://writequit.org/articles/getting-started-with-git-annex.html#h:d8e38f22-8ea0-461c-b888-226e3ebc60b2
[git-annex-tut2]: https://lucas.bourneuf.net/blog/tuto-git-annex.html
[git-annex-walk]: https://git-annex.branchable.com/walkthrough/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment