Git Glossary

A collection of convenient git commands I’ve collected over the years. Github - using Git is a good resource to understand the commands provided in Git.

Commonly used commands

Action Description
commit Creating a commit to your local branch
push Pushing commits to a remote repository
fetch Getting changes made from a remote repository
merge Merges online changes into your local branch
pull Does fetch and merge
rebase Edit previous commit messages, combine multiple commits into one, delete or revert commits that are no longer necessary
checkout Checking out a branch (local or remote) to work on
diff Compares changes made locally to what’s online
cherry-pick choose a commit from one branch and apply it onto another.

Other useful commands

Action Description
whatchanged Shows a detailed history of what has changed
log Shows a brief history of what has changed
clone Cloning a repository

Getting changes

Git merge squash

This will take all the commits from the bugfix branch, squash them into 1 commit and then merge it with your master branch.

git checkout master
git merge --squash bugfix
git commit

GitHub Rebase

The below steps is something I’ve dug out of StackOverFlow. It’s good for when many people are working on a repository, but the better practice is maybe doing a pull request merge.

From Github’s website:

Warning: Because changing your commit history can make things difficult for everyone else using the repository, it’s considered bad practice to rebase commits when you’ve already pushed to a repository. To learn how to safely rebase on GitHub, see “About pull request merges.”

  1. git checkout -b develop
  2. …make some changes…
  3. …notice master has been updated…
  4. …commit changes to develop…
  5. git checkout master
  6. git pull
  7. …bring those changes back into develop…
  8. git checkout develop
  9. git rebase master
  10. …make some more changes…
  11. …commit them to develop…
  12. …merge them into master…
  13. git checkout master
  14. git pull
  15. git merge develop

Rebasing Interactively

Use git rebase -i <after-this-commit> and replace “pick” on the second and subsequent commits with “squash” or “fixup”, as described in the manual.

In this example, <after-this-commit> is either the SHA1 hash or the relative location from the HEAD of the current branch from which commits are analyzed for the rebase command. For example, if the user wishes to view 5 commits from the current HEAD in the past the command is git rebase -i HEAD~5.

Git show merge conflicts

git diff --name-only --diff-filter=U

Git diff between old file and new file, or just same file from different branches

git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt

You can also use relative paths:

git diff branch1:./relative/path/to/foo.txt branch2:./relative/path/to/foo-another.txt

Cherry-picking

Link to an article for a detailed explanation

From SOF:

  1. On the branch you want to apply the commit to: git checkout master
  2. git cherry-pick <commit-hash>

If you cherry-pick from a public branch, you should consider using:

git cherry-pick -x <commit-hash>

This will generate a standardized commit message. This way, you (and your co-workers) can still keep track of the origin of the commit and may avoid merge conflicts in the future.

If you have notes attached to the commit they do not follow the cherry-pick. To bring them over as well, you have to use:

git notes copy <from> <to>

Merging Forked Changes

Fork documentation from Github

  1. $ git checkout master
  2. $ git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAME
  3. $ git push origin master

Cleaning up

Outdated branches in local & remote

Git clean up

tl;dr:

$ git checkout master
$ git branch -r --merged
$ git branch -d old-merged-feature $ git branch --no-merged
$ git branch -r

Outdated references

$ git remote prune origin
$ git fetch -p
$ git branch --v | grep "\[gone\]" | awk '{print \$1}' | xargs git branch -D

Removing bad things

BFG is great for cleaning up if a commit that you want to get rid of (e.g. passwords, big files) has been pushed.

Otherwise, you can also hard reset and force update. This article is insightful about the dangers of doing something like that, as the repo could be easily destroyed. Act with caution and care. This works only if you were the last person to push to master. You cannot force push once other people have pushed, as it the commits they made will be gone forever.

Resources