Skip to content

git use and best practices

Catharine McGhan edited this page Jun 28, 2017 · 6 revisions


Table of Contents

git procedures / use:

General git procedure is as follows:

  • initialize or fork the repository you want to make or change through the github.com interface
  • log into your Ubuntu machine and open a Terminal window (Ctrl-Alt-T) to edit the files:
    • Initially (first-time only), git clone the repository you care about to your computer, e.g.,
      • cd ~
      • mkdir -p github_pulls && cd github_pulls
      • git clone -b ubuntu-16.04-xenial https://github.com/cmcghan/vagrant-rss.git
  • make your changes to your files, e.g.,
    • cd ~/github_pulls/vagrant-rss
    • gedit README.md &
  • when you are finished editing what you need to edit for a piece of your work...
    • go to the top folder of your checkout
      • cd ~/github_pulls/vagrant-rss
    • add your changes to your local git repository copy ('stage' your changes); interactive changes are recommended, try:
      • git add -i
    • once you've added everything, use `git commit -m "your message here"` to add messages, e.g.
      • git commit -m "made changes to README documentation"
    • at this point, you can edit more and then repeat your `git add` and `git commit` steps...
    • ...or you can push your changes to the github.com repositories, via:
      • cd ~/github_pulls/vagrant-rss
      • git push
      • note that the website (github.com) will ask for your GitHub username and password before pushing the locally-committed changes to the server

To include/upload empty directories, use:

touch .keep
in the folder(s) you wish to be uploaded to github, then do a `git add` to those directories(/the `keep` files within each of those subfolders).

To prevent some files from ever being added to a repo, create:

touch .gitignore
in the top folder of your git checkout, then `git add` that file to the repository, commit it, and push it. To add some common exclusions of filetypes you probably don't want in your repo (e.g., see [link](https://gist.github.com/octocat/9257657)), try:

*.pyc
*~
.DS_Store
*.bak
.vagrant/*
*.exe
*.dll
*.o
*.so


Advanced git procedures:

Changing temporarily to an older commit, then back again

If you just want to try things out in an older commit, try:

git clone https://github.com/your_username/reponame.git # this is your fork of the repo
cd reponame
git log # use this to figure out which commit you want, and to see its hash value
git checkout b8905e8002d29a94f2caa26c6e4c9398bcfda15b # replace the hash here with one of the hashes that the 'git log' command gave you

One you're done trying things out, to change it back to the HEAD of the branch (e.g., master), try:

git checkout master

Updating a fork of another repository to include new branches from the original repo:

This needs to be done at the Linux commandline -- github doesn't seem to have a good way of pulling updated remote branches into a fork via the http/web interface.

First, get a copy of your github repo fork on your machine, connect your repo to the original source repo, and get the info you need for updating your repo:

git clone https://github.com/your_username/reponame.git # this is your fork of the repo
cd reponame
git remote -v # shows origin vs. upstream repos, likely no upstream yet exists
git remote add upstream https://github.com/the_account_you_forked_from/reponame.git
#git remote -v # now you should see a list of both "origin" (your) and "upstream" (their) branches
#git branch # shows your current branch(es)
#git branch -r # shows branches your repo currently knows about
git fetch upstream # fetches all info on and in the upstream branches from 'the_account_you_forked_from/reponame'
#git branch -r # should be updated
# ...we may need 'git fetch -p' to prune no-longer-extant branches at some point...?
To update a pre-existing branch in your fork with info from the upstream repo ('the_account_you_forked_from/reponame'):
git fetch upstream # make sure you've got the most up-to-date info
git checkout master # check out your master branch
git merge upstream/master # this will pull commits from the_account_you_forked_from into your local master branch
git push # push the changes to your forked repo on github
To add a new branch from the upstream repo ('the_account_you_forked_from/reponame') to your repo ('your_username/reponame'):
git fetch upstream # make sure you've got the most up-to-date info
git branch -r # lets you see the branches in the upstream repo ('the_account_you_forked_from/reponame')
              # that you do and don't have; let's say here that your fork doesn't have the new branch "newdevel"
git checkout -b newdevel upstream/newdevel # make a new branch in your local repo ('newdevel') from the upstream
git push -u origin newdevel # -u "sets up tracking to the specified remote" (here, that's your fork "origin")
You'll need to perform this update process for every new branch in the upstream repo, to get them into your fork.

Once you have a branch from the upstream repo in your fork, though, you can update those branches from the github http/web interface using the pull request system, as usual.

References: link, link, link, link

Other good references: