Difference between revisions of "Using Git Example"

From Genome Analysis Wiki
Jump to navigationJump to search
Line 60: Line 60:
  
 
== Add Files to the Repository ==
 
== Add Files to the Repository ==
This will create a couple of files in ~/code/learnGit/learningGit/.
+
This will create a couple of files in the learningGit repository.
 
 
The files will not show up in ~/code/learnGit/learningGit2/.
 
  
 
{| border="1"
 
{| border="1"
Line 68: Line 66:
 
=== Create the New Files ===
 
=== Create the New Files ===
 
These steps create README.txt and README1.txt in ~/code/learnGit/learningGit/.
 
These steps create README.txt and README1.txt in ~/code/learnGit/learningGit/.
 +
 +
The files will not show up in ~/code/learnGit/learningGit2/.
  
 
# Move to the ~/code/learnGit/learningGit
 
# Move to the ~/code/learnGit/learningGit
Line 89: Line 89:
 
===Stage the files===
 
===Stage the files===
 
This step stages the addition of README.txt and README1.txt in ~/code/learnGit/learningGit/.
 
This step stages the addition of README.txt and README1.txt in ~/code/learnGit/learningGit/.
 +
 +
The files will not show up in ~/code/learnGit/learningGit2/.
  
 
# Move back to the learningGit directory
 
# Move back to the learningGit directory
Line 109: Line 111:
 
=== Commit the Files ===
 
=== Commit the Files ===
 
This step stores README.txt and README1.txt in the local repository.
 
This step stores README.txt and README1.txt in the local repository.
 +
 +
The files will not show up in ~/code/learnGit/learningGit2/.
  
 
# Move back to the learningGit directory
 
# Move back to the learningGit directory
Line 129: Line 133:
 
=== Push the files to the bare repository ===
 
=== Push the files to the bare repository ===
 
This step pushes the files to the bare repository.
 
This step pushes the files to the bare repository.
 +
 +
The files will show up in ~/code/learnGit/learningGit2/.
  
 
# Move back to the learningGit directory
 
# Move back to the learningGit directory
Line 147: Line 153:
 
The files are now stored in the bare repository, so any new repositories that are clone from it will see the files.
 
The files are now stored in the bare repository, so any new repositories that are clone from it will see the files.
 
|[[Image:GitPush.png]]
 
|[[Image:GitPush.png]]
 +
|}
 +
 +
== Update Files in the Repository ==
 +
This will make updates to the learningGit repositories.
 +
 +
{| border="1"
 +
|valign="top"|
 +
=== Modify the Files ===
 +
These steps update README.txt in ~/code/learnGit/learningGit/.
 +
 +
# Move to the ~/code/learnGit/learningGit
 +
# Look at the current status to see nothing.
 +
# Modify README.txt
 +
# Look at the git status to see the changed file.
 +
# Look at the changes.
 +
cd ~/code/learnGit/learningGit
 +
git status
 +
echo AccidentallyOverwrote > README.txt
 +
git status
 +
git diff
 +
 +
|[[Image:GitUpdateFile.png]]
 +
|-
 +
|valign="top"|
 +
===Stage the Modified File ===
 +
This step stages the modification of README.txt in ~/code/learnGit/learningGit/.
 +
 +
# Stage the file
 +
# Look at the git status to see the staged file.
 +
# Look at the diffs, there are no differences
 +
git add README.txt
 +
git status
 +
git diff
 +
 +
|[[Image:GitStagedUpdateFile.png]]
 +
|-
 +
|valign="top"|
 +
=== Unstage the File ===
 +
This step unstages the modification to README.txt.
 +
 +
# Unstage the file
 +
# Look at the git status to see the unstaged file
 +
# Look at the diffs (they are back).
 +
git reset HEAD README.txt
 +
git status
 +
git diff
 +
 +
|[[Image:GitUnstage.png]]
 +
|-
 +
|valign="top"|
 +
=== Undo the Modifications ===
 +
This step undoes the modifications to README.txt.
 +
 +
# Look at README.txt
 +
# Undo the modification
 +
# Check the status
 +
# Check the diffs
 +
# Look at README.txt
 +
cat README.txt
 +
git checkout -- README.txt
 +
git status
 +
git diff
 +
cat README.txt
 +
 +
|[[Image:GitUndoMod.png]]
 
|}
 
|}

Revision as of 09:57, 8 July 2011

Create new Bare Repository

These steps will create a new bare repository called learningGit in ~/code/learnGit/bareRepo/.

  1. Create the containing directory
  2. Go to the correct directory
  3. Create a new bare repository
  4. Move into the new repository
  5. Look at the contents of the bare repository
mkdir -p ~/code/learnGit/bareRepo/
cd ~/code/learnGit/bareRepo/
git init --bare --shared learningGit
cd learningGit
ls
CreateNewGitRepository.png

Create Working Repositories

For this example, we are going to create 2 working repositories.

