Skip to content

Instantly share code, notes, and snippets.

@leonardreidy
Last active December 14, 2017 10:25
Show Gist options
  • Save leonardreidy/5910b1027d6ad9b9d519a076241c9eee to your computer and use it in GitHub Desktop.
Save leonardreidy/5910b1027d6ad9b9d519a076241c9eee to your computer and use it in GitHub Desktop.
Git

Merge Conflicts

See code before either change (before change in both branches that now conflicts)

git diff --merge

Restoring

In Git there are many ways to revert/restore the codebase, chunk by chunk (verify), file by file, en masse, and so on. One way is simply to make the best of the checkout command! To illustrate, suppose you are working on a Rails project, and you are on branch development where you have put one of your existing serializers through a series of modifications. The word comes down that messing with the given serializer is causing problems for the other devs. You find a better way to do whatever you are trying to do and you want to restore the old serializer quickly and painlessly. QED! Simply checkout the file from the master branch without changing branch!

git checkout master -- <pathspec>

For example:

git checkout master -- app/serializers/settings_serializer.rb

Servers, Branching, and Tagging

Show urls of remote server

git remote -v

Give more details about each

git remote show name

Compare local and remote branches

git diff master..remoteName/master

See changes on HEAD that aren't on remote branch

git log remote/branch..

Create a lightweight tag

git tag nameOfTag

Create an annotated tag

git tag -a v1.1.0 # prompts for tag message

Create and checkout branch

git checkout -b branchName

Push a new local branch to remote

git push -u origin <branch>

Git will take care of the details!

Track a remote branch

git checkout -t <remote/branch>

e.g.:

git checkout -t origin/sso/main

Rename a branch

git branch -m newName oldName

Show branches merged/not merged into current branch

git branch --merged

vs

git branch --no-merged

Show state of all remote branches

git remote show origin

Merging Branches

The difference between merge and rebase is that merge tries to resolve conflicts, while rebase tries to take the changes since you last varied from the other branch and replay from the HEAD of the other branch

##WARNING##

Don't rebase after you've pushed a branch to a remote server - this can cause problems!

git checkout master

git merge featureSomeFeature

git rebase featureSomeFeature

Working with Branches

To take a remote branch and track it locally

git checkout -b someBranch origin/someBranch

or

git checkout -t origin/someBranch

Stashing

Temporarily stash work in progress

Stash your work

git stash

Stash your work with a custom message

git stash save "Some stash message

Pop stashed work off the stash stack

git stash pop

Logging

Log information, including reference logs.

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository. Reflogs are useful in various Git commands, to specify the old value of a reference. For example, HEAD@{2} means "where HEAD used to be two moves ago", master@{one.week.ago} means "where master used to point to one week ago in this local repository", and so on.

View a patch of what changed in each commit in the log

git log -p

View a summary of files changed

git log --stat

Search log by author

git log --author=Leonard

Search log by search term

git log --grap="Something interesting in the message"

Look for the commit that adds or removes a particular content

git log -S "TODO:"

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