Git: An Intermediate Guide (Part 1)
Learning to leverage Git can greatly improve the quality and efficiency of your contributions.
Join the DZone community and get the full member experience.Join For Free
Git plays a significant role in the daily workflow of many developers. Learning to leverage Git can greatly improve the quality and efficiency of your contributions. Here are a few commands to take your Git game to the next level.
You may also like: Top 20 Git Commands With Examples
git checkout -
We'll start with an easy but life-changing shortcut. The
git checkout - command switches you to the penultimate branch you checked-out. For example, if you're on the branch master and you
git checkout feature-branch, running this command would be the equivalent of
git checkout master. This command is helpful if you need to switch branches quickly, if your team has loquacious branch naming conventions, or if you just want to lowkey look like a Git ninja.
Here's an easy one that I use all the time. This command shows the history of your current branch. Use the down arrow to see more; enter
q to exit.
git log ./relative-path/file-name.js
Now the Git history of a file! This command will log the commits that have changed a file resulting in its current state. First, figure out the relative path from the root of your Git repo to the file you'd like to log. If you're using VSCode, open the file, right-click the file's tab, and click "Copy relative path." Make sure you replace the root directory of your repo with a period, like our example above. Then, run the command from the root directory of your git repo. This command is helpful when trying to understand when and why a file was changed.
git cherry-pick <commit hash>
Cherry-picking allows you to selectively copy a commit from one branch to another. First, check out the branch with the commit you're looking to copy.
git log to display the commit history, find your commit, and copy the commit hash — it should look like a considerably long string of numbers and letters (probably 40 characters). Next, check out the branch where you'd like to copy the commit. Run the command above with the commit hash to cherry-pick that commit to the new branch.
git log and you should see your commit! No need to stage or commit after the cherry-pick, Git knows this change is already including in the cherry-picked committed. If you run into any conflicts, you can resolve, save, stage, and run
git commit —this will display your commit in interactive mode. Don't panic — if your editor is the system default (Mac) just press ESC, enter
:x, and press Enter. If you've changed your editor to something other than the system default, I'm sure you know what to do. For more complex conflicts, or for help using the sequencer subcommands, reference the Git documentation for this command (https://git-scm.com/docs/git-cherry-pick). If at any point you've done something terribly wrong, enter
git cherry-pick --abort to exit this process and return to your previous Git state.
git add --patch
Our next command is a bit more technical but fairly straightforward. I've seen entire articles on this command alone, but we'll just discuss the basics. The
git add --patch command allows you to step through your local unstaged changes in "hunks" (which are adjacent unstaged changes). This gives you more control over what changes you stage (AKA
git add) and eventually commit. Running this command will return this slightly esoteric menu:
Stage this hunk [y,n,q,a,d,/,s,e,?]? ...so we'll get started by entering
enter) and break this down together.
Stage this hunk [y,n,q,a,d,/,s,e,?]? ? y - stage this hunk n - do not stage this hunk q - quit; do not stage this hunk or any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk or any of the later hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help
Before we get into it, if at any point you've done something terribly wrong, press CTRL + C (Mac) to exit this process and return to your previous Git state.
When you run this command, Git will show a code change followed by this menu. In the code change, a plus sign (+) means code was added, a minus sign (-) means code was removed. Hopefully, you recognized these changes. Now, the menu:
Starting from the top of the menu,
ywill stage the change that is displayed and advance you to your next "hunk" of a change. It will only stage the change that is displayed, nothing else.
nwill not stage the change displayed and will then advance you to your next "hunk". The code change that was displayed before entering
nwill remain an unstaged change in your code, it is not discarded! If you ran
git add --patchand entered
nfor every change, it would be the same as never running
git add --patchin the first place.
qwill eject you from this process while keeping the previous decisions you made. So if you entered
yon a few "hunks" and then
q, Git will stage the
dare similar, except they'll stage or not stage the remaining "hunks" (similar to entering
nfor the rest of the "hunks").
K— these commands navigate within the interactive mode that results from running
git add --patch. Unless you're comfortable with interactive mode, I would recommend using the other commands we're discussing to proceed through your changes; you can always run
git add --patchagain for unstaged changes, if need be.
swill split your "hunk" into smaller "hunks"; this is helpful if you'd like to stage some changes adjacent to other changes you'd like to leave unstaged.
eis to edit the "hunk"... if you're comfortable with the editor you have configured in your terminal, go for it. Otherwise, I recommend just hitting
non this change and editing the change as you normally would in your day-to-day text editor. Then run
git add --patchagain and enter
yto stage your updated "hunk".
?! If you ever need to print help for
git add --patchagain!
Hopefully, these commands will introduce some efficiency into your everyday workflow! If you know of any helpful Git commands, or you'd like to learn more about these or other Git commands, let us know in the comments below!
Go build something great!
[DZone Refcard] Getting Started With Git
Opinions expressed by DZone contributors are their own.