Magit provides an easy-to-user interface to git from within emacs.
Magit is a package that runs within emacs and aims to provide an interface for the version control system git. The official website of Magit says "Magit aspires to be a complete Git porcelain." A git repository can be accessed independently from the command line directly, without any mediation, but most of the time doing so can become cumbersome. Magit does the magic here by providing the option to use git with ease. Other tools are available as well in the form of git extensions that are specific to different IDEs or base interfaces, but Magit is one of the most popular ones and the only version control tool for git available within emacs. To illustrate further, Magit works as a text-based interface somewhere falling between CLI, IDE, and GUI while combining the features of all three.
Benefits of Magit
The biggest reason to use Magit for managing your git repositories is the comfort and the ease of operations it provides. More specifically, the major operations of git, like staging and applying changes, is where Magit is most helpful and can save you tons of time. Some commands like git add --patch exist for staging purpose, which git itself provides, but because of its difficulty, a lot of developers only use such commands as a last resort. On the other hand, staging is much easier to do with the git CLI, but all these tools are provided by Magit without any involvement of a mouse or GUI.
Getting Started with Magit
To get started with Magit, you need to clone one of your git repositories to the codermerlin server that you wish to manage via Magit. You can use various ways to do so, but the easiest way is to execute the following command:
In this way, the already existing repository gets cloned to your codermerlin server space. In this example, we are considering a basic example repository, which is available here.
Once you clone the repository, enter the emacs interface by moving inside the newly created directory and typing emacs.
You should see something similar to this:
Once you are within the emacs editor, you can initiate Magit by using the following command:
The default Merlin Server configuration includes CONTROL-x g as a keyboard shortcut for magit-status
This is what the basic Magit interface looks like. The most visible part of the Magit is the status buffer, which displays all the required information about the present repository in which Magit is running. The content of this buffer is generated by running several git commands and getting the concerned outputs. It also shows the current branch along with the associated list of unpulled and unpushed changes. This also has a section displaying the staged and unstaged changes. The organization of the interface might make you feel confused, but because each segment can be collapsed, they are actually not that confusing. The status buffer is where we should enter our commands to interact with our git repository.
In our examples, there are segments like "untracked files" and "Unmerged into origin/master" which is not what you see all the time. The segments visible under a repository depends on what stage of the development you are on and accordingly there could be sections including "Unpushed to origin/master," "Staged changes," "Unstaged changes," and the like.
The movements between the different segments are done with p and n while you can collapse or undo the collapse for a certain segment by taking the cursor to that segment and pressing the tab button.
Commits are done in Magit by getting the cursor on the required file and pressing c under the status buffer section. This gives a list of options that can be termed as subcommands and from where a specific type of commit can be chosen.
The list reads like: -a Stage all modified and deleted files (--all)
-e Allow empty commit (--allow-empty) -v Show diff of changes to be committed (--verbose) -n Disable hooks (--no-verify) -R Claim authorship and reset author date (--reset-author) -A Override the author (--author=) -s Add Signed-off-by line (--signoff) -C Reuse commit message (--reuse-message=)
As you can see, the concerned files can be staged, and there is an option to stage all the files with one go in a single command. With the e subcommand, Magit allows an empty commit. For each of the subcommands, the equivalent CLI commands are shown within a bracket just next to them. Once finished with your commit, you can press :CONTROL-c c twice to make the commit effective. While :CONTROL-c c and :CONTROL-c k makes the commit effective and aborts.
As you might already know, to pull using the git CLI, the command is git pull. The equivalent option for Magit has several options with similar base commands and are as follows:
magit-pull: This basic command provides a pop-up with a list of arguments that can be used as subcommands to specify the nature of the pull request.
magit-pull-from-pushremote: This command provides the option to pull from the push-remote of the current branch. But if, for some reason, the 'push-remote' is not configured or available, it is recommended that you configure one before using this command. The execution asks for the destination object's identifier.
This command provides the option to pull from the upstream of the current branch. If, for some reason, the 'upstream' is not configured or available, it is recommended that you configure it before using this command. The execution asks for the destination object's identifier.
magit-pull-branch: This command provides the option to pull from branch read in the minibuffer. The execution asks for the destination object's identifier.
Similar to the command for pulling, the command to push changes from the git CLI is simply git push. The equivalent option for Magit has several options with similar base command and these are:
This executes the basic command for pushing when invoked, and it provides a pop-up with a list of the arguments that can be used as subcommands to specify the nature of the push request.
This command provides the option to push to the pushremote of the current branch. If the 'pushremote' is not configured or available, it is recommended that you configure one before using this command. The execution asks for the destination object's identifier.
This command provides the option to push to the upstream of the current branch. If the 'pushremote' is not configured or available, it is recommended that you configure one before using this command. The execution asks for the destination object's identifier.
This command provides the option to push to the branch read in the minibuffer. The execution asks for the destination object's identifier.
- Show Logs
- CONTROL-x g l l
- Show Logs (particular file)
- CONTROL-x l -- filename l
- Show Tags
- CONTROL-x g y
|Next experience ID
|Knowledge and skills
|Source control systems
|2 hours120 minutes <br />
|understand the purpose of a source control system when working with multiple users
|ability to push and pull changes to/from the source control system
ability to use a source control system to merge changes
ability to properly handle merge conflicts