Skip to content

Instantly share code, notes, and snippets.

@webbj74
Created September 26, 2012 14:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save webbj74/3788509 to your computer and use it in GitHub Desktop.
Save webbj74/3788509 to your computer and use it in GitHub Desktop.
Generic Git Cheatsheet

Git Cheatsheet

Add an upstream remote repo

git remote add <name> <url>
git remote add upstream git@github.com:central-github-repo-username/sitename.git

Pull down, but don't merge, all changes from remotes

git fetch --all

Create a local topic branch

git branch 123-my-topic-branch

Switch to an existing branch

git checkout 123-my-topic-branch

Create a branch and switch in one command

git checkout -b 123-my-topic-branch

List all branches in the current repository

git branch -a

Push a topic branch to your remote repo (origin)

# From the topic branch
git push origin 123-my-topic-branch

Delete a local topic branch

git branch -d 123-my-topic-branch

Delete a remote topic branch

git push origin :123-my-topic-branch

Integrate upstream changes into a topic branch

# From the topic branch
git pull upstream master

Integrate upstream changes into master, and push to origin

git checkout master
git pull upstream master
git push origin master

Pull in submodules

git submodule update --init

Create new tag (v0.1) and push to origin

# Make sure you are on origin/master - this tags the HEAD of the current branch
git tag -a v0.1 -m 'Tagging up a new release for our deploy.'
# If you wish to tag a previous revision based on an SHA
git tag -a v0.1 -m 'Tagging up the last known-good changeset for our deploy.' 58f9229d0a536d8fc69d7491c7f53787353167e3
git push --tags upstream

Apply a hotfix from to a tag without disturbing master

git checkout v0.9.16
git checkout -b b-v0.9.16
# Fix stuff
git tag v0.9.17

Delete a local tag

git tag -d v1.1.1

Delete a remote tag

git push origin :refs/tags/v1.1.1

Cherry-picking

For a great overview of git cherry-pick, see /library/extracting_your_commits

Single Task Workflow

1. Sync with upstream

Integrate upstream changes into your local master branch

git checkout master
git pull upstream master

Push upstream changes to your fork (if necessary)

git push origin master

2. Prep for issue #123

Create a topic branch for issue #123

git checkout -b 123-blog-feature

3. Work on Issue #123

Do some work and commit it

git add path/to/my/file.php
git commit -m "#123: Added blog content type"

Rinse and repeat.

4. Integrate Issue #123 upstream

Integrate any upstream changes.

git pull upstream master

Push the topic branch to your remote

git push origin 123-blog-feature

Issue a pull request for this branch on GitHub

5. Branch cleanup (after issue has been integrated)

Integrate upstream changes into your local master branch

git checkout master
git pull upstream master

Push upstream changes to your fork (if necessary)

git push origin master

Delete your local topic branch

git branch -d 123-blog-feature

Delete your remote topic branch

git push origin :123-blog-feature

Deployment and integration testing tips

Setup multiple remotes

Add a git remote for all the various members of the project, as well as a remote for the master Acquia repository. Here's an example of what running git remote -v should return for a project with several partners involved.

$ git remote -v
acquia	acquia-username@svn-777.prod.hosting.acquia.com:acquia-sitename.git (fetch)
acquia	acquia-username@svn-777.prod.hosting.acquia.com:acquia-sitename.git (push)
partner1	git@github.com:partner1-github-username/partner1.git (push)
partner1	git@github.com:partner1-github-username/partner1.git (fetch)
origin	git@github.com:my-github-username/my-sitename-fork.git (fetch)
origin	git@github.com:my-github-username/my-sitename-fork.git (push)
partner2	git@github.com:partner2-github-username/partner2-sitename-fork.git (fetch)
partner2	git@github.com:partner2-github-username/partner2-sitename-fork.git (push)
partner3	git@github.com:partner3-github-username/partner3-sitename-fork.git (fetch)
partner3	git@github.com:partner3-github-username/partner3-sitename-fork.git (push)
upstream	git@github.com:central-github-repo-username/sitename.git (push)
upstream	git@github.com:central-github-repo-username/sitename.git (fetch)

Notice that origin is my personal fork of the repository, upstream is the Github master repository, and acquia is the Acquia master repository.

Typical merge process

Fetch updates from the desired remote. This will get you the corresponding branch.

git fetch partner1

Pull requests will generally come over via a branch on the remote repository. Checkout the branch for the submitted pull request.

git checkout the-branch-that-fixes-everything

Make sure the branch merges correctly with upstream master.

git pull upstream master

If there are no conflicts, continue with testing, e.g.

drush @alias updb
drush @alias fl
etc.

Once everything is validated, merge into master.

git checkout master
git merge the-branch-that-fixes-everything

Make sure we grab any upstream changes that may have come through.

git pull upstream master

And push to the various remotes. In this case, we push to 3: origin, upstream, acquia.

git push origin master
git push upstream master
git push acquia master

Note that if you're not ready to actually deploy the changes to the Acquia environment, you could simply leave it out of the push process.

Finally, make a tag and send that tag to the various environments. We use a three-point version number.

git tag -a v1.2.3 -m "This is the tag that fixes all the bugs."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment