Skip to content

Instantly share code, notes, and snippets.

@overdrive3000
Last active May 27, 2021 20:24
Show Gist options
  • Save overdrive3000/aba8604412369d3b55cf09ce1fb7306d to your computer and use it in GitHub Desktop.
Save overdrive3000/aba8604412369d3b55cf09ce1fb7306d to your computer and use it in GitHub Desktop.
Useful git commands

Search a string across commits

git log -S <string to find> --source --all

To find all commits that added or removed the fixed string string to find. The --all parameter means to start from every branch and --source means to show which of those branches led to finding that commit.

Find branches the commit is on

git branch --contains <commit>

Check if a merge will conflict befor applying it

git format-patch master --stdout | git-apply --check -

Take in count you should be located in the branch with the latest changes and compare format-path <branch_name> with the branch where you want to merge

Get git logs since a specific date

git log --since "01/07/2017"

Compare the repository between two dates

git diff master@{"yesterday"} master@{"2 weeks 2 days ago"}

Revert to an old version

git revert master@{"last Tuesday"}

Recover lost code

If you have commited or pushed your code anywhere, then in most cases is not lost. First, check the reflog git reflog. If the lost commit is there, retrieve it using its checksum:

git branch myBranch ca3df45

To search for orphan commits, use: git fsck --full

Checkout a specific directory

Since git 1.9 is possible to checkout specific files or directories using the sparse-checkout feature. Sparse checkout allows the user to choose what files to look at. It does the opposite of .gitignore, every file not specified in .git/info/sparse-checkout is ignored.

Let's suppose that you want to clone just the DevOps folder from the https://github.com/awslabs/aws-serverless-workshops.git repository

mkdir workspace && cd workspace # Create a folder to clone your project
git init # Initialize git
git remote add origin -f https://github.com/awslabs/aws-serverless-workshops.git # Add the remote git URL
git config core.sparsecheckout true # Enable the sparsecheckout feature
echo "DevOps/*" >> .git/info/sparse-checkout # Add the directories that you want to clone from
git pull origin master # Just the DevOps folder must be cloned from the remote repo

Squashing commits

Squashing means turning a lump of commits into a single commit. Suppose you’ve created two commits and submitted a PR against a repo you like, closing a bug you’ve filed a few hours earlier. The maintainer does some code review, asks you for a few changes, you commit some more. Now the PR has 5 commits, some of which undo work you did in the first few commits. Merging this PR as-is would add a ton of different commits for only one unit of work – that is, fixing a single bug. Squashing the commits together improves readability of the changelog, and linkability of commits when referring to an individual bug fix.

git reset HEAD~5
git add .
git commit -am "Here's the bug fix that closes #28"
git push --force

After running git reset, all your work will be unstaged, so you can then create another commit. This will rewrite history in the branch for your PR, but that’s okay because nobody depends on that until it gets merged. And when that happens, only one commit will be pulled by everyone else!

Show history of a function

The line log search tool -L shows the history of a function or line of code.

git log -L :myFunction:code.js

The above will show all the changes made to myFunction in the file code.js as a series of patches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment