2017-08-28 15:33:55 +00:00
#+TITLE : Git layer
2015-06-10 16:44:30 +00:00
2019-05-02 21:49:30 +00:00
#+TAGS : layer|versioning
2015-06-10 16:44:30 +00:00
[[file:img/git.png ]]
2019-05-07 20:05:06 +00:00
* Table of Contents :TOC_5_gh:noexport:
2018-02-01 16:20:53 +00:00
- [[#description ][Description ]]
2017-05-16 13:11:18 +00:00
- [[#features ][Features: ]]
- [[#install ][Install ]]
- [[#layer ][Layer ]]
2021-03-25 05:17:42 +00:00
- [[#git ][Git ]]
2017-05-16 13:11:18 +00:00
- [[#magit-status-fullscreen ][Magit status fullscreen ]]
- [[#magit-auto-complete ][Magit auto-complete ]]
2021-03-25 05:17:42 +00:00
- [[#magit-plugins ][Magit Plugins ]]
- [[#magit-delta ][magit-delta ]]
- [[#magit-gitflow ][magit-gitflow ]]
- [[#magit-svn ][magit-svn ]]
- [[#magit-todos ][magit-todos ]]
2018-05-27 06:48:22 +00:00
- [[#global-git-commit-mode ][Global git commit mode ]]
2020-11-14 16:53:33 +00:00
- [[#forge ][Forge ]]
2018-05-27 06:48:22 +00:00
- [[#org-integration ][Org integration ]]
2017-05-16 13:11:18 +00:00
- [[#working-with-git ][Working with Git ]]
- [[#magit ][Magit ]]
- [[#staging-lines ][Staging lines ]]
- [[#commit-message-editing-buffer ][Commit message editing buffer ]]
2019-01-18 11:30:06 +00:00
- [[#log-selection-buffer ][Log selection buffer ]]
2017-05-16 13:11:18 +00:00
- [[#interactive-rebase-buffer ][Interactive rebase buffer ]]
- [[#quick-guide-for-recurring-use-cases-in-magit ][Quick guide for recurring use cases in Magit ]]
2020-06-04 21:50:03 +00:00
- [[#git-blame-transient-state ][Git Blame Transient State ]]
2021-03-25 05:17:42 +00:00
- [[#git-flow ][Git-Flow ]]
2017-05-16 13:11:18 +00:00
- [[#git-time-machine ][Git time machine ]]
- [[#git-links-to-web-services ][Git links to web services ]]
- [[#repository-list ][Repository list ]]
2020-11-14 16:53:33 +00:00
- [[#forge-1 ][Forge ]]
2017-05-16 13:11:18 +00:00
2018-02-01 16:20:53 +00:00
* Description
This layers adds extensive support for [[http://git-scm.com/ ][git ]] to Spacemacs.
2015-06-10 16:44:30 +00:00
2015-06-10 21:16:01 +00:00
** Features:
2015-12-11 12:01:52 +00:00
- git repository management the indispensable [[http://magit.vc/ ][magit ]] package
2020-11-14 16:53:33 +00:00
- [[https://github.com/magit/forge/ ][forge ]] add-on for magit.
2015-06-10 16:44:30 +00:00
- [[https://github.com/jtatarik/magit-gitflow ][git-flow ]] add-on for magit.
2018-07-19 23:43:51 +00:00
- quick in buffer history browsing with [[https://melpa.org/#/git-timemachine ][git-timemachine ]].
2015-06-10 16:44:30 +00:00
- quick in buffer last commit message per line with [[https://github.com/syohex/emacs-git-messenger ][git-messenger ]]
- colorize buffer line by age of commit with [[https://github.com/syohex/emacs-smeargle ][smeargle ]]
2018-03-27 01:19:04 +00:00
- git grep with [[https://github.com/yasuyk/helm-git-grep ][helm-git-grep ]]
2015-06-10 16:44:30 +00:00
- gitignore generator with [[https://github.com/jupl/helm-gitignore ][helm-gitignore ]]
2015-12-20 06:02:12 +00:00
- org integration with magit via [[https://github.com/magit/orgit ][orgit ]]
2015-06-10 16:44:30 +00:00
2016-11-13 02:23:35 +00:00
New to Magit? Checkout the [[https://magit.vc/about/ ][official intro ]].
2015-06-10 16:44:30 +00:00
* Install
** Layer
2016-01-06 05:21:55 +00:00
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.
2015-06-10 16:44:30 +00:00
2021-03-25 05:17:42 +00:00
** 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 [[http://git-scm.com/downloads ][download page ]].
2015-06-10 16:44:30 +00:00
** Magit status fullscreen
To display the =magit status= buffer in fullscreen set the variable
2015-09-28 06:05:18 +00:00
=git-magit-status-fullscreen= to =t= in your =dotspacemacs/user-init= function.
2015-06-10 16:44:30 +00:00
#+BEGIN_SRC emacs-lisp
2015-09-28 06:05:18 +00:00
(defun dotspacemacs/user-init ()
2016-07-05 03:00:11 +00:00
(setq-default git-magit-status-fullscreen t))
2015-06-10 16:44:30 +00:00
#+END_SRC
** Magit auto-complete
2019-01-12 01:22:52 +00:00
Magit auto-complete feature is enabled by default.
For this feature to work best - setup [[#repository-list ][magit repository list ]].
2015-06-10 16:44:30 +00:00
2021-03-25 05:17:42 +00:00
** Magit Plugins
*** magit-delta
[[https://github.com/dandavison/magit-delta ][magit-delta ]] uses [[https://github.com/dandavison/delta ][delta ]] to display diffs, with extensive changes to its
layout and styles.
You need to [[https://github.com/dandavison/delta#installation ][install delta ]] first, and add the following to your =dotspacemacs/user-config= :
2021-04-09 11:56:19 +00:00
2021-03-25 05:17:42 +00:00
#+BEGIN_SRC emacs-lisp
2021-04-09 11:56:19 +00:00
(setq-default dotspacemacs-configuration-layers
'((git :variables git-enable-magit-delta-plugin t)))
2021-03-25 05:17:42 +00:00
#+END_SRC
*** magit-gitflow
[[https://github.com/petervanderdoes/gitflow-avh ][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 [[https://github.com/petervanderdoes/gitflow/wiki ][installing ]] =git-flow= , add the following to your =dotspacemacs/user-config= :
2021-04-09 11:56:19 +00:00
2021-03-25 05:17:42 +00:00
#+BEGIN_SRC emacs-lisp
2021-04-09 11:56:19 +00:00
(setq-default dotspacemacs-configuration-layers
'((git :variables git-enable-magit-gitflow-plugin t)))
2021-03-25 05:17:42 +00:00
#+END_SRC
*** magit-svn
[[https://github.com/emacsorphanage/magit-svn ][magit-svn ]] shows commits which were not pushed to svn yet.
2018-05-27 06:48:22 +00:00
Press ~!~ in *Magit* buffer to open the magit-svn-popup.
There you can push to or rebase from svn.
2018-05-24 16:21:40 +00:00
2021-03-25 05:17:42 +00:00
To enable =magit-svn= plugin, add the following to your =dotspacemacs/user-config= :
2021-04-09 11:56:19 +00:00
2018-05-24 16:21:40 +00:00
#+BEGIN_SRC emacs-lisp
2021-04-09 11:56:19 +00:00
(setq-default dotspacemacs-configuration-layers
'((git :variables git-enable-magit-svn-plugin t)))
2018-05-24 16:21:40 +00:00
#+END_SRC
2015-09-06 06:49:28 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2018-05-27 06:48:22 +00:00
|-------------+----------------------|
| ~~~ | open magit-svn-popup |
2021-03-25 05:17:42 +00:00
*** magit-todos
[[https://github.com/alphapapa/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= :
2021-04-09 11:56:19 +00:00
2021-03-25 05:17:42 +00:00
#+BEGIN_SRC emacs-lisp
2021-04-09 11:56:19 +00:00
(setq-default dotspacemacs-configuration-layers
'((git :variables git-enable-magit-todos-plugin t)))
2021-03-25 05:17:42 +00:00
#+END_SRC
2018-05-27 06:48:22 +00:00
** Global git commit mode
Spacemacs can be used as the =$EDITOR= (or =$GIT_EDITOR= ) for editing git
commits messages. To enable this you have to add the following line to your
=dotspacemacs/user-config= :
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC emacs-lisp
(global-git-commit-mode t)
#+END_SRC
2018-05-27 06:48:22 +00:00
2020-11-14 16:53:33 +00:00
** Forge
The =forge= package uses =emacsql= which requires a C compiler to be available
on MS Windows, see issue [[https://github.com/skeeto/emacsql/issues/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:
#+BEGIN_SRC emacs-lisp
(dotspacemacs-additional-packages '((forge :toggle t)))
#+END_SRC
2018-05-27 06:48:22 +00:00
** Org integration
See the commentary section of the package [[https://github.com/magit/orgit/blob/master/orgit.el#L28 ][here ]].
2015-12-20 06:02:12 +00:00
2015-06-10 16:44:30 +00:00
* Working with Git
2015-06-16 02:11:11 +00:00
Git commands (start with ~g~ ):
2015-06-10 16:44:30 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2015-06-10 16:44:30 +00:00
|-------------+-----------------------------------------------------|
2018-03-27 01:19:04 +00:00
| ~SPC g /~ | open =helm-git-grep= |
| ~SPC g *~ | open =helm-git-grep-at-point= |
2015-06-10 16:44:30 +00:00
| ~SPC g b~ | open a =magit= blame |
2017-05-16 13:11:18 +00:00
| ~SPC g f f~ | view a file at a specific branch or commit |
2018-01-29 18:59:20 +00:00
| ~SPC g f l~ | commits log for current file |
2019-02-15 17:31:01 +00:00
| ~SPC g f d~ | diff for current file |
2021-02-28 05:15:00 +00:00
| ~SPC g f m~ | magit dispatch popup for file operations |
2015-11-02 05:16:13 +00:00
| ~SPC g H c~ | clear highlights |
| ~SPC g H h~ | highlight regions by age of commits |
| ~SPC g H t~ | highlight regions by last updated time |
2017-05-16 19:40:33 +00:00
| ~SPC g i~ | initialize a new git repository |
2015-06-10 16:44:30 +00:00
| ~SPC g I~ | open =helm-gitignore= |
2016-09-12 14:54:26 +00:00
| ~SPC g L~ | open magit-repolist |
2015-06-10 16:44:30 +00:00
| ~SPC g s~ | open a =magit= status window |
2015-12-01 06:43:29 +00:00
| ~SPC g S~ | stage current file |
2016-07-05 02:35:05 +00:00
| ~SPC g m~ | magit dispatch popup |
| ~SPC g M~ | display the last commit message of the current line |
2015-06-10 16:44:30 +00:00
| ~SPC g t~ | launch the git time machine |
2015-12-01 06:43:29 +00:00
| ~SPC g U~ | unstage current file |
2015-06-10 16:44:30 +00:00
2018-09-19 03:54:47 +00:00
Notes:
2015-06-10 16:44:30 +00:00
- Highlight by age of commit or last update time is provided by
2018-09-19 03:54:47 +00:00
[[https://github.com/syohex/emacs-smeargle ][smeargle ]].
2018-07-19 23:43:51 +00:00
- Git time machine is provided by [[https://melpa.org/#/git-timemachine ][git-timemachine ]].
2015-06-10 16:44:30 +00:00
- Git last commit message per line is provided by [[https://github.com/syohex/emacs-git-messenger ][git-messenger ]].
** Magit
2015-10-12 23:12:09 +00:00
Spacemacs uses [[http://magit.vc/ ][magit ]] to manage Git repositories.
2015-06-10 16:44:30 +00:00
2016-07-05 02:35:05 +00:00
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~ .
2015-06-10 16:44:30 +00:00
2020-11-14 16:53:33 +00:00
Spacemacs uses [[https://github.com/magit/forge/ ][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 [[https://magit.vc/manual/forge/Getting-Started.html ][Getting Started page ]].
2021-02-14 18:32:03 +00:00
Spacemacs uses [[https://github.com/emacs-evil/evil-collection/tree/master/modes/magit ][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.
2015-10-29 16:16:40 +00:00
Here are the often used bindings inside a =status buffer= :
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2018-10-23 14:18:21 +00:00
|-------------+--------------------------------------------------------------------|
| ~/~ | evil-search |
| ~$~ | open =command output buffer= |
| ~c c~ | open a =commit message buffer= |
| ~b b~ | checkout a branch |
| ~b c~ | create a branch |
| ~f f~ | fetch changes |
2019-02-16 14:53:34 +00:00
| ~F (r) u~ | pull tracked branch and rebase |
2018-10-23 14:18:21 +00:00
| ~gr~ | refresh |
| ~j~ | goto next magit section |
| ~C-j~ | next visual line |
| ~k~ | goto previous magit section |
| ~C-k~ | previous visual line |
| ~l l~ | open =log buffer= |
| ~n~ | next search occurrence |
| ~N~ | previous search occurrence |
| ~o~ | revert item at point |
| ~P u~ | push to tracked branch |
| ~P m~ | push to matching branch (e.g., upstream/develop to origin/develop) |
| ~q~ | quit |
| ~s~ | on a file or hunk in a diff: stage the file or hunk |
| ~x~ | discard changes |
| ~+~ | on a hunk: increase hunk size |
| ~=~ | on a hunk: decrease hunk size |
| ~S~ | stage all |
| ~TAB~ | on a file: expand/collapse diff |
| ~u~ | on a staged file: unstage |
| ~U~ | unstage all staged files |
| ~v or V~ | select multiple lines |
| ~z z~ | stash changes |
2015-06-10 16:44:30 +00:00
2015-09-30 09:43:42 +00:00
** 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).
2015-06-10 16:44:30 +00:00
2015-10-29 16:16:40 +00:00
** Commit message editing buffer
2019-01-18 11:30:06 +00:00
In a commit message buffer the following key bindings are active:
| Key binding | Description |
|------------------------+-----------------------------------------------------------|
| ~SPC m c~ or ~SPC m ,~ | commit changes with the entered message |
| ~SPC m a~ or ~SPC m k~ | discard message and abort the commit |
| ~g j~ or ~M-n~ | cycle through history to the previous commit message |
| ~g k~ or ~M-p~ | save 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 binding | Description |
|------------------------+---------------------------------------------|
| ~SPC m c~ or ~SPC m ,~ | select the commit at point and act on it |
2019-05-26 20:58:52 +00:00
| ~SPC m a~ or ~SPC m k~ | abort selecting and don't act on any commit |
2015-06-10 16:44:30 +00:00
** Interactive rebase buffer
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2015-06-10 16:44:30 +00:00
|-------------+----------------|
2015-10-30 11:20:58 +00:00
| ~c~ or ~p~ | pick |
| ~e~ | edit |
| ~f~ | fixup |
| ~j~ | go down |
2016-08-19 08:10:09 +00:00
| ~M-j~ | move line down |
2015-10-30 11:20:58 +00:00
| ~k~ | go up |
2016-08-19 08:10:09 +00:00
| ~M-k~ | move line up |
2015-10-30 11:20:58 +00:00
| ~d~ or ~x~ | kill line |
| ~r~ | reword |
| ~s~ | squash |
| ~u~ | undo |
| ~y~ | insert |
| ~!~ | execute |
2015-06-10 16:44:30 +00:00
** Quick guide for recurring use cases in Magit
- Amend a commit:
2015-10-29 16:16:40 +00:00
- ~l l~ to open =log buffer=
2015-06-12 01:23:07 +00:00
- ~c a~ on the commit you want to amend
2016-11-21 20:25:22 +00:00
- ~ , c~ or ~C-c C-c~ to submit the changes
2015-06-10 16:44:30 +00:00
- Squash last commit:
2015-10-29 16:16:40 +00:00
- ~l l~ to open =log buffer=
2015-07-02 17:56:02 +00:00
- ~r e~ on the second to last commit, it opens the =rebase buffer=
2015-06-12 01:23:07 +00:00
- ~j~ to put point on last commit
- ~s~ to squash it
2016-11-21 20:25:22 +00:00
- ~ , 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
2015-06-10 16:44:30 +00:00
- Force push a squashed commit:
2015-06-10 21:16:01 +00:00
- in the =status buffer= you should see the new commit unpushed and the old
commit unpulled
2015-06-12 01:23:07 +00:00
- ~P -f P~ for force a push (*beware* usually it is not recommended to rewrite
2015-06-10 21:16:01 +00:00
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).
2015-06-10 16:44:30 +00:00
- Add upstream remote (the parent repository you have forked):
2015-07-02 04:36:26 +00:00
- ~M~ to open the =remote popup=
2015-06-12 01:23:07 +00:00
- ~a~ to add a remote, type the name (i.e. =upstream= ) and the URL
2015-06-10 16:44:30 +00:00
- Pull changes from upstream (the parent repository you have forked) and push:
2015-07-03 02:08:03 +00:00
- ~F -r C-u F~ and choose =upstream= or the name you gave to it
2015-06-12 01:23:07 +00:00
- ~P P~ to push the commit to =origin=
2015-06-10 16:44:30 +00:00
2020-06-04 21:50:03 +00:00
** Git Blame Transient State
| Key binding | Description |
|-------------+----------------------------------------------------------|
| ~SPC g b~ | start magit-blame and open the git blame transient state |
| ~?~ | toggle hint |
| ~p~ | prev chunk |
| ~P~ | prev chunk same commit |
| ~n~ | next chunk |
| ~N~ | next chunk same commit |
| ~RET~ | show commit |
| ~b~ | show commits with adding lines |
| ~r~ | show commits with removing lines |
| ~f~ | show last commits that still have lines |
| ~e~ | show line revision info in echo area (not read only) |
| ~q~ | kill recursive blame buffer or disable magit-blame-mode |
| ~c~ | cycle style |
| ~Y~ | copy hash |
| ~B~ | magit-blame (magit transient) |
| ~Q~ | quit transient state |
2015-08-07 02:02:00 +00:00
** Git-Flow
[[https://github.com/jtatarik/magit-gitflow ][magit-gitflow ]] provides git-flow commands in its own magit menu.
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2015-08-07 02:02:00 +00:00
|-------------+-------------------------|
2015-10-24 03:29:58 +00:00
| ~%~ | open magit-gitflow menu |
2015-08-07 02:02:00 +00:00
2015-06-10 16:44:30 +00:00
** Git time machine
2018-07-19 23:43:51 +00:00
[[https://melpa.org/#/git-timemachine ][git-timemachine ]] allows to quickly browse the commits of the current buffer.
2015-06-10 16:44:30 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2016-01-30 11:32:53 +00:00
|-------------+----------------------------------------------------|
| ~SPC g t~ | start git timemachine and initiate transient-state |
| ~c~ | show current commit |
| ~n~ | show next commit |
| ~N~ | show previous commit |
| ~p~ | show previous commit |
| ~q~ | leave transient-state and git timemachine |
| ~Y~ | copy current commit hash |
2015-07-02 04:36:26 +00:00
2016-07-05 03:00:11 +00:00
** 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...
2019-10-01 14:04:30 +00:00
| Key binding | Description |
|-------------+-----------------------------------------------------------------------------------------------|
| ~SPC g l c~ | on a commit hash, browse to the current file at this commit |
| ~SPC g l C~ | on a commit hash, create link to the file at this commit and copy it |
| ~SPC g l l~ | on a region, browse to file at current lines position |
| ~SPC g l L~ | on a region, create a link to the file highlighting the selected lines |
| ~SPC g l p~ | on a region, browse to file at current lines position (using permalink link) |
| ~SPC g l P~ | on a region, create a link to the file highlighting the selected lines (using permalink link) |
2016-07-05 03:00:11 +00:00
*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= .
2016-09-12 14:54:26 +00:00
** Repository list
2019-01-12 01:22:52 +00:00
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.
2016-09-12 14:54:26 +00:00
#+BEGIN_SRC emacs-lisp
2019-01-20 15:34:11 +00:00
(setq magit-repository-directories
'(("~/Development/" . 2) ("~ /src/ " . 2)))
2016-09-12 14:54:26 +00:00
#+END_SRC
2019-01-12 01:22:52 +00:00
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.
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2016-09-12 14:54:26 +00:00
|-------------+-----------------------------------------------------|
| ~SPC g L~ | start git repo list |
| ~RET~ | show the git status window for the selected project |
| ~gr~ | refresh the project list |
2019-01-12 01:22:52 +00:00
For more information, look into [[http://magit.vc/manual/magit.html#Status-Buffer ][Magit-User-Manual#Status-Buffer ]]
2020-11-14 16:53:33 +00:00
** Forge
In a =magit-status= buffer (~SPC g s~ ):
| Key binding | Description |
|-------------+-----------------------------------------------------------|
| ~b Y~ | create branch from pull-request |
| ~b y~ | create and check out branch from pull-request |
| ~F f~ | fetch issues and pull-requests |
| ~F n~ | fetch notifications |
| ~F p~ | create pull-request |
| ~F i~ | create issue |
| ~F F~ | list notifications |
| ~F P~ | list pull-requests |
| ~F I~ | list issues |
| ~p y~ | pull pull-requests and issues for the current repository |
| ~p Y~ | pull all notifications for the current repository's forge |
In a =forge-topic= buffer:
| Key binding | Description |
|-------------+-----------------|
| ~SPC m c~ | create new post |
| ~SPC m e~ | edit post |
In a =forge-post= buffer (assuming the major mode leader key is ~,~ )
| Key binding | Description |
|------------------------+-------------|
| ~SPC m c~ or ~SPC m ,~ | submit post |
| ~SPC m k~ or ~SPC m k~ | cancel post |