Difference between revisions of "Magit"

From Coder Merlin
(2 intermediate revisions by the same user not shown)
Line 8: Line 8:
Magit provides an easy-to-user interface to git from within emacs.  
Magit provides an easy-to-user interface to git from within emacs.  
== Introduction ==
== Introduction ==
Magit is a package that runs within emacs and the aim of this package is to provide an interface for the version control system git. The official website of Magit says "Magit aspires to be a complete Git porcelain". So basically, git repository can be accessed well independently from the [https://cli.github.com CLI] directly without any mediation but most of the time such a practice becomes cumbersome. Magit does the magic here by providing the scope to the developer to use the git with ease. There are other tools available as well in the form of git extensions which are specific to different IDE or base interface but Magit is one of the most popular one and the only version control tool for git available 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. There are other tools available as well in the form of git extensions which 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 all the features of the three.  
To illustrate further, Magit works as a text based interface somewhere falling between CLI, IDE and GUI while combining the features of all three.  
[[File:Magit Illustration.png|frame|center]]
[[File:Magit Illustration.png|frame|center]]


=== Benefits of Magit ===
=== Benefits of Magit ===


While the reason why you should use Magit for managing your gits will always remain the comfortability and the ease of operations. To be specific, the major operations of git like staging and applying changes is where Magit is truly helpful and can save tons of your time. There are some commands like ''git add --patch'' for staging purpose which git itself provides, but due  to its difficulty, a lot of developers only use such commands in the case of exceptional scenario. Whereas staging, even in partial way is very easy to execute but all of these are provided by Magit without any involvement of mouse or GUI.  
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. There are some commands like ''git add --patch'' for staging purpose which git itself provides, but due  to 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 of these tools are provided by Magit without any involvement of a mouse or GUI.


== Getting Started with Magit ==  
== Getting Started with Magit ==  


To get started with Magit, obviously you need to clone one of your git repository to the codermerlin server which you wish to manage via Magit. There are various ways to do the same. the easiest way is just to execute the following command:  
To get started with Magit, you need to clone one of your git repositories to the codermerlin server which you wish to manage via Magit. There are various ways to do so, the easiest of which is to just execute the following command:  


git clone https://github.com/your_git_username/your_git-name.git
git clone https://github.com/your_git_username/your_git-name.git
Line 25: Line 25:


[[File:Snapshot 2.jpg|frame|center]]
[[File:Snapshot 2.jpg|frame|center]]
In this way, the already existing git will get cloned to the codermerlin server space.  
In this way, the already existing repository will get cloned to your codermerlin server space.  
In this example we are considering a basic example git, the which is available at [https://github.com/CoderMarik/github-slideshow/ here].  
In this example we are considering a basic example repository, which is available [https://github.com/TheCoderMerlin/Curses here].  


Once you clone the repository, just enter into emacs interface by first moving inside the repository and then typing ''emacs''.  
Once you clone the repository, just enter the emacs interface by moving inside the newly created directory and typing ''emacs''.  
[[File:Snapshot 3.jpg|frame|center]]
[[File:Snapshot 3.jpg|frame|center]]


Line 35: Line 35:
[[File:Snapshot 4.jpg|frame|center]]
[[File:Snapshot 4.jpg|frame|center]]


Once you are within the emacs editor, you can initiate Magit by the following command  
Once you are within the emacs editor, you can initiate Magit by using the following command  


''M-x magit-status''
''M-x magit-status''
Line 47: Line 47:
[[File:Snapshot 6.jpg|frame|center]]
[[File:Snapshot 6.jpg|frame|center]]


So, this should be most of the time what the basic Magit interface should look like. The mostly visible part of the Magit is the status buffer displaying all the required information about the present repository on which the Magit is running. The content of this buffer is generated by running several git commands and getting the concerned outputs actionable. 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 may make you feel confused but since each of the segment can be collapsed, they are actually not that confusing. This status buffer is the place where we should enter our commands in order to interact with our git.  
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 may make you feel confused but since each of the segment can be collapsed, they are actually not that confusing. The status buffer is where we should enter our commands in order 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 may be sections including  "Unpushed to origin/master", "Staged changes", "Unstaged changes" so on and so forth.  
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 may be sections including  "Unpushed to origin/master", "Staged changes", "Unstaged changes" so on and so forth.  
Line 57: Line 57:
=== Commits ===
=== Commits ===
   
   
Commits are done in Magit by a very easy way of getting the cursor on the required file and hitting ''c'' under the staus buffer section. This will give a list of options which may be termed as subcommands and from where a specific type of commit can be chosen.  
Commits are done in Magit by getting the cursor on the required file and hitting ''c'' under the status buffer section. This will give a list of options which may be termed as subcommands and from where a specific type of commit can be chosen.  


[[File:Snapshot 7.jpg|frame|center]]
[[File:Snapshot 7.jpg|frame|center]]
Line 71: Line 71:
  -C Reuse commit message (--reuse-message=)''                                                                         
  -C Reuse commit message (--reuse-message=)''                                                                         
                                              
                                              
As can be understandable, the concerned file may be staged and there is the option to stage all the files at one go with a single command. With the ''e'' subcommand, Magit will allow an empty commit. For each of the subcommands, the equivalent CLI commands are shown within a bracket just adjacent to them. Once done with your commit, you can simply press :{{SpecialKey|CONTROL|c}} {{Key|c}} twice to make the commit effective. While :{{SpecialKey|CONTROL|c}} {{Key|c}} and :{{SpecialKey|CONTROL|c}} {{Key|k}} makes the commit effective and aborts.  
As you can see, the concerned file(s) may be staged and there is an option to stage all the files with one go in a single command. With the ''e'' subcommand, Magit will allow an empty commit. For each of the subcommands, the equivalent CLI commands are shown within a bracket just adjacent to them. Once done with your commit, you can simply press :{{SpecialKey|CONTROL|c}} {{Key|c}} twice to make the commit effective. While :{{SpecialKey|CONTROL|c}} {{Key|c}} and :{{SpecialKey|CONTROL|c}} {{Key|k}} makes the commit effective and aborts.  


=== Pulling ===  
=== Pulling ===  
The primary function of pulling in git CLI happens to be ''git pull''. The equivalent option for Magit has several options with similar base command and these are:
As you may 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'':
''magit-pull'':
This basic command when invoked, provides a pop up with a list of the arguments which can be used as the subcommands and to specify the nature of the pull request.  
This basic command provides a pop up with a list of arguments which can be used as subcommands to specify the nature of the pull request.  
[[File:Snapshot 8.0.jpg|frame|center]]
[[File:Snapshot 8.0.jpg|frame|center]]
[[File:Snapshot 8.jpg|frame|center]]
[[File:Snapshot 8.jpg|frame|center]]


''magit-pull-from-pushremote'':
''magit-pull-from-pushremote'':
This command provides the option to pull from the push-remote of the current branch. But for some reasons, if the 'push-remote' is not configured or available, it is advisable to configure the same before using this command. The execution will ask for the destination object's identifier.  
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 to configure one before using this command. The execution will ask for the destination object's identifier.  


[[File:Snapshot 9.jpg|frame|center]]
[[File:Snapshot 9.jpg|frame|center]]
Line 88: Line 88:
''magit-pull-from-upstream'':
''magit-pull-from-upstream'':


This command provides the option to pull from the upstream of the current branch. But for some reasons, if the 'upstream' is not configured or available, it is advisable to configure the same before using this command. The execution will ask 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 to configure it before using this command. The execution will ask for the destination object's identifier.  
[[File:Snapshot 10.jpg|frame|center]]
[[File:Snapshot 10.jpg|frame|center]]
[[File:Snapshot 11.jpg|frame|center]]
[[File:Snapshot 11.jpg|frame|center]]
Line 99: Line 99:


=== Pushing ===
=== Pushing ===
The primary function of pushing in git CLI happens to be ''git push''. The equivalent option for Magit has several options with similar base command and these are:
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:


''magit-push'':
''magit-push'':


This basic command for pushing when invoked, provides a pop up with a list of the arguments which can be used as the subcommands and to specify the nature of the push request.  
This will execute the basic command for pushing when invoked, and provides a pop up with a list of the arguments which can be used as subcommands to specify the nature of the push request.  


[[File:Snapshot 14.jpg|frame|center]]
[[File:Snapshot 14.jpg|frame|center]]
Line 110: Line 110:
''magit-push-current-to-pushremote'':
''magit-push-current-to-pushremote'':


This command provides the option to push to the pushremote of the current branch. But for some reasons, if the 'pushremote' is not configured or available, it is advisable to configure the same before using this command. The execution will ask for the destination object's identifier.
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 to configure one before using this command. The execution will ask for the destination object's identifier.


[[File:Snapshot 16.jpg|frame|center]]
[[File:Snapshot 16.jpg|frame|center]]
Line 117: Line 117:
''magit-push-current-to-upstream'':
''magit-push-current-to-upstream'':


This command provides the option to push to the upstream of the current branch. But for some reasons, if the 'upstream' is not configured or available, it is advisable to configure the same before using this command. The execution will ask 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 to configure one before using this command. The execution will ask for the destination object's identifier.


[[File:Snapshot 18.jpg|frame|center]]
[[File:Snapshot 18.jpg|frame|center]]
Line 124: Line 124:
''magit-push-current'':
''magit-push-current'':


This command provides the option to push branch read in the minibuffer. The execution will ask for the destination object's identifier.
This command provides the option to push to the branch read in the minibuffer. The execution will ask for the destination object's identifier.
[[File:Snapshot 20.jpg|frame|center]]
[[File:Snapshot 20.jpg|frame|center]]
[[File:Snapshot 21.jpg|frame|center]]
[[File:Snapshot 21.jpg|frame|center]]

Revision as of 19:56, 29 December 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Magit

Prerequisites[edit]

Background[edit]

Magit provides an easy-to-user interface to git from within emacs.

Introduction[edit]

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. There are other tools available as well in the form of git extensions which 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.

Magit Illustration.png

Benefits of Magit[edit]

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. There are some commands like git add --patch for staging purpose which git itself provides, but due to 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 of these tools are provided by Magit without any involvement of a mouse or GUI.

Getting Started with Magit[edit]

To get started with Magit, you need to clone one of your git repositories to the codermerlin server which you wish to manage via Magit. There are various ways to do so, the easiest of which is to just execute the following command:

git clone https://github.com/your_git_username/your_git-name.git

Cloning
Snapshot 2.jpg

In this way, the already existing repository will get 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, just enter the emacs interface by moving inside the newly created directory and typing emacs.

Snapshot 3.jpg

You should see something similar to this:

Snapshot 4.jpg

Once you are within the emacs editor, you can initiate Magit by using the following command

M-x magit-status

Snapshot 5.jpg
Hint.pngHelpful Hint

The default Merlin Server configuration includes CONTROL-x g as a keyboard shortcut for magit-status

Snapshot 6.jpg

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 may make you feel confused but since each of the segment can be collapsed, they are actually not that confusing. The status buffer is where we should enter our commands in order 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 may be sections including "Unpushed to origin/master", "Staged changes", "Unstaged changes" so on and so forth.

Hint.pngHelpful Hint

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 hitting the tab button.

Commits[edit]

Commits are done in Magit by getting the cursor on the required file and hitting c under the status buffer section. This will give a list of options which may be termed as subcommands and from where a specific type of commit can be chosen.

Snapshot 7.jpg

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 file(s) may be staged and there is an option to stage all the files with one go in a single command. With the e subcommand, Magit will allow an empty commit. For each of the subcommands, the equivalent CLI commands are shown within a bracket just adjacent to them. Once done with your commit, you can simply press :CONTROL-c c twice to make the commit effective. While :CONTROL-c c and :CONTROL-c k makes the commit effective and aborts.

Pulling[edit]

As you may 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 which can be used as subcommands to specify the nature of the pull request.

Snapshot 8.0.jpg
Snapshot 8.jpg

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 to configure one before using this command. The execution will ask for the destination object's identifier.

Snapshot 9.jpg

magit-pull-from-upstream:

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 to configure it before using this command. The execution will ask for the destination object's identifier.

Snapshot 10.jpg
Snapshot 11.jpg

magit-pull-branch: This command provides the option to pull from branch read in the minibuffer. The execution will ask for the destination object's identifier.

Snapshot 12.jpg
Snapshot 13.jpg

Pushing[edit]

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:

magit-push:

This will execute the basic command for pushing when invoked, and provides a pop up with a list of the arguments which can be used as subcommands to specify the nature of the push request.

Snapshot 14.jpg
Snapshot 15.jpg

magit-push-current-to-pushremote:

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 to configure one before using this command. The execution will ask for the destination object's identifier.

Snapshot 16.jpg
Snapshot 17.jpg

magit-push-current-to-upstream:

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 to configure one before using this command. The execution will ask for the destination object's identifier.

Snapshot 18.jpg
Snapshot 19.jpg

magit-push-current:

This command provides the option to push to the branch read in the minibuffer. The execution will ask for the destination object's identifier.

Snapshot 20.jpg
Snapshot 21.jpg

Useful Commands[edit]

Show Logs
CONTROL-x g l l
Show Logs (particular file)
CONTROL-x l -- filename l
Show Tags
CONTROL-x g y

Key Concepts[edit]

References[edit]