Git FAQs

From Genome Analysis Wiki
Revision as of 18:04, 1 September 2015 by Pjvh (talk | contribs) (Subtrees: Format code.)
Jump to: navigation, search

Git FAQs

For more information on how to use git, see: How To Use Git

Learn how a specific command works

Command Example
git help <command> git help clone
man git-<command> man git-clone
git-<command> -h git-clone -h

Turn an already existing directory into a Git Repository

Its easy to turn an already existing directory into its own repository.

  1. cd into your directory.
  2. Tell git to create a repository:
    • git init
  3. Add all your files to the repository:
    • git add .
  4. Check that you didn't add any undesired files:
    • git status
  5. Remove any undesired files (if necessary):
    • git reset HEAD filename1 filename2
  6. Commit the files:
    • git commit -m Initialize the repository with all the files

Note: The shared parameter on git init allows everyone in the same group to push to this repository. If this is the case, add that parameter.

Continue to the next section if you want to create a bare repository for it to push to/pull from.

Create a remote bare Git repository for a repository to push to/pull from

  1. cd to where you want the remote repository located.
  2. Create the bare repository.
    • git init --bare --shared bareRepoName
      • The shared parameter allows everyone in the same group to push to this repository. Remove that option if this is not the case.
  3. cd to your repository that you want link to this bare repository.
  4. Set the remote.
    • git remote add origin path to/url for your bare repository
  5. Set the configuration for pulling.
    • git config branch.master.remote origin
    • git config branch.master.merge refs/heads/master
  6. Push your files to the bare repository (must specify origin master since this is the first push to the empty repository)
    • git push origin master

You can now operate as if you had cloned from bareRepoName.

Use a Previously Setup Repository

Clone it:

 git clone <repository to be cloned> [optional new directory name]

Update files

Unmodify files

Instructions are also given in git status

  • git checkout -- fileName

Identify non-committed changes

  • Using Command-Line (2 ways)
    • git status - list changed/removed/staged files
    • git diff - lists the differences
  • Using git-gui
    • They are listed under Unstaged Changes

Stage files/changes prior to committing them

  • Using Command-Line (multiple ways)
    • git add <file1> <file2> (adds specified files)
    • git add . (adds all files)
      • first do a git status to see what files will be staged
        • after, do a git status to verify you want those files staged
  • Using git-gui (multiple ways): Should see files move from Unstaged to Staged
    • Click on the file icons in the Unstaged Changes window
    • 2 Step process
      1. Select the files to be staged in the Unstaged Changes Window;
      2. Commit->Stage To Commit

Unstage files

Instructions are also given in git status

  • git reset HEAD filename1 filename2

Commit/Store Changes

Files are already staged:

  • To see staged files use:
    • git diff --cached
  • Using Command-Line
    • git commit -m "your commit message"
      • if you do not specify -m, it will open up your default editor for you to enter the message, enter it, save, and exit
  • Using git-gui
    • Press the "Commit" button

Files are not already staged (2 ways):

  • git commit -a -m "your commit message"
    • Always do a git status first - all modified files will be committed.
  • git add (adds to the index/stage); git commit -m "your commit message"

Bringing in (Pulling) changes from source repository (the one you cloned from)

  • In your local/working repository:
    • git pull
    • Resolve any unresolved merges & commit those resolutions.

Ignore files that you don't want to show up in git status

Setup git to ignore files with certain names, extensions, etc

