Github documentation
Git commands;
- clone clone a repository from Github to my computer.
- add track files & changes in Git. ( use git add . to track and save all changes in repo).
- commit save files to Git.
- push upload Git commits to Github.
- pull download changes from Github to my computer (the opposite to push).
Creating a new repository in Github
- Go to Github
- Click plus in top right and new repository
- Name repo, set privacy settings and click create
- Click creating a new file
- Name file, README.md
Pulling a repo from Github to VS Code
- Open VS Code
- Go to the terminal
- In Github, go to repo, click code and copy clone HTTPS link
- Back in terminal, go to the folder where I want to save repo by using cd (folder name)
- Write git clone (clone link)
- In terminal, move into git folder by typing cd (name of folder)
Saving changes to Github
- In terminal, write git status to check status of what has not been saved or tracked.
- To save/track, write git add . to save and track all changed in repo.
- To commit changes, write git commit -m "(commit message goes here)" -m "(description goes here)"
- To push changes, write git push origin master (origin is the location of the git repo & master is the name of the branch to push to)
Creating a repository locally
- In terminal, write cd to go to the folder of the project.
- To initialize a new repo, write git init or git config --global init.deafultbranch {name}
- Save/track, write git add . to save and track all changed in repo.
- Commit changes, write git commit -m "(commit message goes here)" -m "(description goes here)"
- Go to Github and make a new repository.
- Copy HTTPS or SSH link for repo and write in terminal git remote add origin (paste Github link)
- Check any connected repository by writing git remote -v
- To push changes, write git push -u origin master (after creating an upstream with -u we can then just write git push in the future)
Github Workflow
- Write code
- Stage changes git add .
- Commit changes git commit
- Add & commit changes git commit -am (commit title) (only works for modified files, not newly created files)
- Push changes git push
- Make pull request
Git branching
- Master branch is where all the main code lives.
- Create new branches when creating new features to applications to test before merging to master or for bug fixing.
- Branches are like staging environments before merging to master.
- Type git branch to see what branches there are.
How to use Git branching
- git checkout (name of branch) to change between branches.
- git checkout -b (name of branch) to create a new branch.
- git diff -b (name of branch) to compare changes of 2 branches.
- git merge to merge branches locally (only used to merge all local branches to local master if other people are constantly merging to master in Github)
- Once changes are pushed, open a pull request in Github.
- Review pull request and then merge changes to the master branch.
- git pull to pull down the changes from Github.
- git branch -d (name of branch) to delete a branch.
Branch structure
Permanent branches
- Master
- Dev
Temporary branches
- New feature branches
- Bug fixing branches
Branch conflicts
When a team is working on different branches at the same time, and pushing changes to the master branch, sometimes the same files get modified and Github does not know which version to keep, which creates a conflict.
- Unable to switch branches if multiple branches have conflicting files. (changes need to be commited first)
- git merge to merge branches locally (only used to merge all local branches to local master if other people are constantly merging to master in Github)
- If conflict emerges when merging branches locally, review conflicts in VS Code and save changes.
- Commit changes after fixing conflict issues.
Undoing mistakes in Github
- git reset undoes a git staging.
- git reset HEAD~1 undoes a commit.
- git log to see a log of all commits.
- git reset (commit code from log) goes unstage to an older commit.
- git reset --hard (commit code from log) removes changes to an older commit.
Forking in git
Forking code is cloning someone elses public repo from Github.
List of sources
- Colt Steeles web
developer bootcamp 2021
- Intro to Github