Git with GitHub

Get Started. It's Free
or sign up with your email address
Git with GitHub by Mind Map: Git with GitHub

1. Initial Setup

1.1. Install Software Needed

1.1.1. Windows Only

1.1.1.1. Install git

1.1.1.2. Install github desktop

1.1.1.2.1. GitHub Desktop

1.1.2. Windows with WSL

1.1.2.1. install git

1.1.3. Mac

1.1.3.1. install git

1.2. Create a git key if you will be using git key (most likely on WSL or Mac)

1.2.1. Settings > SSH and GPG keys

1.3. Store your creds

1.3.1. MAC

1.3.1.1. tell git to put your next set of credentials in the OSX keychain

1.3.1.1.1. git config --global credential.helper osxkeychain

1.3.2. Windows

1.3.2.1. https://help.github.com/articles/caching-your-github-password-in-git/

1.3.3. Linux/WSL

1.4. create a directory where you'll store your github repository. (You don't have to store them all in the same place, but it makes sense). EG ~/github or C:\github

1.5. setup w Visual Studio Code

1.5.1. Use GitHub Desktop for clone/push/pull

1.5.1.1. simpler, but not all work in VSC

1.5.2. OR... Using SSH Keys in Visual Studio Code on Windows · cgranade::space

2. Day-to-Day Work

2.1. Oops! Did you start doing work before creating a new branch?

2.1.1. No prob. create the branch. what matters is what branch you are in when you commit

2.2. create a branch and checkout

2.2.1. git checkout -b <branch>

2.2.1.1. this combines

2.2.1.1.1. git branch <branch>

2.2.1.1.2. git checkout branch

2.3. do work

2.3.1. add changes to git (changed files, new files, new dir)

2.3.1.1. git add <file or parent dir> [<file or dir> <file or dir>...]

2.3.1.1.1. EG

2.3.1.2. this tells git to track these files for changes

2.3.2. show git changes

2.3.2.1. git diff

2.3.2.2. git status

2.3.2.3. git branch

2.3.3. commit work to the branch

2.3.3.1. git commit -m "<change message>"

2.3.3.2. until you do this, you really haven't done any work in the branch

2.3.4. push committed work to github

2.3.4.1. first time pushing this branch?

2.3.4.1.1. git push -u origin <branch>

2.3.4.2. additional pushes

2.3.4.2.1. git push

2.3.5. someone else made changes in the master branch that you need to pull in

2.3.5.1. git merge origin master

2.3.5.2. git pull

2.3.5.3. frequently, I find I need to do another git merge origin master after the git pull

2.4. Merge to Master & Clean Up

2.4.1. Clean up

2.4.1.1. git status

2.4.1.1.1. should be up-to-date

2.4.1.2. git checkout master

2.4.1.3. git pull

2.4.1.3.1. if you need to pull changes others have made

2.4.1.3.2. git pull origin master

2.4.1.4. git branch -d <old_branch>

2.4.1.4.1. deletes <old_branch>

2.4.1.5. run git diff to confirm your merge to master only changed what you expected

2.4.1.5.1. git --no-pager diff HEAD~ HEAD

2.4.1.6. AUTOMATION: here is a script that I call gitdone that automates these steps:

2.4.1.6.1. branch=$(git branch | grep '*' | cut -f2 -d ' ') if [ $branch == "master" ]; then echo "You are in master! NOT deleting the branch" exit 1; fi git checkout master git branch -d $branch git pull set -o xtrace # this just shows the git diff command for when we copy/paste into a comment git --no-pager diff HEAD~ HEAD set +o xtrace

3. Other Tools

3.1. GitHub GUI

3.1.1. pull request

3.1.2. get approval

3.1.3. merge pull request

3.1.4. delete branch

4. Get Started

4.1. clone a repo

4.1.1. need to do this before you work w a repo

4.1.2. GH Desktop:

4.1.2.1. File > Clone repository...

4.1.3. OR, git clone <url>

4.2. OR, create a new repo

4.2.1. Easiest: Create repo in github & clone

4.2.1.1. Create repo in github

4.2.1.2. clone repo (see above)

4.2.1.3. add files (see Day-to-Day work)

4.2.2. OR, GitHub Desktop:

4.2.2.1. File > New Repository...

4.2.2.1.1. At a minumum, specify:

4.2.2.2. Publish Repository

4.2.2.2.1. Decide if private or not

4.2.2.2.2. Choose the appropriate org

4.2.2.3. add files (see Day-to-Day work)

4.2.3. OR, create a new repo from files that exist already

5. CRAP! I goofed!

5.1. How Do I...

5.1.1. Roll back a pull request

5.1.1.1. This is easy! In the GitHub GUI, go to your pull request, and there is a revert option (or undo or roll back or something like that)

5.1.2. Undo my last commit

5.1.2.1. How can I undo the last commit?

5.1.3. Move a commit to master (that I did not push) to a new branch - or an existing branch? (Yup! I accidentally committed to master!)

5.1.3.1. Move the most recent commit(s) to a new branch with Git

5.1.3.2. more info on undoing a commit: How to With Git: Undo Commit - HostingAdvice.com

6. Pulling from other branches

6.1. Pull latest from master

6.1.1. git merge orgin master

6.2. Pull latest from another branch

6.2.1. git fetch origin other-branch

6.2.2. git merge origin/other-branch

6.2.3. resolve your merge conflicts

6.2.4. git add ... (add your conflict fixes)

6.2.5. git commit ...

6.2.6. Reference

6.3. super article about branching

6.3.1. git: fetch and merge, don’t pull | Mark's Blog

6.4. There is no tracking information for the current branch.

6.4.1. This is a special case: Likely you have branched, made code changes, but have not pushed yet, so everything is local. Git doesn't know that you need updates from the remote master. When you do a git pull (or fetch+merge) it says "There is no tracking information for the current branch." You need to set the upstream to master (or another branch if you're branching from a different branch).

6.4.1.1. git branch --set-upstream-to=origin/<upstream-branch> <your-branch>

7. Diff

7.1. compare two branches

7.1.1. get the URL of the repo, and add /compare/branch1..branch2

7.1.1.1. EG: https://github.com/Repo/Repo-Sub/compare/master...branch

7.2. what changed in this commit?

7.2.1. git show

7.3. more what changed (need to fill in more info)

7.3.1. git log

7.3.2. git whatchanged

8. Point to specific code

8.1. use #L

8.1.1. EG url#L48

9. READ THIS!

9.1. htps://learngitbranching.js.org

9.2. git - the simple guide - no deep shit!

9.3. Frequently Asked Questions About Git

10. Don't forget to create a README!

10.1. othneildrew/Best-README-Template

11. git log

11.1. shows history of branch

11.1.1. sample git log that also saves to an HTML file, so you can work with it later

11.1.1.1. git --no-pager log --date=local --date-order --since='2021-07-01' --until='2021-07-23' --stat=120 --graph -m --decorate --all --color=always| aha --black -w --title "git log" > /tmp/gitlog.html

11.1.1.1.1. Some notes on the parameters used: --no-pager to dump the information without paging through it --date=local to convert all dates to the same time zone (the local timezone of whoever runs the command) --date-order to order the output by date --since & --until to limit the range --stat to show commit changes, =120 forces the width --graph to show branching -m to show files changed at merges --decorate might add some additional information --all to show all merges --color=always to force it to use color when piping to another app