These steps will create a new bare repository called learningGit in ~/code/learnGit/bareRepo/.
Create the containing directory
Go to the correct directory
Create a new bare repository
Move into the new repository
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
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.
Move to the appropriate directory
Clone the initial repository
Move into the working directory
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
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.
Move to the appropriate directory
Clone the initial repository
Move into the working directory
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
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/.
Move to the ~/code/learnGit/learningGit
Look at the current status to see nothing.
Create README.txt
Create README1.txt
Look at the git status to see 2 untracked files.
Move to the learningGit2 directory
Check that the files are not there
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.
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/.
Move back to the learningGit directory
Stage the files
Look at the git status to see 2 staged files.
Move back to the learningGit2 directory
Try to pull files
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.
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/.
Move back to the learningGit directory
Commit the files
Look at the git status to see no uncommitted changes.
Move back to the learningGit2 directory
Try to pull files
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.
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/.
Move back to the learningGit directory
Push the files without specifying the remote or branch (fails)
Push the files specifying the remote and the branch (succeeds)
Look at the git status to see no uncommitted changes.
Move back to the learningGit2 directory
Try to pull files
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.
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/.
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
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
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
Undo the Modifications
This step undoes the modifications to README.txt.
Your modifications are lost, since they were never stored in the database.
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.
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
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
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
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
Look at the log
Look at the log
git log
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.