Difference between revisions of "Using Git Example"
(16 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Create new Bare Repository == | == Create new Bare Repository == | ||
− | {| border="1" | + | {| style="width: 1200px" border="1" |
|valign="top"| | |valign="top"| | ||
These steps will create a new bare repository called learningGit in ~/code/learnGit/bareRepo/. | These steps will create a new bare repository called learningGit in ~/code/learnGit/bareRepo/. | ||
Line 23: | Line 23: | ||
This is so you can see how changes don't affect the other repositories unless pushes & pulls are done. | This is so you can see how changes don't affect the other repositories unless pushes & pulls are done. | ||
− | {| border="1" | + | {| style="width: 1200px" border="1" |
|valign="top"| | |valign="top"| | ||
=== Create a Working Repository === | === Create a Working Repository === | ||
Line 62: | Line 62: | ||
This will create a couple of files in the learningGit repository. | This will create a couple of files in the learningGit repository. | ||
− | {| border="1" | + | {| style="width: 1200px" border="1" |
|valign="top"| | |valign="top"| | ||
=== Create the New Files === | === Create the New Files === | ||
Line 81: | Line 81: | ||
echo This repo is for Learning Git. > README.txt | echo This repo is for Learning Git. > README.txt | ||
echo Another file in the repository > README1.txt | echo Another file in the repository > README1.txt | ||
+ | git status | ||
+ | cd ~/code/learnGit/learningGit2 | ||
+ | ls | ||
git status | git status | ||
Line 87: | Line 90: | ||
|- | |- | ||
|valign="top"| | |valign="top"| | ||
+ | |||
===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/. | ||
Line 155: | Line 159: | ||
|} | |} | ||
− | == | + | == Undo File Modifications (uncommitted) in the Repository == |
− | + | In the end, this has no affect on the learningGit repositories. | |
− | {| border="1" | + | {| style="width: 1200px" border="1" |
|valign="top"| | |valign="top"| | ||
=== Modify the Files === | === Modify the Files === | ||
Line 205: | Line 209: | ||
=== Undo the Modifications === | === Undo the Modifications === | ||
This step undoes the modifications to README.txt. | This step undoes the modifications to README.txt. | ||
+ | |||
+ | Your modifications are lost, since they were never stored in the database. | ||
# Look at README.txt | # Look at README.txt | ||
Line 218: | Line 224: | ||
|[[Image:GitUndoMod.png]] | |[[Image:GitUndoMod.png]] | ||
+ | |} | ||
+ | |||
+ | |||
+ | == Remove a File from the Repository == | ||
+ | This will remove a file from the learningGit repositories. | ||
+ | |||
+ | {| style="width: 1200px" border="1" | ||
+ | |valign="top"| | ||
+ | === Remove the File === | ||
+ | These steps removes README1.txt from ~/code/learnGit/learningGit/. | ||
+ | |||
+ | # Look at the current status to see nothing. | ||
+ | # Remove README.txt | ||
+ | # Look at the git status to see the removed file. | ||
+ | # Look at the changes. | ||
+ | git status | ||
+ | ls | ||
+ | rm README1.txt | ||
+ | git status | ||
+ | git diff | ||
+ | |||
+ | |[[Image:GitRemoveFile.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | ===Stage the File Removal === | ||
+ | This step stages the removal of README1.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 rm README1.txt | ||
+ | git status | ||
+ | git diff | ||
+ | |||
+ | |[[Image:GitStageRemoval.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | |||
+ | === Commit the File Removal === | ||
+ | This step commits the removal of README1.txt. | ||
+ | |||
+ | # Commit the file removal | ||
+ | # Look at the git status to see no modified files | ||
+ | git commit -m "Remove unnecessary file, README1.txt." | ||
+ | git status | ||
+ | |||
+ | |[[Image:GItCommitFileRemoval.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Push & Pull the File Removal === | ||
+ | This step pushes the removal of README1.txt to the main repository and pulls it into learningGit2. | ||
+ | |||
+ | # Push the file removal to the origin repository | ||
+ | # Move to learningGit2 | ||
+ | # See what files are there | ||
+ | # Check the status | ||
+ | # Pull the latest changes | ||
+ | # See what files are there | ||
+ | # Check the log | ||
+ | git push | ||
+ | cd ~/code/learnGit/learningGit2 | ||
+ | ls | ||
+ | git status | ||
+ | git pull | ||
+ | ls | ||
+ | git log | ||
+ | |||
+ | |[[Image:GitPushFileRemoval.png]] | ||
+ | |} | ||
+ | |||
+ | == Undo File Removal (uncommitted) in the Repository == | ||
+ | In the end, this has no affect on the learningGit repositories. | ||
+ | |||
+ | {| style="width: 1200px" border="1" | ||
+ | |valign="top"| | ||
+ | === Remove the Files === | ||
+ | These steps remove README.txt from ~/code/learnGit/learningGit/. | ||
+ | |||
+ | # Move to the ~/code/learnGit/learningGit | ||
+ | # Look at the current status to see nothing. | ||
+ | # Remove README.txt | ||
+ | # Look at the git status to see the removed file. | ||
+ | # Look at the changes. | ||
+ | cd ~/code/learnGit/learningGit | ||
+ | git status | ||
+ | rm README.txt | ||
+ | git status | ||
+ | git diff | ||
+ | |||
+ | |[[Image:GitRemoveFile2.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | ===Stage the File Removal === | ||
+ | This step stages the removal of README.txt from ~/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 rm README.txt | ||
+ | git status | ||
+ | git diff | ||
+ | |||
+ | |[[Image:GitStagedRemoveFile2.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Unstage the File Removal === | ||
+ | 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:GitUnstageFile2.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Undo the Removal === | ||
+ | This step undoes the removal of README.txt. | ||
+ | |||
+ | The file removal was never stored in the database. | ||
+ | |||
+ | # Look at the directory contents | ||
+ | # Undo the removal | ||
+ | # Check the status | ||
+ | # Check the diffs | ||
+ | # Look at the directory contents | ||
+ | ls | ||
+ | git checkout -- README.txt | ||
+ | git status | ||
+ | git diff | ||
+ | ls | ||
+ | |||
+ | |[[Image:GitUndoRemoval.png]] | ||
+ | |} | ||
+ | |||
+ | == Ignore Files == | ||
+ | This sets up a .gitignore file in the repository. | ||
+ | |||
+ | {| style="width: 1200px" border="1" | ||
+ | |valign="top"| | ||
+ | === Create Files That Should be Ignored === | ||
+ | These steps creates temp1.o in ~/code/learnGit/learningGit/ and temp2.o in ~/code/learnGit/learningGit2/. | ||
+ | |||
+ | # Move to ~/code/learnGit/learningGit/ | ||
+ | # Create temp1.o | ||
+ | # Check status | ||
+ | # Move to ~/code/learnGit/learningGit2/ | ||
+ | # Create temp2.o | ||
+ | # Check status | ||
+ | |||
+ | cd ~/code/learnGit/learningGit | ||
+ | touch temp1.o | ||
+ | git status | ||
+ | cd ~/code/learnGit/learningGit2 | ||
+ | touch temp2.o | ||
+ | git status | ||
+ | |||
+ | |[[Image:GitCreateoFile.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Accidentally Stage the file, then Unstage === | ||
+ | This step accidentally stages an undesired file due to using the "git add ." and then unstages the file. | ||
+ | |||
+ | # Add everything | ||
+ | # Check status | ||
+ | # Undo the add | ||
+ | # Check status | ||
+ | |||
+ | git add . | ||
+ | git status | ||
+ | git reset HEAD temp2.o | ||
+ | git status | ||
+ | |||
+ | |[[Image:GitStageoFile.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Ignore .o Files === | ||
+ | Adds a .gitignore to ignore .o files. | ||
+ | |||
+ | # Create a .gitignore file containing *.o to ignore .o files | ||
+ | # Check status | ||
+ | # Stage the .gitignore file | ||
+ | # Commit the .gitignore file | ||
+ | # Push the .gitignore file | ||
+ | # 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 | ||
+ | |||
+ | |[[Image:GitGitIgnore.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Pull .gitignore into other repository === | ||
+ | Pulls the .gitignore file into the other repository | ||
+ | |||
+ | # Move to ~/code/learnGit/learningGit/ | ||
+ | # Check status | ||
+ | # Pull in the changes | ||
+ | # Get the status | ||
+ | |||
+ | cd ~/code/learnGit/learningGit/ | ||
+ | git status | ||
+ | git pull | ||
+ | git status | ||
+ | ls -a | ||
+ | |||
+ | |[[Image:GitPullGitIgnore.png]] | ||
+ | |} | ||
+ | |||
+ | == Diff against an Old Version == | ||
+ | These steps show how to look at an old version of a file | ||
+ | |||
+ | {| style="width: 1200px" border="1" | ||
+ | |valign="top"| | ||
+ | === Make updates === | ||
+ | |||
+ | These steps make updates to README.txt. | ||
+ | |||
+ | # Look at README.txt | ||
+ | # Make update to README.txt | ||
+ | # Stage the update | ||
+ | # Commit the update | ||
+ | # Make another update to README.txt | ||
+ | # Stage the update | ||
+ | # Commit the update | ||
+ | # 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 | ||
+ | |||
+ | |[[Image:GitFileUpdates.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Looking at log === | ||
+ | |||
+ | These steps look at the logs. | ||
+ | |||
+ | # Look at the log | ||
+ | # Look at the log for just README.txt | ||
+ | |||
+ | |[[Image:GitLogs.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | |||
+ | === Diffs using Commit IDs === | ||
+ | |||
+ | THESE STEPS CANNOT BE SIMPLY COPIED AND PASTED SINCE YOUR COMMIT IDS WILL BE DIFFERENT THAN MINE! | ||
+ | |||
+ | # Diff current to a previous log, I chose the commit associated with adding .gitignore | ||
+ | # 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 | ||
+ | |||
+ | |[[Image: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 [[Using_Git_Example#Diff_against_an_Old_Version|Diff against an Old Version]] section. | ||
+ | |||
+ | |||
+ | {| style="width: 1200px" border="1" | ||
+ | |valign="top"| | ||
+ | === Make updates to learningGit2 === | ||
+ | These steps make updates to README.txt in learningGit2. | ||
+ | |||
+ | # Move to learningGit2 | ||
+ | # Look at README.txt | ||
+ | # Make update to README.txt | ||
+ | # Stage the update | ||
+ | # Commit the update | ||
+ | # 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 | ||
+ | |||
+ | |[[Image:GitUpdate2.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Pull Updates to learningGit === | ||
+ | |||
+ | # Move to learningGit | ||
+ | # Look at README.txt | ||
+ | # Try to push (fails) | ||
+ | # Pull the latest updates | ||
+ | |||
+ | cd ~/code/learnGit/learningGit | ||
+ | cat README.txt | ||
+ | git push | ||
+ | git pull | ||
+ | |||
+ | |[[Image:GitMerge1.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Reslove the Merge === | ||
+ | |||
+ | # Look at the file with the failed merge | ||
+ | # Resolve the merge (by hand) | ||
+ | # Look at the merged file | ||
+ | |||
+ | RESOLVE THE MERGE USING YOUR TEXT EDITOR | ||
+ | |||
+ | cat README.txt | ||
+ | xemacs README.txt | ||
+ | cat README.txt | ||
+ | |||
+ | |[[Image:GitMerge2.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Reslove the Merge === | ||
+ | |||
+ | # Look at the status (notice the ~ file in my directory, an update to .gitignore will remove that) | ||
+ | # Stage the resolution | ||
+ | # 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. | ||
+ | # Push the final version | ||
+ | |||
+ | git status | ||
+ | git add README.txt | ||
+ | git commit | ||
+ | git push | ||
+ | |||
+ | |[[Image:GitMerge3.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Look at the log === | ||
+ | |||
+ | # Look at the log | ||
+ | |||
+ | git log | ||
+ | |[[Image:GitMerge4.png]] | ||
+ | |} | ||
+ | |||
+ | |||
+ | == Undoing Commits == | ||
+ | These steps show how to undo commits, by using revert. It creates a new commit that undoes the specified commit. | ||
+ | |||
+ | |||
+ | {| style="width: 1200px" border="1" | ||
+ | |valign="top"| | ||
+ | === Make updates to learningGit === | ||
+ | These steps make updates to README.txt in learningGit. | ||
+ | |||
+ | # Make update to README.txt | ||
+ | # Stage the update | ||
+ | # Commit the update | ||
+ | # Push the update | ||
+ | echo "Another Update" >> README.txt | ||
+ | git add README.txt | ||
+ | git commit -m "Another Update to README.txt" | ||
+ | git push | ||
+ | |||
+ | |[[Image:GitAnotherUpdate.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Look at the modified file === | ||
+ | |||
+ | # Look at the modified file | ||
+ | |||
+ | cat README.txt | ||
+ | |||
+ | |[[Image:GitAnotherUpdateFile.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Revert the Commit === | ||
+ | |||
+ | # Undo the last commit. | ||
+ | # Save & Exit the editor with the default commit message. | ||
+ | # Look at the log | ||
+ | # Push the revert. | ||
+ | |||
+ | git revert HEAD | ||
+ | git log | ||
+ | git push | ||
+ | |||
+ | |[[Image:GitRevert.png]] | ||
+ | |- | ||
+ | |valign="top"| | ||
+ | === Look at the reverted file === | ||
+ | |||
+ | # Look at the reverted file | ||
+ | |||
+ | cat README.txt | ||
+ | |||
+ | |[[Image:GitRevertedFile.png]] | ||
|} | |} |
Latest revision as of 17:51, 12 July 2011
Create new Bare Repository
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 RepositoryThese steps will create a new working repository called learningGit in ~/code/learnGit/ that is a clone of ~/code/learnGit/bareRepo/learningGit.
cd ~/code/learnGit git clone ~/code/learnGit/bareRepo/learningGit cd learningGit ls -a |
|
Create a Second Working RepositoryThese steps will create a second working repository called learningGit2 in ~/code/learnGit/ that is a clone of ~/code/learnGit/bareRepo/learningGit.
cd ~/code/learnGit git clone ~/code/learnGit/bareRepo/learningGit learningGit2 cd learningGit2 ls -a |
Add Files to the Repository
This will create a couple of files in the learningGit repository.
Undo File Modifications (uncommitted) in the Repository
In the end, this has no affect on the learningGit repositories.
Remove a File from the Repository
This will remove a file from the learningGit repositories.
Undo File Removal (uncommitted) in the Repository
In the end, this has no affect on the learningGit repositories.
Ignore Files
This sets up a .gitignore file in the repository.
Diff against an Old Version
These steps show how to look at an old version of a file
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.
Undoing Commits
These steps show how to undo commits, by using revert. It creates a new commit that undoes the specified commit.