Difference between revisions of "Git Tips"

From IARC 207 Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by one other user not shown)
Line 15: Line 15:
 
To prevent tracking of extraneous dot files, pycs etc etc, it's a good idea to add a .gitignore to your root git directory... or supply one to your global settings.  To set up a per-repo one, find the text appropriate to your repository here:<br>
 
To prevent tracking of extraneous dot files, pycs etc etc, it's a good idea to add a .gitignore to your root git directory... or supply one to your global settings.  To set up a per-repo one, find the text appropriate to your repository here:<br>
 
https://github.com/github/gitignore<br>
 
https://github.com/github/gitignore<br>
 +
e.g.<br>
 +
https://github.com/github/gitignore/blob/master/Perl.gitignore<br>
 +
https://github.com/github/gitignore/blob/master/Python.gitignore<br>
 +
https://github.com/github/gitignore/blob/master/R.gitignore<br>
 +
https://github.com/github/gitignore/blob/master/Global/Linux.gitignore<br>
 +
https://github.com/github/gitignore/blob/master/Global/OSX.gitignore<br>
 +
https://github.com/github/gitignore/blob/master/Global/Windows.gitignore<br>
 
Then, using a text editor, write these to a .ignore file in the root repo directory.  Alternately, you can do the same on a global basis (i.e. for all of the git projects on your computer):
 
Then, using a text editor, write these to a .ignore file in the root repo directory.  Alternately, you can do the same on a global basis (i.e. for all of the git projects on your computer):
 +
$ gedit ~/.global_ignore &
 +
[Paste in content from the links above & save]
 
  $ git config --global core.excludesfile ~/.global_ignore
 
  $ git config --global core.excludesfile ~/.global_ignore
 
If you screw up and add the wrong files to your project by accident in the initial setup, you can remove them like this:
 
If you screw up and add the wrong files to your project by accident in the initial setup, you can remove them like this:
Line 61: Line 70:
 
Push local repo to your remote one (in this case, remote is a dropbox share):
 
Push local repo to your remote one (in this case, remote is a dropbox share):
 
  $ git push ~/Dropbox/bob_n_bob/git/busey_erode_inputs.git master
 
  $ git push ~/Dropbox/bob_n_bob/git/busey_erode_inputs.git master
 +
 +
Now, say you are just starting out sharing a codebase with someone.  In the past you've each had independent repositories.  Now however, you'd like to combine them and your partner has already initialized the shared remote repo... 
 +
$ git push ~/Dropbox/bob_n_bob/git/framework.git master
 +
'''Ooops!'''
 +
To /Users/bob/Dropbox/bob_n_bob/git/Framework.git
 +
    ! [rejected]        master -> master (non-fast-forward)
 +
  error: failed to push some refs to '/Users/bob/Dropbox/bob_n_bob/git/Framework.git'
 +
  hint: Updates were rejected because the tip of your current branch is behind
 +
  hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
 +
  hint: before pushing again.
 +
  hint: See the 'Note about fast-forwards' in 'git push --help' for details.
 +
So:
 +
$ git fetch origin
 +
From /Users/bob/Dropbox/bob_n_bob/git/Framework
 +
* [new branch]      master    -> origin/master
 +
No problem there.  Now let's try pulling the rest:
 +
$ git pull origin master
 +
    U    Erode3_Jan_2013/erode_base.py
 +
    U    framework.py
 +
Pull is not possible because you have unmerged files.
 +
Please, fix them up in the work tree, and then use 'git add/rm <file>'
 +
as appropriate to mark resolution, or use 'git commit -a'.
 +
So, time to fire up the mergetool to sort this out:
 +
$ git mergetool
 +
  Merging:
 +
    Erode3_Jan_2013/erode_base.py
 +
    framework.py
 +
This may / should bring up a windowed diff manager to allow you to select which changes to accept... if it doesn't, install one:
 +
# aptitude install meld
 +
Next... commit the merged conflict fixes:
 +
$ git commit -m "rectified merge conflicts"
 +
$ git push origin master
 +
Should be golden.  Here's one place for more info:
 +
http://stackoverflow.com/questions/161813/how-do-i-fix-merge-conflicts-in-git
 +
 +
==Yet to be fleshed out but probably of use==
 +
I haven't gotten to it yet but I am guessing that when it comes to configuration files in the repositories where there are absolute paths, which vary by user, filtering using ''gitattributes'' will be the solution:
 +
http://man.he.net/man5/gitattributes
 +
