Difference between revisions of "Git FAQs"
(→Bring in changes from a subtree's remote: It won't work if you only have the remote but not the branch.) |
|||
(7 intermediate revisions by 2 users not shown) | |||
Line 35: | Line 35: | ||
#* <code>git commit -m ''Initialize the repository with all the files''</code> | #* <code>git commit -m ''Initialize the repository with all the files''</code> | ||
+ | Note: The <code>shared</code> parameter on <code>git init</code> 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. | Continue to the next section if you want to create a bare repository for it to push to/pull from. | ||
Line 43: | Line 44: | ||
# Create the bare repository. | # Create the bare repository. | ||
#* <code>git init --bare --shared ''bareRepoName''</code> | #* <code>git init --bare --shared ''bareRepoName''</code> | ||
+ | #** The <code>shared</code> parameter allows everyone in the same group to push to this repository. Remove that option if this is not the case. | ||
# cd to your repository that you want link to this bare repository. | # cd to your repository that you want link to this bare repository. | ||
# Set the remote. | # Set the remote. | ||
Line 123: | Line 125: | ||
== Check Branches == | == Check Branches == | ||
− | *See what branches there are: | + | *See what branches there are locally: |
** git branch | ** git branch | ||
+ | ** Current branch marked with '*' | ||
+ | |||
+ | * To see all branches including remote branches: | ||
+ | ** git branch -a | ||
** Current branch marked with '*' | ** Current branch marked with '*' | ||
Line 170: | Line 176: | ||
* git push origin ''branchName'' | * git push origin ''branchName'' | ||
− | == Work on a branch that exists on the remote == | + | == Work on/Use a branch that exists on the remote == |
*To see all branches including remotes: | *To see all branches including remotes: | ||
** git branch -a | ** git branch -a | ||
* Base work on a remote branch/merge back to it | * Base work on a remote branch/merge back to it | ||
− | ** git checkout - | + | ** git checkout -t origin/''branchName'' |
** Creates & checkouts branch called ''branchName'' | ** Creates & checkouts branch called ''branchName'' | ||
* Now push & pull from your ''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 == | == Resolve a merge/pull conflict == | ||
Line 188: | Line 198: | ||
resolved the merge | resolved the merge | ||
* You can also use a mergetool: git mergetool | * You can also use a mergetool: git mergetool | ||
− | |||
== Create a tag == | == Create a tag == | ||
Line 208: | Line 217: | ||
* Not automatically pushed to the remote | * Not automatically pushed to the remote | ||
* git push origin ''tagName'' | * 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: | ||
+ | <pre> | ||
+ | [hooks] | ||
+ | mailinglist = "space delimited email addresses to email when changes are pushed" | ||
+ | </pre> | ||
+ | |||
+ | == "Push rejected non-fast forward" == | ||
+ | Two possible answers: | ||
+ | |||
+ | (1). You did not pull the current branch before trying to push. | ||
+ | |||
+ | Solution: do a <code>git pull</code>, then retry the <code>git push</code> | ||
+ | |||
+ | (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 | ||
+ | |||
+ | |||
+ | == Subtrees == | ||
+ | === Bring in changes from a subtree's remote === | ||
+ | If the remote is not already a part of this git repo: | ||
+ | #Add other project as a branch and fetch (-f). | ||
+ | #:<pre>git remote add -f libStatGen_remote https://github.com/statgen/libStatGen.git</pre> | ||
+ | #Create & checkout a branch for the remote. | ||
+ | #* taking <code>master</code>: | ||
+ | #: <pre>git checkout -b libStatGen_branch libStatGen_remote/master</pre> | ||
+ | #* taking a tag: | ||
+ | #: <pre>git checkout -b vt_branch_SWS tags/SWS</pre> | ||
+ | |||
+ | If the branch is already a part of this git repo: | ||
+ | #Checkout the branch | ||
+ | #: <pre>git checkout libStatGen_branch</pre> | ||
+ | #Bring in the new version | ||
+ | #: <pre>git pull</pre> | ||
+ | |||
+ | === Merge the changes from the subtree into the base repository === | ||
+ | #Go back to the master branch | ||
+ | #: <pre>git checkout master</pre> | ||
+ | #(Optional) Only if adding a new subdirectory, do: | ||
+ | #: <pre>git read-tree --prefix=src/libStatGen -u libStatGen_branch</pre> | ||
+ | #Merge in the new version. | ||
+ | #: <pre>git merge --squash -s subtree --no-commit libStatGen_branch</pre> | ||
+ | #:* --squash: do not merge histories | ||
+ | #:* -s subtree: merge strategy | ||
+ | #:*--no-commit: just merge, don’t commit the changes | ||
+ | #Commit the changes | ||
+ | #: <pre>git commit -m “message”</pre> |
Latest revision as of 18:50, 8 September 2015
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.
- cd into your directory.
- Tell git to create a repository:
git init
- Add all your files to the repository:
git add .
- Check that you didn't add any undesired files:
git status
- Remove any undesired files (if necessary):
git reset HEAD filename1 filename2
- 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
- cd to where you want the remote repository located.
- 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.
- The
- cd to your repository that you want link to this bare repository.
- Set the remote.
git remote add origin path to/url for your bare repository
- Set the configuration for pulling.
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
- 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
- Edit a file.
- When done editing, commit the changes
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
- after, do a
- first do a
- 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
- Select the files to be staged in the Unstaged Changes Window;
- 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
- git commit -m "your commit message"
- 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. *.o
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):
- Create the branch
- git branch <branch-name> [<start-point(branch/tag)>]
- Switch to a Specified Branch
- git checkout <branch-name>
- Create the branch
- Create and switch to the branch in one step
- Create and switch to (checkout) a branch
- git checkout -b <branch-name> <start-point>
- Create and switch to (checkout) a branch
- Create branch, then switch to it (default starting point is the same as the current branch):
- See Your Branches - current one is marked with a "*"
- git branch
NOTES:
- 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
- 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:
[hooks] 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
Subtrees
Bring in changes from a subtree's remote
If the remote is not already a part of this git repo:
- Add other project as a branch and fetch (-f).
git remote add -f libStatGen_remote https://github.com/statgen/libStatGen.git
- 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
- taking
If the branch is already a part of this git repo:
- Checkout the branch
git checkout libStatGen_branch
- Bring in the new version
git pull
Merge the changes from the subtree into the base repository
- Go back to the master branch
git checkout master
- (Optional) Only if adding a new subdirectory, do:
git read-tree --prefix=src/libStatGen -u libStatGen_branch
- 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
- Commit the changes
git commit -m “message”