Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DEV Community

Pauline Vos
Pauline Vos

Posted on

Git Legit cheat sheet

This is a repost of my 2018 Git cheat sheet, due to getting rid of my personal blog :) Enjoy

I’ve given my talk about best practices in Git a few times now, and I’ve had several people come up to me asking if I’m going to upload the slides, or “what was that one Git command again?” I thought it might be a good idea to offer a cheat sheet based on the talk to refer them to in the future. Maybe throw in some handy resources.

So here you go 🙂 There’s a lot more options to Git than this, but this is what I use daily to easily keep my commits atomic and my sanity in tact.

Here’s a quick recap of the talk. It explains atomic commits and why they’re a Good Thing™. You’ll need them for the below to help you

Add changes to your last commit

Stage your changes with git add ., then amend them to your last commit with git commit --am. Edit your commit message and/or description if desired, and write the file.

Change several past commits

Go into interactive rebase with git rebase -i HEAD~5 where 5 is the number of commits you want to go back. Change pick into e for edit on any commit you want to change. Write the file. Make the changes you want to do on that commit. Continue to the next commit with git rebase --continue. It will first offer you to change the commit message and/or description, similar to when you git commit --am.

Stash your changes

You can stash away your changes with git stash and retrieve them later with git stash pop. Very handy if you’ve made some changes that you want to add to a commit further back than your latest one.

Drop a commit

Enter interactive rebase. Simply delete the line that shows the commit you want to drop. Write the file. Done.

Change the order of commits

Enter interactive rebase. Cut the line of a commit you want to move. Paste it anywhere in the list to change the order, and write the file.

Reset a commit

Resetting a commit will remove the commit and make all its changes reappear in your working directory (soft reset). Especially handy when you’ve made a quick checkpoint commit and want to organize all the changes into relevant commits. Use git reset HEAD~ to reset your latest commit. Add a number to the end to reset that many of your latest commits.

Adding the --hard option will remove the changes entirely, as if they never happened.

Take a commit from another branch

git log [branch] where branch is the name of the branch that has the commit. Find the commit you want. Copy the commit hash. Move back to your own branch. Cherry-pick the commit with git cherry-pick 887ab710ac81a5d8cf6986305e9303ae5771b602 (replace with the commit hash).

Stage a hunk of changes

Instead of staging all the changes in a file, you can stage hunks of changes separately so as to add them to their respective atomic commits. Do this with git add --patch some-file.php. Git will stop at each hunk and ask you if you want to stage it (y, n).

Some other options are: –d: Don’t add this and any remaining hunks. You’re finished. –s: Split the hunk into smaller hunks.
–e: Manually edit the hunk. Keep in mind you're editing _which changes you want to stage`. You are not affecting the actual change.

Find the commit that broke something

You’re a bunch of commits along and notice something’s broken along the way. You can either check out every commit separately (takes a while), or use the binary search method (takes less time). Here’s how:

git bisect start
git bisect bad will mark your HEAD as the bad commit. Pass a commit hash if the bad commit it an earlier one.
git bisect good 887ab710ac81a5d8cf6986305e9303ae5771b602 (replace with hash of good commit)

Git will now check out a commit somewhere in the middle. Check if it’s broken (bad) or not (good) and mark it in the same way as the last two steps. Repeat this until there’s nothing more to check. The CLI output will give you the first bad commit

– Run git bisect reset to leave the bisect.

Top comments (0)