Maybe something like this (although I suspect there is more to it):
 +
* Add to ''.gitattributes'':
 +
Test_erode_global.cfg filter=in_directory
 +
Test_erode_global.cfg fileter=out_directory
 +
* Then, in .git/config add something like:
 +
[filter "in_directory"]
 +
        clean = indent
 +
        smudge = cat
 +
I'm sort of leaving this incomplete for the moment but basically that link is a good starting point and I'll try to do implement myself.  I also remember Ken suggesting I do something like this for CR basic programs.  So, I might implement that, couldn't hurt.
  
 
==Incorporating Github as a remote repository==
 
==Incorporating Github as a remote repository==

Latest revision as of 12:43, 28 February 2013

Some Online Resources:

As specific questions come up you'll find more stuff via searching, I'm sure.
http://try.github.com/levels/1/challenges/1
http://git-scm.com/book/en/Git-Basics-Viewing-the-Commit-History
http://rogerdudler.github.com/git-guide/
http://ndpsoftware.com/git-cheatsheet.html
http://git-scm.com/downloads
http://www.sbf5.com/~cduan/technical/git/git-1.shtml https://tiredblogger.wordpress.com/2009/11/09/creating-local-git-repositories-yeah-its-that-simple/

Settings More Global / General in Nature

Set up your name/ email address type stuff:

$ git config --global user.email "me@here.com"
$ git config --global user.name "Bob"

To prevent tracking of extraneous dot files, pycs etc etc, it's a good idea to add a .gitignore to your root git directory... or supply one to your global settings. To set up a per-repo one, find the text appropriate to your repository here:
https://github.com/github/gitignore
e.g.
https://github.com/github/gitignore/blob/master/Perl.gitignore
https://github.com/github/gitignore/blob/master/Python.gitignore
https://github.com/github/gitignore/blob/master/R.gitignore
https://github.com/github/gitignore/blob/master/Global/Linux.gitignore
https://github.com/github/gitignore/blob/master/Global/OSX.gitignore
https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
Then, using a text editor, write these to a .ignore file in the root repo directory. Alternately, you can do the same on a global basis (i.e. for all of the git projects on your computer):

$ gedit ~/.global_ignore & 
[Paste in content from the links above & save]
$ git config --global core.excludesfile ~/.global_ignore

If you screw up and add the wrong files to your project by accident in the initial setup, you can remove them like this:

$ git rm *.pyc
$ git rm --cached *.pyc
$ git commit -am 'Accidentally added an excluded file type.. did not have .ignore configured yet'   

Then, check to see they're now excluded by listing all the files in version control:

$ git ls-tree -r master --name-only

Quick overview of commits that have been made:

$ git log --pretty=format:"%h - %an, %ar : %s"
8d8befa - Frank, 11 hours ago : relative paths set up for the initial configs
1e0b20d - Frank, 17 hours ago : Initial commit of the super simple test run supplied by Scott.

Oh, also, if you don't want to use nano as your default text editor here are another couple of options:

$ git config --global core.editor "gedit"
$ git config --global core.editor "vim"
$ git config --global core.editor "mate -w"

Getting down to business with setting up and using a particular repository (Basic Usage)

Now, let's set up a local repository and a remote one to push to / share with others.
First, set up your local repo:

$ cd /path/to/directory_to_enable_git_in/
$ git init
$ git add *
$ git commit -am "first commit"


Next, set up a remote repository when you already have an existing one... in this case, the remote is in another folder, like a dropbox share: e.g.
http://stackoverflow.com/questions/1960799/using-gitdropbox-together-effectively

$ cd ~/Dropbox/bob_n_bob/git/
$ git init --bare erode_utilities.git
$ cd ~/path/to/local_repo/
$ git remote add origin ~/Dropbox/bob_n_bob/git/erode_utilities.git
$ git remote set-url origin ~/Dropbox/bob_n_bob/git/erode_utilities.git
$ git push -u origin master

Say you typo the previous set up or your remote has a new location, you can change no problem from git:
http://stackoverflow.com/questions/2432764/how-to-change-a-remote-repository-uri-using-git

$ git remote add origin ~/dropbox/bob_n_bob/git/erode_clubfooted_typos_in_here.git   # oops!
$ git remote set-url origin ~/dropbox/bob_n_bob/git/erode_correct_utilities.git      # this command corrects for the previous error
$ git push -u origin master                                                          # now you can push your local repo to the remote.


