Skip to content

Latest commit

 

History

History
489 lines (407 loc) · 24.5 KB

README.org

File metadata and controls

489 lines (407 loc) · 24.5 KB

Git layer

img/git.png

Table of Contents

Description

This layers adds extensive support for git to Spacemacs.

Features:

New to Magit? Checkout the official intro and Practicalli Spacemacs guide to configuring and using the Git and version control layers.

Install

Layer

To use this configuration layer, add it to your ~/.spacemacs. You will need to add git to the existing dotspacemacs-configuration-layers list in this file.

Git

Of course if your OS does not ship with git (!) you’ll have to install it on your machine. You can download it from the download page.

Basic Git configuration

Define a git identity using the git commands in a terminal window replacing username and name@domain.tld with your own values

git config --global user.name "username"
git config --global user.email "name@domain.tld"

Using SSH URLs for remote repositories

Generate an SSH key to use SSH URLs to access remote repositories. Add the SSH key to your account on the remote service managing repository access. An SSH key removes the need to provide login details for each request from Magit to the remote repository service.

Magit status fullscreen

To display the magit status buffer in fullscreen set the variable git-magit-status-fullscreen to t in your dotspacemacs/user-init function.

(defun dotspacemacs/user-init ()
  (setq-default git-magit-status-fullscreen t))

Magit auto-complete

Magit auto-complete feature is enabled by default. For this feature to work best - setup magit repository list.

Magit Plugins

magit-delta

magit-delta uses delta to display diffs, with extensive changes to its layout and styles.

You need to install delta first, and add the following to your dotspacemacs/user-config:

