Using Git Example

From Genome Analysis Wiki
Jump to navigationJump to search

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
cd ~/code/learnGit/learningGit2
ls
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

Undo File Modifications (uncommitted) in the Repository

In the end, this has no affect on 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.

Your modifications are lost, since they were never stored in the database.

  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


Remove a File from the Repository

This will remove a file from the learningGit repositories.

Remove the File

These steps removes README1.txt from ~/code/learnGit/learningGit/.

  1. Look at the current status to see nothing.
  2. Remove README.txt
  3. Look at the git status to see the removed file.
  4. Look at the changes.
git status
ls
rm README1.txt
git status
git diff
GitRemoveFile.png

Stage the File Removal

This step stages the removal of README1.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 rm README1.txt
git status
git diff
GitStageRemoval.png

Commit the File Removal

This step commits the removal of README1.txt.

  1. Commit the file removal
  2. Look at the git status to see no modified files
git commit -m "Remove unnecessary file, README1.txt."
git status
GItCommitFileRemoval.png

Push & Pull the File Removal

This step pushes the removal of README1.txt to the main repository and pulls it into learningGit2.

  1. Push the file removal to the origin repository
  2. Move to learningGit2
  3. See what files are there
  4. Check the status
  5. Pull the latest changes
  6. See what files are there
  7. Check the log
git push
cd ~/code/learnGit/learningGit2
ls
git status
git pull
ls
git log
GitPushFileRemoval.png

Undo File Removal (uncommitted) in the Repository

In the end, this has no affect on the learningGit repositories.

Remove the Files

These steps remove README.txt from ~/code/learnGit/learningGit/.

  1. Move to the ~/code/learnGit/learningGit
  2. Look at the current status to see nothing.
  3. Remove README.txt
  4. Look at the git status to see the removed file.
  5. Look at the changes.
cd ~/code/learnGit/learningGit
git status
rm README.txt
git status
git diff
GitRemoveFile2.png

Stage the File Removal

This step stages the removal of README.txt from ~/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 rm README.txt
git status
git diff
GitStagedRemoveFile2.png

Unstage the File Removal

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
GitUnstageFile2.png

Undo the Removal

This step undoes the removal of README.txt.

The file removal was never stored in the database.

  1. Look at the directory contents
  2. Undo the removal
  3. Check the status
  4. Check the diffs
  5. Look at the directory contents
ls
git checkout -- README.txt
git status
git diff
ls
GitUndoRemoval.png

Ignore Files

This sets up a .gitignore file in the repository.

Create Files That Should be Ignored

These steps creates temp1.o in ~/code/learnGit/learningGit/ and temp2.o in ~/code/learnGit/learningGit2/.

  1. Move to ~/code/learnGit/learningGit/
  2. Create temp1.o
  3. Check status
  4. Move to ~/code/learnGit/learningGit2/
  5. Create temp2.o
  6. Check status
cd ~/code/learnGit/learningGit
touch temp1.o
git status
cd ~/code/learnGit/learningGit2
touch temp2.o
git status
GitCreateoFile.png

Accidentally Stage the file, then Unstage

This step accidentally stages an undesired file due to using the "git add ." and then unstages the file.

  1. Add everything
  2. Check status
  3. Undo the add
  4. Check status
git add .
git status
git reset HEAD temp2.o
git status
GitStageoFile.png

Ignore .o Files

Adds a .gitignore to ignore .o files.

  1. Create a .gitignore file containing *.o to ignore .o files
  2. Check status
  3. Stage the .gitignore file
  4. Commit the .gitignore file
  5. Push the .gitignore file
  6. Get the status
echo "*.o" > .gitignore
git status
git add .gitignore
git commit -m "Add .gitignore file to ignore *.o files"
git push
git status
GitGitIgnore.png

Pull .gitignore into other repository

Pulls the .gitignore file into the other repository

  1. Move to ~/code/learnGit/learningGit/
  2. Check status
  3. Pull in the changes
  4. Get the status