Push local repo to your remote one (in this case, remote is a dropbox share):

$ git push ~/Dropbox/bob_n_bob/git/busey_erode_inputs.git master

Now, say you are just starting out sharing a codebase with someone. In the past you've each had independent repositories. Now however, you'd like to combine them and your partner has already initialized the shared remote repo...

$ git push ~/Dropbox/bob_n_bob/git/framework.git master

Ooops!

To /Users/bob/Dropbox/bob_n_bob/git/Framework.git
    ! [rejected]        master -> master (non-fast-forward)
 error: failed to push some refs to '/Users/bob/Dropbox/bob_n_bob/git/Framework.git'
 hint: Updates were rejected because the tip of your current branch is behind
 hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
 hint: before pushing again.
 hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So:

$ git fetch origin
From /Users/bob/Dropbox/bob_n_bob/git/Framework
* [new branch]      master     -> origin/master

No problem there. Now let's try pulling the rest:

$ git pull origin master
   U    Erode3_Jan_2013/erode_base.py
   U    framework.py
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'.

So, time to fire up the mergetool to sort this out:

$ git mergetool
  Merging:
    Erode3_Jan_2013/erode_base.py
    framework.py

This may / should bring up a windowed diff manager to allow you to select which changes to accept... if it doesn't, install one:

# aptitude install meld

Next... commit the merged conflict fixes:

$ git commit -m "rectified merge conflicts"
$ git push origin master

Should be golden. Here's one place for more info: http://stackoverflow.com/questions/161813/how-do-i-fix-merge-conflicts-in-git

Yet to be fleshed out but probably of use

I haven't gotten to it yet but I am guessing that when it comes to configuration files in the repositories where there are absolute paths, which vary by user, filtering using gitattributes will be the solution: http://man.he.net/man5/gitattributes Maybe something like this (although I suspect there is more to it):

  • Add to .gitattributes:
Test_erode_global.cfg filter=in_directory
Test_erode_global.cfg fileter=out_directory
  • Then, in .git/config add something like:
[filter "in_directory"]
       clean = indent
       smudge = cat

I'm sort of leaving this incomplete for the moment but basically that link is a good starting point and I'll try to do implement myself. I also remember Ken suggesting I do something like this for CR basic programs. So, I might implement that, couldn't hurt.

Incorporating Github as a remote repository

To deal with creating keys and using them with GitHub:
https://help.github.com/articles/generating-ssh-keys

If you're using Github... and you mess up, post some private data (people pushing private keys to github was in the news in 2012 I think)
https://help.github.com/articles/remove-sensitive-data



Windows Specific Notes

To use Git from Aptana Studio 3:

  • (from Mozilla Firefox, not IE) install Aptana Studio 3:
  • Note that after installing Aptana Studio 3, it will have to be configured to use Pydev:
    • Under the windows menu in Aptana Studio, select preferences then select the python interpreter.
    • Under the windows menu in Aptana Studio, select Open Perspective, then other, then yDev
    • Keys will have to be created in the Git Bash window.
    • Also from the GitBash window, git init will need to be run from the desired workspace directory
    • Some git configuring will be required from the Git Bash window (as shown above in the general git section):
      • git config --global user.email "me@here.com"
      • git config --global user.name "Your Name Comes Here"
  • To work on and download existing project from GitHub:
    • right-click on the PyDev Package Explorer, select the “Import” option, and in the “URI” box that pops up, paste the DataPro from GitHub link:
      • git@github.com:frankohanlon/DataPro.git (the other box should automatically fill up with corresponding text)
  • To use/locate git commands from Aptana Studio 3:
    • Right-click on the project, select Team and then the appropriate Git command.
  • To upload new project to GITHUB from Aptana Studio 3:
  1. Create a new project
  2. Right-click on project..."Team > Share Project". Commit your local contents to that repo
  3. Create a new Github repo
  4. From Aptana studio, right-click "Add remote" and paste in the URL of your project<br\>(Team-> Remotes-->Add Remote)
  5. $ git status
  6. $ git add src/
  7. You have to follow up with one manual step ATM. Type "git push -u origin master" in the console for the project
  8. You can now push your files up to Github.

Notes from Bryan:

I once had a weird error..The error message was something like this:
:error: src refspec master does not match any.
:error: failed to push some refs to 'git@github ... .git'
It was solved by executing the following commands:
:$ touch README
:$ git add README
:$ git add (all other files)
:$ git commit -m 'reinitialized files'
:$ git push origin master --force