Magit in Spacemacs (evil-magit) notes
Magit and Org are two killer apps for Emacs. Here are my Magit notes using (the also excellent) Spacemacs (which uses evil-magit).
Contents¶
- Show git status
- Show help
- Show git log
- Show all commits for the current file
- Diff a range of commits
- Checkout a local branch
- Checkout a commit
- Checkout a different revision of a file
- Open a different revision of a file
- Create a local branch from a remote branch
- Pull from upstream
- Push to upstream
- Stage specific hunks
- Merge master into the current branch
- Rebase the current branch onto master
- Use interactive rebase to squash commits
- Use interactive rebase to reorder commits
- Revert a commit
- (Soft) reset the last commit
- Stash changes
- Pop stash
- Copy git commit SHA
- Copy text from a Magit buffer
- Run a shell command
- List all branches
- Jump to the next/prev section in the status view
- Create a worktree from an existing branch
- Switch to a worktree
- Delete a worktree
- References
Show git status¶
- SPC g s show Magit status view
Show help¶
- SPC g s show Magit status view
- ? get help
Show git log¶
- SPC g s show Magit status view
- l l show log view
Show all commits for the current file 1 ¶
- SPC g f l show git log for the current file
Diff a range of commits¶
- SPC g s show Magit status view
- l l show log view
- use j and k to position the cursor on a commit
- V to select the line
- use j and k to position the cursor on another commit
- d r to show a diff of the range of commits
Checkout a local branch¶
- SPC g s show Magit status view
- b b checkout a branch
- select or enter the branch name and hit ENTER
Checkout a commit¶
- SPC g s show Magit status view
- l l show log view
- use j and k to position the cursor on a commit
- b b ENTER to checkout that commit
Checkout a different revision of a file¶
- SPC g s show Magit status view
- l l show log view
- move point to the commit you want to checkout (using j and k)
- O (capital letter O) f reset a file
- hit ENTER to select the default revision selected above. (it will look something like
master~4
) - select a file
- q to close the log view and see the file at the selected revision is staged
Open a different revision of a file 8 ¶
- SPC g s show Magit status view
- l l show log view
- move point to the commit you want to checkout (using j and k)
- SPC g f F (
magit-find-file
) to open a file at a revision - ENTER to use the selected commit
- select the name of the file to open
Create a local branch from a remote branch¶
- SPC g s show Magit status view
- b c create a branch
- select or enter the remote branch and hit ENTER
- hit ENTER to use the same name or enter a new name and hit ENTER
Pull from upstream¶
- SPC g s show Magit status view
- F u pull from upstream
Push to upstream¶
- SPC g s show Magit status view
- P u push to upstream
Stage files and commit¶
- SPC g s show Magit status view
- use j and k to position the cursor on a file
- TAB to show and hide the diff for the file
- s to stage a file (u to unstage a file and x to discard changes to a file)
- c c to commit
- write a commit message and save with SPC f s
- , c to finish the commit message
Stage specific hunks 2 ¶
- SPC g s show Magit status view
- M-n / M-p to move to the "Unstaged changes" section
- j / k to move to the desired file
- TAB to expand the hunks in the file
- M-n / M-p to move to different hunks
- s / u to stage or unstange hunks
- x to discard a hunk
- c c to commit
- Enter a commit message and save with SPC f s
- , c to finish the commit
Merge master into the current branch¶
- SPC g s show Magit status view
- m m merge
- select or enter
master
and hit ENTER
Rebase the current branch onto master¶
- SPC g s show Magit status view
- r e rebase
- select or enter
master
and hit ENTER
Use interactive rebase to squash commits¶
- SPC g s show Magit status view
- l l show log view
- use j and k to position the cursor on a commit
- r i to start the interactive rebase
- use j and k to position the cursor on a commit to squash
- s to mark the commit as to be squashed. (use s multiple times to squash multiple commits.)
- , c to make it happen
- edit the new squashed commit message and save with SPC f s
- , c to finish
Use interactive rebase to reorder commits ¶
- SPC g s show Magit status view
- l l show log view
- use j and k to position the cursor on a commit
- ri to start the interactive rebase
- use j and k to position the cursor on a commit to reorder
- use M-k or M-j to move the commit up or down
- , c to make it happen
Revert a commit¶
- SPC g s show Magit status view
- l l show log view
- use j and k to position the cursor on the commit you want to revert
- _ O (capital letter O) to revert the commit
- edit the commit message and save with SPC f s
- , c to finish
(Soft) reset the last commit¶
- SPC g s show Magit status view
- l l show log view
- use j and k to position the cursor one commit before the last one
- O (capital letter O) s to soft reset
- the selected commit should be e.g.
master~1
. Hit ENTER
Stash changes¶
- SPC g s show Magit status view
- z z stash changes
- enter stash message and hit ENTER
Pop stash¶
- SPC g s show Magit status view
- z p pop from stash
- select the stash to pop and hit ENTER
Copy git commit SHA 3 ¶
- SPC g s show Magit status view
- l l show log view
- use j and k to position the cursor on a commit
- y s copy the git commit SHA
Copy text from a Magit buffer 4 ¶
- SPC g s show Magit status view
- \ switch to text mode
- copy text using normal vim keystrokes
- \ switch back to Magit mode
Run a shell command 5 ¶
- SPC g s show Magit status view
- ! s run a shell command
- enter a command to run and hit ENTER
List all branches 6 ¶
- SPC g s show Magit status view
- y r show refs
Jump to the next/prev section in the status view 7 ¶
- SPC g s show Magit status view
- g j jump to the next section
- g k jump to the previous section
Create a worktree from an existing branch¶
- SPC g s show Magit status view
- ? % b create a worktree from an existing branch
- select or enter the branch name and hit ENTER
- enter the path for the worktree and hit ENTER
Switch to a worktree¶
- SPC g s show Magit status view
- ? % g visit worktree
- select or enter the path of the worktree and hit ENTER
Delete a worktree¶
- SPC g s show Magit status view
- ? % k delete a worktree
- select or enter the path of the worktree and hit ENTER
References¶
- https://twitter.com/iLemming/status/1058507342830923776
- https://github.com/emacs-evil/evil-magit
- https://twitter.com/a_simpson/status/749316494224265216
- https://twitter.com/iLemming/status/986074309234802688
- https://twitter.com/_wilfredh/status/689955624080248833
- https://emacs.stackexchange.com/a/27148
- https://www.youtube.com/watch?v=j-k-lkilbEs
- https://emacs.stackexchange.com/questions/7655/how-can-i-open-a-specific-revision-of-a-file-with-magit/7683#7683
Related posts
- Switching from Emacs to Vim (actually Spacemacs) — posted 2015-12-31
- Colorized, interactive "git blame" in Emacs: vc-annotate — posted 2011-05-28
- My Emacs Python environment — posted 2010-05-10
- Emacs espresso-mode for jQuery — posted 2010-03-10
- Notes on C++ development with Emacs on Ubuntu Linux — posted 2009-07-08