(setq-default dotspacemacs-configuration-layers
              '((git :variables git-enable-magit-delta-plugin t)))

magit-gitflow

git-flow is a standardized branching pattern for git repositories with the aim of making things more manageable. While there are tools to assist with making this easier, these do nothing you couldn’t do manually.

After installing git-flow, add the following to your dotspacemacs/user-config:

(setq-default dotspacemacs-configuration-layers
              '((git :variables git-enable-magit-gitflow-plugin t)))

magit-svn

magit-svn shows commits which were not pushed to svn yet. Press ! in Magit buffer to open the magit-svn-popup. There you can push to or rebase from svn.

To enable magit-svn plugin, add the following to your dotspacemacs/user-config:

(setq-default dotspacemacs-configuration-layers
              '((git :variables git-enable-magit-svn-plugin t)))
Key bindingDescription
~~~open magit-svn-popup

magit-todos

magit-todos displays TODO-entries in source code comments and Org files in the Magit status buffer.

To enable magit-todos plugin, add the following to your dotspacemacs/user-config:

(setq-default dotspacemacs-configuration-layers
              '((git :variables git-enable-magit-todos-plugin t)))

Global git commit mode

Spacemacs can be used as the $EDITOR (or $GIT_EDITOR) for editing git commits messages. This requires the entire library to be loaded immediately which will cost some time, disable it if you run into performance issues. To enable it you have to add the following lines to your dotspacemacs/user-config:

(require 'git-commit)
(global-git-commit-mode t)

Forge

Magit Forge can view and create issues & pull requests with forges (e.g. GitHub, GitLab)

Magit Forge requires a username for the respective forge and will prompt for a username if not found, writing it to ~/.gitconfig

Explicitly define a forge identity using the `git` command.

For GitHub:

git config --global github.user "username"

For GitLab:

git config --global gitlab.user "username"

See the official Magit Forge and GHub Getting Started for general guides or follow a community written Spacemacs specific guide to configuring Magit Forge.

Magit Forge configuration

For each forge (e.g. GitHub, GitLab), add a machine configuration to the PGP encrypted ~/.authinfo.gpg file. Detailed instructions to create an encrypted .authinfo.gpg file with Spacemacs

The machine configuration should use your forge username and personal access token GitHub token permissions: repo, user and read:org GitLab token permissions: api

machine api.github.com login forge-username^forge password 01personal02access03token

MS Windows support

The forge package uses emacsql which requires a C compiler to be available on MS Windows, see issue magit/emacsql#46.

For this reason the forge package is not installed on MS Windows by default. If you still want to install it (which means you do have a C compiler available in your PATH) then use the dotspacemacs-additional-packages variable in your dotfile:

(dotspacemacs-additional-packages '((forge :toggle t)))

Org integration

See the commentary section of the package here.

Working with Git

Git commands (start with g):

Key bindingDescription
SPC g /open helm-git-grep
SPC g *open helm-git-grep-at-point
SPC g bopen a magit blame
SPC g f fview a file at a specific branch or commit
SPC g f lcommits log for current file
SPC g f ddiff for current file
SPC g f mmagit dispatch popup for file operations
SPC g H cclear highlights
SPC g H hhighlight regions by age of commits
SPC g H thighlight regions by last updated time
SPC g iinitialize a new git repository
SPC g Lopen magit-repolist
SPC g sopen a magit status window
SPC g Sstage current file
SPC g mmagit dispatch popup
SPC g Mdisplay the last commit message of the current line
SPC g tlaunch the git time machine
SPC g Uunstage current file

Notes:

  • Highlight by age of commit or last update time is provided by smeargle.
  • Git time machine is provided by git-timemachine.
  • Git last commit message per line is provided by git-messenger.

Magit

Spacemacs uses magit to manage Git repositories.

To open a status buffer, type in a buffer of a Git repository: SPC g s. The central key binding hub of Magit is available on SPC g m.

Spacemacs uses forge for integration with remote forges, it is available from the status buffer with the @ key binding. For information on setting up remotes check the manual’s Getting Started page.

Spacemacs uses evil-collection-magit for key bindings in magit buffers (unless your editing style is set to emacs, in which case you get the default magit bindings), which are the standard magit key bindings with some minimal changes to make them comfortable for evil users.

Here are the often used bindings inside a status buffer:

Key bindingDescription
/evil-search
$open command output buffer
c copen a commit message buffer
b bcheckout a branch
b ccreate a branch
f ffetch changes
F (r) upull tracked branch and rebase
grrefresh
jgoto next magit section
C-jnext visual line
kgoto previous magit section
C-kprevious visual line
l lopen log buffer
nnext search occurrence
Nprevious search occurrence
orevert item at point
P upush to tracked branch
P mpush to matching branch (e.g., upstream/develop to origin/develop)
qquit
son a file or hunk in a diff: stage the file or hunk
xdiscard changes
+on a hunk: increase hunk size
=on a hunk: decrease hunk size
Sstage all
TABon a file: expand/collapse diff
uon a staged file: unstage
Uunstage all staged files
v or Vselect multiple lines
z zstash changes

Staging lines

Magit allows you to stage specific lines by selecting them in a diff and hitting s to stage. Due to inconsistencies between Vim and Emacs editing styles, if you enter visual line state with V, you will stage one more line than intended. To work around this, you can use v instead (since Magit only stages whole lines, in any case).

Commit message editing buffer

In a commit message buffer the following key bindings are active:

Key bindingDescription
SPC m c or ~SPC m ,~commit changes with the entered message
SPC m a or SPC m kdiscard message and abort the commit
g j or M-ncycle through history to the previous commit message
g k or M-psave current commit message and cycle to the next message

In addition, regular commands for saving and killing a buffer such as :wq and ZZ can be used to commit changes.

Log selection buffer

A log selection buffer is presented as an interactive way of selecting a recent commit that is reachable from HEAD. such as when selecting the beginning of a rebase and when selecting a commit to be squashed into.

Key bindingDescription
SPC m c or ~SPC m ,~select the commit at point and act on it
SPC m a or SPC m kabort selecting and don’t act on any commit

Interactive rebase buffer

Key bindingDescription
c or ppick
eedit
ffixup
jgo down
M-jmove line down
kgo up
M-kmove line up
d or xkill line
rreword
ssquash
uundo
yinsert
!execute

Quick guide for recurring use cases in Magit

  • Amend a commit:
    • l l to open log buffer
    • c a on the commit you want to amend
    • ​,​c or C-c C-c to submit the changes
  • Squash last commit:
    • l l to open log buffer
    • r e on the second to last commit, it opens the rebase buffer
    • j to put point on last commit
    • s to squash it
    • ​,​c or C-c C-c to continue to the commit message buffer
    • ​,​c or C-c C-c again when you have finished to edit the commit message
  • Force push a squashed commit:
    • in the status buffer you should see the new commit unpushed and the old commit unpulled
    • P -f P for force a push (beware usually it is not recommended to rewrite the history of a public repository, but if you are sure that you are the only one to work on a repository it is ok - i.e. in your fork).
  • Add upstream remote (the parent repository you have forked):
    • M to open the remote popup
    • a to add a remote, type the name (i.e. upstream) and the URL
  • Pull changes from upstream (the parent repository you have forked) and push:
    • F -r C-u F and choose upstream or the name you gave to it
    • P P to push the commit to origin

Git Blame Transient State

Key bindingDescription
SPC g bstart magit-blame and open the git blame transient state
?toggle hint
pprev chunk
Pprev chunk same commit
nnext chunk
Nnext chunk same commit
RETshow commit
bshow commits with adding lines
rshow commits with removing lines
fshow last commits that still have lines
eshow line revision info in echo area (not read only)
qkill recursive blame buffer or disable magit-blame-mode
ccycle style
Ycopy hash
Bmagit-blame (magit transient)
Qquit transient state

Git-Flow

magit-gitflow provides git-flow commands in its own magit menu.

Key bindingDescription
%open magit-gitflow menu

Git time machine

git-timemachine allows to quickly browse the commits of the current buffer.

Key bindingDescription
SPC g tstart git timemachine and initiate transient-state
cshow current commit
nshow next commit
Nshow previous commit
pshow previous commit
qleave transient-state and git timemachine
Ycopy current commit hash

Git links to web services

These key bindings allow to quickly construct URLs pointing to a given commit or lines in a file hosted on Git web services like GitHub, GitLab, Bitbucket…

Key bindingDescription
SPC g l con a commit hash, browse to the current file at this commit
SPC g l Con a commit hash, create link to the file at this commit and copy it
SPC g l lon a region, browse to file at current lines position
SPC g l Lon a region, create a link to the file highlighting the selected lines
SPC g l pon a region, browse to file at current lines position (using permalink link)
SPC g l Pon a region, create a link to the file highlighting the selected lines (using permalink link)

Notes:

  • You can use the universal argument SPC u to select a remote repository.
  • When the link is opened, the URL is also copied in the kill ring, you can override this behavior by setting the variable git-link-open-in-browser to nil.

Repository list

Feature displays a status-list of git repositories. Within your .spacemacs config, in the dotspacemacs/user-config() stanza configure magit-repository-directories to target Emacs to directories to look into.

(setq magit-repository-directories
      '(("~/Development/" . 2) ("~/src/" . 2)))

Where each element has the form (DIRECTORY . DEPTH), when DEPTH is 0 - then only add DIRECTORY itself. The DIRECTORY should end up with a / to respect Emacs conventions.

Key bindingDescription
SPC g Lstart git repo list
RETshow the git status window for the selected project
grrefresh the project list

For more information, look into Magit-User-Manual#Status-Buffer

Forge

In a magit-status buffer (SPC g s):

Key bindingDescription
b Ncreate branch from pull-request
b Fcreate and check out branch from pull-request
@ f ffetch issues and pull-requests
@ f nfetch notifications
@ c pcreate pull-request
@ c icreate issue
@ l nlist notifications
@ l plist pull-requests
@ l ilist issues
f npull pull-requests and issues for the current repository
f Npull all notifications for the current repository’s forge

In a forge-topic buffer: (a topic is either an issue or pull request)

Key bindingDescription
SPC m aassign people to topic
SPC m bbrowse topic (open in web browser)
SPC m ccreate comment post to existing topic)
SPC m CCheckout pull request (not for issues)
SPC m dtoggle draft pull request
SPC m Ddelete comment under cursor
SPC m eedit topic body
SPC m medit topic marks (mark is an unshared label)
SPC m Mcreate mark to use with topics
SPC m nedit personal note (adds to top of topic)
SPC m redit list of people to review an existing topic
SPC m schange topic state (open, closed, draft, etc.)
SPC m tedit topic title
SPC m ucopy URL of topic (add to kill ring)

In a forge-post buffer (assuming the major mode leader key is ~,~)

Key bindingDescription
SPC m c or ~SPC m ,~submit post
SPC m k or SPC m kcancel post