This is so you can see how changes don't affect the other repositories unless pushes & pulls are done.

Create a Working Repository

These steps will create a new working repository called learningGit in ~/code/learnGit/ that is a clone of ~/code/learnGit/bareRepo/learningGit.

  1. Move to the appropriate directory
  2. Clone the initial repository
  3. Move into the working directory
  4. Look at the contents (Note: learningGit has not yet had any files added to it)
cd ~/code/learnGit
git clone ~/code/learnGit/bareRepo/learningGit
cd learningGit
ls -a

GitEmptyRepo.png

Create a Second Working Repository

These steps will create a second working repository called learningGit2 in ~/code/learnGit/ that is a clone of ~/code/learnGit/bareRepo/learningGit.


  1. Move to the appropriate directory
  2. Clone the initial repository
  3. Move into the working directory
  4. Look at the contents (Note: learningGit has not yet had any files added to it)
cd ~/code/learnGit
git clone ~/code/learnGit/bareRepo/learningGit learningGit2
cd learningGit2
ls -a

GitCreate2ndRepo.png

Add Files to the Repository

This will create a couple of files in the learningGit repository.

Create the New Files

These steps create README.txt and README1.txt in ~/code/learnGit/learningGit/.

The files will not show up in ~/code/learnGit/learningGit2/.

  1. Move to the ~/code/learnGit/learningGit
  2. Look at the current status to see nothing.
  3. Create README.txt
  4. Create README1.txt
  5. Look at the git status to see 2 untracked files.
  6. Move to the learningGit2 directory
  7. Check that the files are not there
  8. Check the status in learningGit2
cd ~/code/learnGit/learningGit
git status
echo This repo is for Learning Git. > README.txt
echo Another file in the repository > README1.txt
git status

Note: the files are not yet stored in the repository.

GitNewFiles.png

Stage the files

This step stages the addition of README.txt and README1.txt in ~/code/learnGit/learningGit/.

The files will not show up in ~/code/learnGit/learningGit2/.

  1. Move back to the learningGit directory
  2. Stage the files
  3. Look at the git status to see 2 staged files.
  4. Move back to the learningGit2 directory
  5. Try to pull files
  6. Check the contents of learningGit2 to verify the files are NOT there.
cd ../learningGit
git add .
git status
cd ../learningGit2
git pull
ls

Note: the files are staged, but not yet stored in the ~/code/learnGit/learningGit repository.

GitStageFiles.png

Commit the Files

This step stores README.txt and README1.txt in the local repository.

The files will not show up in ~/code/learnGit/learningGit2/.

  1. Move back to the learningGit directory
  2. Commit the files
  3. Look at the git status to see no uncommitted changes.
  4. Move back to the learningGit2 directory
  5. Try to pull files
  6. Check the contents of learningGit2 to verify the files are NOT there.
cd ../learningGit
git commit -m "Add the first files"
git status
cd ../learningGit2
git pull
ls

Note: the files are stored in the ~/code/learnGit/learningGit repository, but are not yet in the bare repository.

GitCommit.png

Push the files to the bare repository

This step pushes the files to the bare repository.

The files will show up in ~/code/learnGit/learningGit2/.

  1. Move back to the learningGit directory
  2. Push the files without specifying the remote or branch (fails)
  3. Push the files specifying the remote and the branch (succeeds)
  4. Look at the git status to see no uncommitted changes.
  5. Move back to the learningGit2 directory
  6. Try to pull files
  7. Check the contents of learningGit2 to verify the files are there.
cd ../learningGit
git push
git push origin master
git status
cd ../learningGit2
git pull
ls

The files are now stored in the bare repository, so any new repositories that are clone from it will see the files.

GitPush.png

Update Files in the Repository

This will make updates to the learningGit repositories.

Modify the Files

These steps update README.txt in ~/code/learnGit/learningGit/.

  1. Move to the ~/code/learnGit/learningGit
  2. Look at the current status to see nothing.
  3. Modify README.txt
  4. Look at the git status to see the changed file.
  5. Look at the changes.
cd ~/code/learnGit/learningGit
git status
echo AccidentallyOverwrote > README.txt
git status
git diff
GitUpdateFile.png

Stage the Modified File

This step stages the modification of README.txt in ~/code/learnGit/learningGit/.

  1. Stage the file
  2. Look at the git status to see the staged file.
  3. Look at the diffs, there are no differences
git add README.txt
git status
git diff
GitStagedUpdateFile.png

Unstage the File

This step unstages the modification to README.txt.

  1. Unstage the file
  2. Look at the git status to see the unstaged file
  3. Look at the diffs (they are back).
git reset HEAD README.txt
git status
git diff
GitUnstage.png

Undo the Modifications

This step undoes the modifications to README.txt.

  1. Look at README.txt
  2. Undo the modification
  3. Check the status
  4. Check the diffs
  5. Look at README.txt
cat README.txt
git checkout -- README.txt
git status
git diff
cat README.txt
GitUndoMod.png