Add some more advanced tips

This commit is contained in:
Chalarangelo
2021-04-06 21:35:01 +03:00
parent 676a0aba80
commit cf58b9c285
5 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,17 @@
---
title: Add a commit message template
tags: repository,configuration,advanced
---
Sets up a commit message template for the current repository.
- Use `git config commit.template <file>` to specify `<file>` as the commit message template for the current repository.
```sh
git config commit.template <file>
```
```sh
git config commit.template "commit-template"
# Sets "commit-template" as the commit message template
```

View File

@ -0,0 +1,20 @@
---
title: Find lost files
tags: repository,advanced
---
Prints a list of lost files and commits.
- Use `git fsck --lost-found` to print a list of all dangling objects.
- All appropriate files will be extracted into the `.git/lost-found` directory.
```sh
git fsck --lost-found
```
```sh
git fsck --lost-found
# dangling commit 3050fc0de
# dangling blob 807e3fa41
# dangling commit 59ff8481d
```

17
snippets/line-endings.md Normal file
View File

@ -0,0 +1,17 @@
---
title: Configure line endings
tags: repository,configuration,intermediate
---
Configures the line endings for a repository.
- Use `git config core.eol [lf | crlf]` to configure the line endings.
- `lf` is the UNIX line ending (`\n`), whereas `crlf` is the DOS line ending (`\r\n`).
```sh
git config core.eol [lf | crlf]
```
```sh
git config core.eol lf # Configured to use UNIX line endings
```

View File

@ -0,0 +1,27 @@
---
title: Move commits from master to a new branch
tags: branch,repository,intermediate
---
Moves local commits from the `master` branch to a new branch.
- Use `git branch <branch>` to create a new branch at the tip of the current `master`.
- Use `git reset HEAD~<n> --hard` to rewind back `<n>` commits and discard changes.
- Use `git checkout <branch>` to switch to the new branch.
- Only works if the changes have only been committed locally and not pushed to the remote.
```sh
git branch <branch>
git reset HEAD~<n> --hard
git checkout <branch>
```
```sh
git checkout master
git add .
git commit -m "Fix network bug"
git branch patch-1
# `patch-1` branch is created containing the commit "Fix network bug"
git reset HEAD~1 --hard # Remove the commit from `master`
git checkout patch-1
```

View File

@ -0,0 +1,20 @@
---
title: View branches sorted by date
tags: repository,branch,intermediate
---
Prints a list of all local branches sorted by date.
- Use `git branch --sort=-committerdate` to display a list of all local branches and sort them based on the date of their last commit.
- Use arrow keys to navigate, press <kbd>Q</kbd> to exit.
```sh
git branch --sort=-committerdate
```
```sh
git branch --sort=-committerdate
# master
# patch-1
# patch-2
```