cd ~/code/learnGit/learningGit/
git status
git pull
git status
ls -a
GitPullGitIgnore.png

Diff against an Old Version

These steps show how to look at an old version of a file

Make updates

These steps make updates to README.txt.

  1. Look at README.txt
  2. Make update to README.txt
  3. Stage the update
  4. Commit the update
  5. Make another update to README.txt
  6. Stage the update
  7. Commit the update
  8. Look at README.txt
cat README.txt
echo "Update1" >> README.txt
git add README.txt
git commit -m "Add Update1 to README.txt"
echo "Update2" >> README.txt
git add README.txt
git commit -m "Add Update2 to README.txt"
cat README.txt
GitFileUpdates.png

Looking at log

These steps look at the logs.

  1. Look at the log
  2. Look at the log for just README.txt
GitLogs.png

Diffs using Commit IDs

THESE STEPS CANNOT BE SIMPLY COPIED AND PASTED SINCE YOUR COMMIT IDS WILL BE DIFFERENT THAN MINE!

  1. Diff current to a previous log, I chose the commit associated with adding .gitignore
  2. Diff 2 previous logs just for README.txt, I chose the commits associated with the first commit & adding Update1 to README.txt
git diff 6c4366f6d8b52587f7ec589a62498a14cfce720a
git diff 2fb90d8896af728e1ac6d2933812d5c78d8b8be1
  c38fc7a7f81a13dfe80ef7129e2294f1b6407608
git diff 2fb90d8896af728e1ac6d2933812d5c78d8b8be1
  c38fc7a7f81a13dfe80ef7129e2294f1b6407608 README.txt
GitDiffs.png

Merging

These steps show how to merge when files are modified in 2 repositories. This steps assumes the committed, but not pushed changes to learningGit that was done in the Diff against an Old Version section.


Make updates to learningGit2

These steps make updates to README.txt in learningGit2.

  1. Move to learningGit2
  2. Look at README.txt
  3. Make update to README.txt
  4. Stage the update
  5. Commit the update
  6. Push the update
cd ~/code/learnGit/learningGit2
cat README.txt
echo "Update from learningGit2" >> README.txt
git add README.txt
git commit -m "Add Update1 to README.txt"
git push
GitUpdate2.png

Pull Updates to learningGit

  1. Move to learningGit
  2. Look at README.txt
  3. Try to push (fails)
  4. Pull the latest updates
cd ~/code/learnGit/learningGit
cat README.txt
git push
git pull
GitMerge1.png

Reslove the Merge

  1. Look at the file with the failed merge
  2. Resolve the merge (by hand)
  3. Look at the merged file
RESOLVE THE MERGE USING YOUR TEXT EDITOR
cat README.txt
xemacs README.txt
cat README.txt
GitMerge2.png

Reslove the Merge

  1. Look at the status (notice the ~ file in my directory, an update to .gitignore will remove that)
  2. Stage the resolution
  3. Commit the resolution.
    Here I did not specify the -m.
    The default editor will open with a default log message, just save and close it to use that.
  4. Push the final version
git status
git add README.txt
git commit
git push
GitMerge3.png

Look at the log

  1. Look at the log
git log
GitMerge4.png


Undoing Commits

These steps show how to undo commits, by using revert. It creates a new commit that undoes the specified commit.


Make updates to learningGit

These steps make updates to README.txt in learningGit.

  1. Make update to README.txt
  2. Stage the update
  3. Commit the update
  4. Push the update
echo "Another Update" >> README.txt
git add README.txt
git commit -m "Another Update to README.txt"
git push
GitAnotherUpdate.png

Look at the modified file

  1. Look at the modified file
cat README.txt
GitAnotherUpdateFile.png

Revert the Commit

  1. Undo the last commit.
  2. Save & Exit the editor with the default commit message.
  3. Look at the log
  4. Push the revert.
git revert HEAD
git log
git push
GitRevert.png

Look at the reverted file

  1. Look at the reverted file
cat README.txt
GitRevertedFile.png