Difference between revisions of "Git for dummies"

From FreekiWiki
Jump to navigation Jump to search
(s/devo/dev.freegeek.org/g)
 
(31 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
==The basics: What you need to know==
 +
 
===Setup===
 
===Setup===
  
 
Set up your system:
 
Set up your system:
  sudo apt-get install git-core
+
sudo apt-get install git-core
  git config --global user.email somebody@somewhere.tld
+
git config --global user.email somebody@somewhere.tld
  git config --global user.name "John Doe"
+
git config --global user.name "John Doe"
  
 
Note that if you are using debian etch, you will need to get a backport of git from backports.org.
 
Note that if you are using debian etch, you will need to get a backport of git from backports.org.
Line 10: Line 12:
 
===Getting the repo===
 
===Getting the repo===
  
Get a copy of the ''library'' project (assuming the central repository is on a server named ''dev.freegeek.org''):
+
Get a copy of the ''fgdb.rb'' project:
  git clone dev.freegeek.org:/git/library
+
git clone https://github.com/freegeek-pdx/fgdb.rb
  cd library/
+
cd fgdb.rb
  
Sort of like svn update:
+
Sort of like svn update, to receive new changes to the repository:
  git pull
+
git pull
  
Like svn update (but if you have local commits):
 
  git fetch
 
  git rebase origin/master
 
 
(Master is like trunk is in svn.)
 
(Master is like trunk is in svn.)
  
Line 25: Line 24:
  
 
Now edit a file
 
Now edit a file
  vi TODO  
+
vi TODO
 
(See it's just like svn!!)
 
(See it's just like svn!!)
 +
 +
To see the status of all files, whether they are changed, staged, or not tracked:
 +
git status
  
 
Now schedule this modification to be committed locally:
 
Now schedule this modification to be committed locally:
  git add TODO  
+
git add TODO
  
Want to see changes that you hain't yet git added?
+
Want to see changes that you haven't yet git added?
  git diff
+
git diff
  
 
Want to see what you are about to commit?
 
Want to see what you are about to commit?
  git diff --cached
+
git diff --cached
  
 
Now commit it locally:
 
Now commit it locally:
  git commit
+
git commit
 +
 
 +
Now, if you want to commit all of the changes you've made, but don't want to "git add" a million files, you can shorten it to this:
 +
git commit -a
 +
 
 +
The -a tells git to commit "all" - NOTE: this only applies to files already tracked by git.
 +
 
 +
If you screw up changes to a file, and just want to put it back:
 +
git checkout myfile
 +
 
 +
think of checkout (when used like this) as the git equivalent of "svn revert".
  
 
Now send your locally committed changes to the main repository:
 
Now send your locally committed changes to the main repository:
  git push
+
git push
 +
 
 +
==Cool toys: What you want to know==
 +
 
 +
also known as "candy for the coders".
 +
 
 +
===cool settings===
 +
 
 +
TODO
 +
 
 +
===git-rebase===
 +
 
 +
TODO
 +
 
 +
====keeping a fork====
 +
 
 +
TODO
 +
 
 +
====reverting a commit====
 +
 
 +
yes, you can rebase out a commit. But '''never''' make changes to commits that have already been pushed. The correct way to "undo" a pushed commit is to do this:
 +
git revert SHA1_HASH_GOES_HERE
 +
 
 +
===git-add -i===
 +
 
 +
TODO
 +
 
 +
===repository maintenance: taking out the trash===
 +
 
 +
TODO
 +
 
 +
==Complicated stuff: What you really don't want to know==
 +
 
 +
===Referencing a commit===
 +
 
 +
TODO
 +
 
 +
===Wtf is a refspec===
 +
 
 +
TODO
 +
 
 +
===Branches===
 +
 
 +
TODO
 +
 
 +
====Creating a new remote branch====
 +
 
 +
To push your local branch, A, to a new (not yet created) remote branch, B, do this:
 +
git push origin A:B
 +
 
 +
Most of the time, you'll just want to do this:
 +
git push origin master:master
 +
 
 +
After you push a local branch to a new remote branch, if you want your local branch to automatically know to pull from it, run this:
 +
git config branch.$(git branch | awk '/^* /{print $2}').remote origin; git config branch.$(git branch | awk '/^* /{print $2}').merge $(git branch | awk '/^* /{print $2}')
 +
 
 +
That looks complicated, but it isn't. I just added a bunch of awks so that it automatically works on the current branch.
 +
The way that you would normally do it is like this:
 +
git config branch.A.remote origin; git config branch.A.merge B
 +
 
 +
where B is the remote branch, A is the local branch, and origin in the remote (origin is the default created when you clone from somewhere).
 +
 
 +
====Deleting remote branch====
 +
 
 +
from [https://37s.backpackit.com/pub/1465067 here]:
 +
git branch -d {your_branch_here}
 +
git push {repository} :heads/{your_branch_here}
 +
 
 +
====Merging====
 +
 
 +
TODO: explain how to merge
 +
 
 +
If you are going to merge something, for now make the changes '''in the branch''', then merge it into trunk. This way it is tracked correctly. While cherry-pick can be used to get one commit from trunk into the branch, it does not track it as a merge.
 +
 
 +
===Tags and all the associated evil===
 +
 
 +
TODO
 +
 
 +
The evil: once you push a tag, it's final. you can push a new version of the tag, but anybody who has already fetched the tag will not even know about the change. When you screw up a tag, you have to either send out an email instructing people to delete the tag and refetch it or use a different tag name. Not a good thing to do. There is reasoning behind this, though. Git is targeted towards security, and works by the concept that if you know the 40 character SHA1 hash of the most recent commit, you can trust the entire tree of commits.
 +
 
 +
===The index===
 +
 
 +
So, with git you have multiple places where changes are "kept":
 +
* remote repository
 +
* local repository
 +
* the index
 +
* your working copy
 +
 
 +
The index is where you "stage" changes you are about to commit. git-add takes changes from your working copy and updates the index to match (this was called git-update-index in previous versions of git).
 +
 
 +
After you edit the file (in your working copy), there are a few steps to get it to the remote repository.
 +
 
 +
From working copy to index:
 +
 
 +
git add file
 +
 
 +
From index to local repository:
 +
 
 +
git commit
 +
 
 +
From local repository to remote repository:
 +
 
 +
git push
 +
 
 +
==ticgit for dummies==
 +
===setup (for users)===
 +
In your home dir run:
 +
sudo apt-get install ruby libgit-ruby
 +
git clone git://github.com/schacon/ticgit.git
 +
git clone dev.freegeek.org:/git/fgdb.rb
 +
cd fgdb.rb
 +
git branch ticgit origin/ticgit
 +
 
 +
Add this to /usr/bin/ti:
 +
#!/bin/sh
 +
RUBYLIB=/home/$USER/ticgit/lib /home/$USER/ticgit/bin/ti "$@"
 +
 
 +
and run 'chmod +x /usr/bin/ti'.
 +
 
 +
===setup (for repositories)===
 +
 
 +
Run something like this:
 +
git symbolic-ref HEAD refs/heads/ticgit; rm .git/index; echo hold > .hold; git add .hold; git commit -m "create ticgit branch"; git checkout master
 +
 
 +
then push the ticgit branch, &c.
 +
 
 +
===example commands===
 +
 
 +
While in ~/fgdb.rb you can run ti commands.
 +
 
 +
See this for how to work it: [http://github.com/schacon/ticgit/wikis]
 +
 
 +
Here's some examples:
 +
ti list
 +
ti recent
 +
ti show ######
 +
ti state ###### resolved
 +
ti comment ######
 +
ti tag ###### sprint-21
 +
ti new
 +
 
 +
NOTE: changes will be made in your name. it gets your name from your git config. so make sure you set it up correctly (see above).
 +
 
 +
===The git part===
 +
 
 +
updating:
 +
git pull origin ticgit:ticgit
 +
 
 +
pushing your changes:
 +
git push origin ticgit:ticgit
 +
 
 +
be sure to update before you change stuff, and push after, unless you want to have some merging fun ;)
 +
 
 +
If you screw it up and just want to put it back to what the server has, run this:
  
 +
git branch -f ticgit origin/ticgit
  
[http://git.or.cz/course/svn.html Git - SVN Crash Course]
+
==Other resources==
 +
* [http://git.or.cz/course/svn.html Git - SVN Crash Course] - A good beginners tutorial
 +
* [http://en.wikipedia.org/wiki/Git_(software) Git (software) - Wikipedia, the free encyclopedia] - Explains a lot about how git works
  
 
[[Category: Coders]]
 
[[Category: Coders]]

Latest revision as of 10:59, 16 December 2013

The basics: What you need to know

Setup

Set up your system:

sudo apt-get install git-core
git config --global user.email somebody@somewhere.tld
git config --global user.name "John Doe"

Note that if you are using debian etch, you will need to get a backport of git from backports.org.

Getting the repo

Get a copy of the fgdb.rb project:

git clone https://github.com/freegeek-pdx/fgdb.rb
cd fgdb.rb

Sort of like svn update, to receive new changes to the repository:

git pull

(Master is like trunk is in svn.)

Making changes

Now edit a file

vi TODO

(See it's just like svn!!)

To see the status of all files, whether they are changed, staged, or not tracked:

git status

Now schedule this modification to be committed locally:

git add TODO

Want to see changes that you haven't yet git added?

git diff

Want to see what you are about to commit?

git diff --cached

Now commit it locally:

git commit

Now, if you want to commit all of the changes you've made, but don't want to "git add" a million files, you can shorten it to this:

git commit -a

The -a tells git to commit "all" - NOTE: this only applies to files already tracked by git.

If you screw up changes to a file, and just want to put it back:

git checkout myfile

think of checkout (when used like this) as the git equivalent of "svn revert".

Now send your locally committed changes to the main repository:

git push

Cool toys: What you want to know

also known as "candy for the coders".

cool settings

TODO

git-rebase

TODO

keeping a fork

TODO

reverting a commit

yes, you can rebase out a commit. But never make changes to commits that have already been pushed. The correct way to "undo" a pushed commit is to do this:

git revert SHA1_HASH_GOES_HERE

git-add -i

TODO

repository maintenance: taking out the trash

TODO

Complicated stuff: What you really don't want to know

Referencing a commit

TODO

Wtf is a refspec

TODO

Branches

TODO

Creating a new remote branch

To push your local branch, A, to a new (not yet created) remote branch, B, do this:

git push origin A:B

Most of the time, you'll just want to do this:

git push origin master:master

After you push a local branch to a new remote branch, if you want your local branch to automatically know to pull from it, run this:

git config branch.$(git branch | awk '/^* /{print $2}').remote origin; git config branch.$(git branch | awk '/^* /{print $2}').merge $(git branch | awk '/^* /{print $2}')

That looks complicated, but it isn't. I just added a bunch of awks so that it automatically works on the current branch. The way that you would normally do it is like this:

git config branch.A.remote origin; git config branch.A.merge B

where B is the remote branch, A is the local branch, and origin in the remote (origin is the default created when you clone from somewhere).

Deleting remote branch

from here:

git branch -d {your_branch_here}
git push {repository} :heads/{your_branch_here}

Merging

TODO: explain how to merge

If you are going to merge something, for now make the changes in the branch, then merge it into trunk. This way it is tracked correctly. While cherry-pick can be used to get one commit from trunk into the branch, it does not track it as a merge.

Tags and all the associated evil

TODO

The evil: once you push a tag, it's final. you can push a new version of the tag, but anybody who has already fetched the tag will not even know about the change. When you screw up a tag, you have to either send out an email instructing people to delete the tag and refetch it or use a different tag name. Not a good thing to do. There is reasoning behind this, though. Git is targeted towards security, and works by the concept that if you know the 40 character SHA1 hash of the most recent commit, you can trust the entire tree of commits.

The index

So, with git you have multiple places where changes are "kept":

  • remote repository
  • local repository
  • the index
  • your working copy

The index is where you "stage" changes you are about to commit. git-add takes changes from your working copy and updates the index to match (this was called git-update-index in previous versions of git).

After you edit the file (in your working copy), there are a few steps to get it to the remote repository.

From working copy to index:

git add file

From index to local repository:

git commit

From local repository to remote repository:

git push

ticgit for dummies

setup (for users)

In your home dir run:

sudo apt-get install ruby libgit-ruby
git clone git://github.com/schacon/ticgit.git
git clone dev.freegeek.org:/git/fgdb.rb
cd fgdb.rb
git branch ticgit origin/ticgit

Add this to /usr/bin/ti:

#!/bin/sh
RUBYLIB=/home/$USER/ticgit/lib /home/$USER/ticgit/bin/ti "$@"

and run 'chmod +x /usr/bin/ti'.

setup (for repositories)

Run something like this:

git symbolic-ref HEAD refs/heads/ticgit; rm .git/index; echo hold > .hold; git add .hold; git commit -m "create ticgit branch"; git checkout master

then push the ticgit branch, &c.

example commands

While in ~/fgdb.rb you can run ti commands.

See this for how to work it: [1]

Here's some examples:

ti list
ti recent
ti show ######
ti state ###### resolved
ti comment ######
ti tag ###### sprint-21
ti new

NOTE: changes will be made in your name. it gets your name from your git config. so make sure you set it up correctly (see above).

The git part

updating:

git pull origin ticgit:ticgit

pushing your changes:

git push origin ticgit:ticgit

be sure to update before you change stuff, and push after, unless you want to have some merging fun ;)

If you screw it up and just want to put it back to what the server has, run this:

git branch -f ticgit origin/ticgit

Other resources