Skip to content

lahoffm/github_help

Repository files navigation

Atlanta BEST scratchpad of Github tips

Installing git

  1. Get a Github account
  2. Download latest version of Git
    • For Windows, I like using git bash. Download it here.
    • For Mac, download git here and you can run it from the Terminal.
    • For Linux, install git here.
  3. Do the setup steps described here
  4. Set up credentials, to avoid annoyance of entering password each time you connect to Github
    • Not sure how to do this step-by-step on Windows and Mac.

Nice tutorials & guides

Nice cheatsheets

Simple pipeline (best practice: don't commit to master, commit to branches then merge into master)

git status
git add -A - track files in staging area for commits
git diff - see changes
git commit -am "message" - commit tracked files to local computer.
git commit -a -m "Main commit message" -m "Second paragraph with more details" - commit multiple paragraphs.
git push origin master - sync local master branch to online repo's master branch Explanation

Working with branches

git checkout -b mybranch - switch to branch (-b is to create & checkout in 1 step)
git branch - list branches in repo and current branch (-a option will also show remote-tracking branches)
git add -A - if files were staged in master they are also staged in branch but good to do in case new files are made
git commit as usual within the branch
git push origin mybranch - sync local branch mybranch to online repo's branch mybranch
git merge mybranch --no-ff --m "merging mybranch into current branch" - mybranch = name of branch you want to merge with current branch (such as master). Make sure you're in the branch you want to merge to! --no-ff - no fast-forward (can add or reduce confusion on case-by-case basis)

  • Solve merge conflict
    • Open file with conflict
    • Delete conflict marker lines containing <<<<<<<, =======, >>>>>>>
    • Make the changes you want to see in the final merge
    • Save file
    • git add .
    • git commit -m "resolved merge conflict"
  • Solve merge conflict during pull requests

git branch -d mybranch - delete local branch
git branch -rd upstream/master - delete remote-tracking branch locally. Doesn't delete the remote branch on Github.
git push origin --delete mybranch - delete remote branch
git push origin master - don't forget to push "the branch that you merged into" to the remote too!
gitk --all - visualize commit tree

gitk

Undoing commits (Only use locally)

  • Restoring single file locally HEAD - most recent commit, HEAD~1 = one commit before HEAD (read the docs on how HEAD pointers work) git log --oneline -5 - see unique identifiers for last 5 commits
    git log --patch src/helloworld.py - narrow down which commit you want to restore (gives list of SHAs - Secure Hash Algorithm values uniquely identifying the commit)
    git checkout c6fab02 src/helloworld.py - checkout a file from the commit whose SHA started with c6fab02
    git commit –a –m “restore helloworld.py from commit c6fab02” - commit the file you checked out, which was from an earlier commit
  • Restore everything locally to where it was after a prior commit (use with caution) git reset --hard c6fab02
  • Make a new branch starting at a previous commit
    git checkout c6fab02; git checkout –b mybranch
  • Undoing a merge
    • Haven't tried this but here's two sites
  • Restore after pushing to remote repo
    git revert - but safest way is to just fix the bad code locally and push a new commit

Other useful commands

git remote -v - remote URLs (such as origin)
git ls-remote - references in remote repo
git ls-tree -r master - list all tracked files in branch named "master"
git tag, git push origin --tags, etc. - tag stable versions of your code and push them online so people can download "Release v1.5" for example

How to contribute to group project repos

Explanations

General fork-merge workflow (Source)

  1. Fork the project.
  2. Create a topic branch from master.
  3. Make some commits to improve the project.
  4. Push this branch to your GitHub project.
  5. Open a Pull Request on GitHub.
  6. Discuss, and optionally continue committing.
  7. The project owner merges or closes the Pull Request.

Example fork-merge pipeline

  • Fork the group project repo (in browser)
  • cd to a folder in command line
  • git clone "https://github.com/my-username/my-reponame.git" - copy forked repo to your computer
  • cd my-reponame - do all subsequent git commands from the repo's local folder
  • git remote add upstream https://github.com/group-username/group-reponame.git - track a remote repo called upstream. This should be the group project repository that you just forked.
  • git remote -v - shows origin is the repo that you forked, the URL should have your username. Everything you push to origin will go to a repo under your username. There should also be a remote called upstream.
  • git checkout -b mybranch - make a new branch
  • Write code, make some commits to mybranch.
  • Make sure your local master and mybranch are always up-to-date with the upstream/master branch!
    • Get changes from upstream/master branch into local master branch
      • git checkout master - subsequent bullets assume you're in master branch.
      • Method 1: git pull -r upstream master - Pull upstream master branch into currently checked out local branch (should be master branch). -r option is to rebase (otherwise it would do a merge).
      • Method 2: safer than Method 1 because you can examine changes before merging
        • git fetch upstream master - fetch master branch of upstream repo but don't merge or rebase yet, in other words "update my local copy of upstream/master branch"
        • gitk --all, git diff master..upstream/master, git log --oneline master..upstream/master - See differences between local master and fetched upstream/master
        • git merge upstream/master - merge fetched upstream/master branch into currently checked out local branch (should be master branch).
      • Now, local master branch is up-to-date with upstream repo's master branch.
    • Merge those changes to mybranch, so it stays up-to-date with upstream/master branch.
      • git checkout mybranch - subsequent bullets assume you're in mybranch
      • git diff master..mybranch - difference from master to mybranch. For example, if a commit on mybranch added "123", it shows up as +123, meaning "123 added, relative to master".
      • git merge master - merge master into mybranch.
      • Resolve any merge conflicts. Try to avoid edits conflicting with new features in upstream/master so your feature can be pulled seamlessly into main codebase later.
    • Now, your local master and the group project repo's master (upstream/master) are identical, and mybranch has successfully incorporated the latest state of the codebase, plus your new feature.
  • git push origin master; git push origin mybranch - push master and mybranch to your own repo.
  • In browser, initiate pull request from mybranch when it's ready to be merged into the group project's master branch (upstream/master).
  • Monitor comments in pull request, make more commits/comments as needed until they merge and close pull request.
  • Pull the updated upstream/master back into your local master branch and delete mybranch, since your work is now merged to the main codebase.

About

Lukas's scratchpad of Github tips

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages