diff --git a/snippets/commit-template.md b/snippets/commit-template.md new file mode 100644 index 000000000..e39ebaec2 --- /dev/null +++ b/snippets/commit-template.md @@ -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 ` to specify `` as the commit message template for the current repository. + +```sh +git config commit.template +``` + +```sh +git config commit.template "commit-template" +# Sets "commit-template" as the commit message template +``` diff --git a/snippets/find-lost-files.md b/snippets/find-lost-files.md new file mode 100644 index 000000000..39600189f --- /dev/null +++ b/snippets/find-lost-files.md @@ -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 +``` diff --git a/snippets/line-endings.md b/snippets/line-endings.md new file mode 100644 index 000000000..4aa3a5d03 --- /dev/null +++ b/snippets/line-endings.md @@ -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 +``` diff --git a/snippets/move-commits-to-branch.md b/snippets/move-commits-to-branch.md new file mode 100644 index 000000000..fa241e7ae --- /dev/null +++ b/snippets/move-commits-to-branch.md @@ -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 ` to create a new branch at the tip of the current `master`. +- Use `git reset HEAD~ --hard` to rewind back `` commits and discard changes. +- Use `git checkout ` 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 +git reset HEAD~ --hard +git checkout +``` + +```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 +``` diff --git a/snippets/sort-branches-by-date.md b/snippets/sort-branches-by-date.md new file mode 100644 index 000000000..d45fc4072 --- /dev/null +++ b/snippets/sort-branches-by-date.md @@ -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 Q to exit. + +```sh +git branch --sort=-committerdate +``` + +```sh +git branch --sort=-committerdate +# master +# patch-1 +# patch-2 +```