Continuing from Part 1 where we installed magit and configured a github repository to be able to practice with it. Let’s get into how we can use magit to make common workflows seamless and intuitive.

Workflows

Repositories

fetch

  • To use fetch, type f, this will bring up the fetch popup: fetch-popup

  • Where you can then press p to fetch from the remote

pull

  • To pull into a repository, press F p

push

  • To push commits, use P p

Branching

  • To bring up the branch popup with all of the possible commands, press b, this will show as such: branch-popup

  • To checkout one of your own branches, use b

  • To create a new branch and checkout that branch at the same time, type c, you will be prompted to choose from where the new branch should be branched off of and once that is chosen, what the name for the new branch should be

  • To delete a branch, press k, where you will then be prompted to choose which branch you want to delete

  • To checkout a branch that is not your own, use l - this will create and check out a new local branch with the same name, and configure the selected remote branch as the push target

    • Do not use b as using this will cause the HEAD to become detached

Committing

  • To view changes, you can either use TAB to show the section under Unstaged changes or type RET to go to the file and see the changes unstaged-changes

  • It is also possible to view the Recent commits by pressing TAB on it and then pressing RET on what commit you want to view

  • To stage(s) or unstage(u), they both follow the same workflow:

    • To do it for all files, click the appropriate letter on Unstaged changes or Staged changes
    • For one file, you can press on the file itself
    • To act on one part of a file, can click the appropriate command on the hunk within the file: unstage-staged
  • To commit changes:

    • Press c c to go to the next buffer which shows the changes that will be commited and and provides a space for writing the commit message: commit-screen
  • From here you can write your command message

    • Once you’re ready you can commit it with C-c C-c or to go back and not commit it, press C-c C-k
  • Remember, as mentioned in part 1, nearly everything is actionable in magit, so whenever a section is shown, pressing TAB will hide or show it or using RET (return) will usually go to that section

Merge and Rebase

merge

  • To merge, click m to bring up the merge popup, there will be multiple options but to do a regular merge click m again

rebase

  • To initiate a rebase, press r from the magit status buffer, this will bring up the rebase popup: rebase-popup

  • There are 2 ways to rebase: 1: The first way is non-interactively, this is generally used for when you want to do a large, sweeping rebase, such as rebasing a whole branch onto main and scenarios such as that

  • To accomplish this, from the rebase screen, click u and then choose what branch you want to rebase onto

  • To push the branch to its pushRemote, press P -f p

    • We use --force with lease via the -f command instead of --force as it is the safer command and ensures that you will not overwrite someone else’s work by force pushing

2: The second way is interactively, this is used for more fine-tune control

  • From the the rebase screen, press i, this will bring up the commits in order of most to least recently committed rebase-screen

  • Go to whichever commit you want to rebase from and all the commits above it by pressing C-c C-c

  • From here you can reword commits, change the order, drop a commit, basically anything by going to that commit at point and pressing the corresponding command for it!

    • Take note that the commits are now in order of least to most recent interactive-rebase
  • There is a list of commands on the page for how to interact with the commits, so I will go through the main ones that I tend to use on a daily basis:

    • To reword - r
    • To fixup - f
      • fixup is squashing the commit into the previous commit but keeping only the previous commit’s log message
    • To move a commit up or down - M-p and M-n respectively
    • To complete the rebase, press C-c C-c
    • To abort the rebase, press C-c C-k
  • To push the branch to its pushRemote, press P -f p

Cherry Picking

  • Both methods start from the magit status buffer

  • There are generally two methods for cherry picking, depending on what the goal is:

1: Cherry Pick changes from another branch one by one, for more detailed control

  • From the branch that needs the cherry picked commits

  • Press l o to get a list of other branches

    • Use the up and down arrow keys to cycle through the branches
  • Select the branch you want to cherry pick from

  • Move to the commit you need and press A A

  • The status line will prompt you to choose which branch to cherry pick from

  • Press RET to apply changes

2: Cherry Pick all changes from another branch

  • From the branch that needs cherry picked commits

  • Press A to choose the cherry pick mode

  • Press a to only apply changes or press A again to apply and commit changes

  • Choose a branch to cherry pick changes from and press RET

Conclusion

There we have it, we’ve been able to easily go through many typical git workflows right from within Emacs. I hope this tutorial has been helpful in showing you how powerful and effortless using magit really is!