If all users of the repository will want to ignore the files:

  • Create/update a .gitignore file containing the files you want to ignore (use #'s as comments), for example:
# Ignore editor created temp files
# ignore object files.

If the ignored files are specific for just you, add your ignores file (formatted like the .gitignore):

  • git config core.excludesfile ~/git/config/ignoreFiles

Check Branches

  • See what branches there are locally:
    • git branch
    • Current branch marked with '*'
  • To see all branches including remote branches:
    • git branch -a
    • Current branch marked with '*'

Create New Branch

First change to the branch (could be master, the default branch) you want your branch to start from.

  • 2 ways to create a new branch and switch to it:
    • Create branch, then switch to it (default starting point is the same as the current branch):
      1. Create the branch
        • git branch <branch-name> [<start-point(branch/tag)>]
      2. Switch to a Specified Branch
        • git checkout <branch-name>
    • Create and switch to the branch in one step
      1. Create and switch to (checkout) a branch
        • git checkout -b <branch-name> <start-point>
  • See Your Branches - current one is marked with a "*"
    • git branch


  • If you switch to the branch in one window, it switches it in all
  • Beware of switching branches when you have non-committed files.
    • Until you commit the files, they will be in all branches.
    • If a branch has a different head than your current branch for the non-committed files, the branch change fails.

Merging Changes

  • Checkout the branch you want to merge into
    • git checkout branchMergeInto
    • Often: git checkout master
  • Merge the other branch into it
    • git merge branchMergingFrom
  • Resolve and commit any merge conflicts.
  • To Undo a merge before committing:
    • git reset --hard HEAD

Note: ”Fast forward” - no divergent work, just moves the pointer to the latest commit on the other branch

Delete a Branch

Delete branch after merging & you are done with it

  • git branch -d branchName
  • Make sure you have merged first!
  • Delete remote branch:
  • git push origin :branchName

Share a Branch by pushing to the Remote

  • git push origin branchName

Work on/Use a branch that exists on the remote

  • To see all branches including remotes:
    • git branch -a
  • Base work on a remote branch/merge back to it
    • git checkout -t origin/branchName
    • Creates & checkouts branch called branchName
  • Now push & pull from your branchName

Note: A typical wrong way is: clone from the remote repository, then pull the branch

Note: A local branch name is only informative to you. There is no relationship between that branch name and the branches named at the repository that you initially did the pull from. So you may, for example, name your local branch "fix_karma_paired_end" or anything else arbitrary even if the remote branch is "0.8.8S". As described above, the -t option will create a local branch named after the remote branch, also specify -b if you want to customize the name.

Resolve a merge/pull conflict

  • Make appropriate changes
  • Delete the <<<<<<<, =======, and >>>>>>>
  • Add to the staged files
    • git add nowMergedFile
  • Commit the merge
    • git commit
    • Update the default merge message with a description of how you

resolved the merge

  • You can also use a mergetool: git mergetool

Create a tag

Tags can be used when releasing, if you want to mark that version so you can go back to it

  • git tag -a tagName -m 'tag description'
  • Example:
git tag -a v1.1 -m 'version 1.1 contains the first version

See what tags there are

  • git tag
  • git tag -l v1.1.*

Get info on a tag

  • git show tagName

Push Tag to remote

  • Not automatically pushed to the remote
  • git push origin tagName

Setup an email hook on pushes

  • cd yourDirectory/.git/hooks
  • cp post-receive.sample post-receive
  • Uncomment the line at the end of post-receive that references post-receive-email
  • Add the following to yourDirectory/.git/config:
	mailinglist = "space delimited email addresses to email when changes are pushed"

"Push rejected non-fast forward"

Two possible answers:

(1). You did not pull the current branch before trying to push.

Solution: do a git pull, then retry the git push

(2). You only pulled a branch but want to push it back to master branch.

Committed to the wrong branch

If you have NOT yet pushed to a public repository and it was the last commit:

  • git reset --soft HEAD^
    • puts the changes back to staged.

git checkout branch_you_want git commit


Bring in changes from a subtree's remote

If the remote is not already a part of this git repo:

  1. Add other project as a branch and fetch (-f).
    git remote add -f libStatGen_remote
  2. Create & checkout a branch for the remote.
    • taking master:
    git checkout -b libStatGen_branch libStatGen_remote/master
    • taking a tag:
    git checkout -b vt_branch_SWS tags/SWS

If the remote is already a part of this git repo:

  1. Checkout the branch
    git checkout libStatGen_branch
  2. Bring in the new version
    git pull

Merge the changes from the subtree into the base repository

  1. Go back to the master branch
    git checkout master
  2. (Optional) Only if adding a new subdirectory, do:
    git read-tree --prefix=src/libStatGen -u libStatGen_branch
  3. Merge in the new version.
    git merge --squash -s subtree --no-commit libStatGen_branch
    • --squash: do not merge histories
    • -s subtree: merge strategy
    • --no-commit: just merge, don’t commit the changes
  4. Commit the changes
    git commit -m “message”