My Git Notes

This is just a collection of the notes I have jotted down for myself as I learned how to use Git. I have tried to make sure they are valid, and that the language is understandable, though not necessarily proper 'Git Speak'. I don't think anything is catastrophically wrong, but please don't rely on them for a production machine, or anything you haven't got backed up.

If something really bad happens, feel free to visit my Feedback page and give me a piece of your mind. I won't promise I'll know what happened, I am a newbie myself, but I'll at least grieve with you.

For any of you Git gurus out there, I hope you'll let me know if I'm doing bad things. Any help is appreciated.


Creating Two Remote GIT Repositories that Sync with a Master

  1. Create the local repo1.
    • mkdir <repo_dir>
      cd <repo_dir>
      git init
  2. Add content to the repo1 and commit it.
    • git add .
      git commit -m "comment"
  3. Create a bare repository on the server.
    • git init --bare --shared
    • You need the --shared to have git deal with the permissions on the bare repo.
  4. Change to the local repo1 created in step 1.
    • cd <repo directory>
  5. If there is an "origin" defined, change it with:
    • git remote rm origin
      git remote add origin <path to the bare repository>
    • If it complains about removing "origin", you can always specify the path on the git command line.
    • If you cloned a repo, the origin is set to the source by default.
  6. Push the local repo1 to the bare repo created in step 3.
    • git push origin master
  7. Create a new repo2 by cloning the bare repo.
    • git clone <path to bare repo>
    • If done over ssh and the current dir is where the repo is, use "." as the path. i.e. username@192.168.1.10:.
    • Note: the repo is placed in a directory within the working dir with the name of the bare repo on the server.
  8. Edit something in the new repository and commit the changes.
    • git add .
      git commit -m "comment"
  9. Push the changes back to the bare repo.
    • git push
    • Because the repo was cloned from the bare repo, it remembers the origin and you don't have to specify it.
  10. Retrieve the latest updates from the server into a temp branch.
    • git fetch
    • Fetches the changes in the repo but does not merge them into your local repo.
    • git pull fetches and merges.
  11. View what is changed between the fetch and your repo.
    • git diff ... <temp fetch branch>
    • Check the output of "git fetch" to see if is fetched into FETCH_HEAD or origin, etc.
  12. View the actual files in the new fetch.
    • git checkout <temp fetch branch>
    • Switches to the fetched branch and view the newly fetched files.
    • Editing changes are not saved when you change out of the temp branch.
  13. Merge the changes into your main repo.
    • git checkout <main branch>
    • git merge <temp fetch repo>
    • Accepts the changes and update the conflicted files in your repo.
    • If there are conflicts between the fetched and local files, "git merge" will tell you and add both.
    • cat newfile
      • This is the second line from j
        This is the third line from j
        <<<<<<< HEAD
        This is the forth line from d
        =======
        This is the forth line from j - This conflicts with d!
        >>>>>>> FETCH_HEAD
      • Edit the file, add it and then commit it.
Go to page top

Creating and Merging Branches

  1. Create a local repo.
    • git init <repo_name>
      cd <repo_name>
  2. Add and commit something to the repo to create a master branch.
    • echo "Hello World" > <file_name>
      git add .
      git commit -m "Created Hello World file <file_name>"
  3. Create a new branch and change into it.
    • git checkout -b <new_branch>

    • You now have a second version, or 'timeline' of the repo directory that is active when you have it checked out. At this point the new_branch and the master branch are identical. Any changes to either splits the repo into two separate versions of the entire repo that diverge at the point any changes are made.
    • Any changes made while a branch is checked out are only visible when that branch is checked out. To synchronize changes between branches, you need to merge. (step 6)
  4. Add or change something in the new branch.
    • echo "howdy right back at you" >> <file_name>
      git add .
      git commit -m "Answered Hello World in <file_name>"
  5. Change to the master branch.
    • git checkout master
  6. Merge the changes to the new branch to the master branch.
    • git merge <new_branch>
    • The divergent 'timeline' of the two branches rejoin to identical versions of the repo.
Go to page top

Checking Out Specific Files

  1. git checkout <repo or branch> <path to file>
  2. Ensure that the file is correct.
  3. git commit -m "Fixed conflict by copying remote file over local file"
Go to page top

Shortcut to Fetch or Push a Specific Branch

  1. In .git/config, add or edit a [remote "<repo shortcut>"] section for the remote you want to configure fetch or push for.
  2. In the remote section, add or edit the following:
    • [remote "<shortcut name>"]
         url = jim@192.168.1.10:/tmp/test_repo3
         fetch = +refs/heads/<remote branch>:refs/remotes/<remote repo name>/<temp branch on local>
         push = +refs/heads/<local branch>:refs/remotes/<local repo name>/<temp branch on remote>
                     
Go to page top

Deleting Files

Go to page top

Recorded Reflections

Home    .     About     .     Feedback     .     Help
Previous Section
Pevious Section

'Writing and photography define the present by interpreting the past'
For help using this site, please click Help. Thanks, Jim.