Git (Hub) Training
Git (Hub) Training
Installing git
Windows: https://git-scm.com/download/win
○ Updates information from the main repo so you know what branches you have
Remotes
● Link between git and Github
○ Lets you get (fetch) code from github and send (push) code to github
● Naming conventions
○ upstream - the repo you forked
■ Usually frc1678
■ In this case, ccsaposs
○ origin - your fork
○ <first-name> - buddy forks (other people in your app group)
■ e.g. “carl”
● List all remotes: git remote
First change
● Open SPR.py
○ Change the range for “randint” from (1,5) to (1,8)
○ Save
● Check git status, check git diff
> git status
On branch master
Your branch is up to date with 'origin/master'.
modified: SPR.py
no changes added to commit (use "git add" and/or "git commit -a")
Second change
● Open server.py
○ Change the time.sleep() from 5 to 15
○ Save
● Check git status, check git diff
> git status
On branch master
Your branch is up to date with 'origin/master'.
modified: SPR.py
modified: server.py
no changes added to commit (use "git add" and/or "git commit -a")
First commit
● Stage SPR.py and server.py for commit
○ When we commit, only the staged changes will be included
○ git add SPR.py
○ git add server.py
> git status
● Check git status On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: SPR.py
modified: server.py
First commit
● Wait! We don’t want SPR.py and server.py in the same commit!
○ They’re separate changes
○ We want to keep our commits as small as possible
○ Should the following be separated?
i. Moving code around, updating comments
ii. Renaming a file, deleting commented out code
iii. Changing an import in two different files
● We want to unstage server.py
○ git reset HEAD server.py
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
First commit
● git status
> git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: SPR.py
modified: server.py
First commit
● Create a commit
○ git commit
○ Type a good commit message
■ Subject line - 50 characters or less
■ Extended description should be
included unless the change is very
simple
● Each line should be 72
characters or less
○ Combines the master branch of the upstream remote with your HEAD
● Pulling
> git pull upstream master
○ Shorthand for the above two commands, will merge remote code with local code
Branches
● Create branch
Tip:
> git branch [branch-name]
> git checkout -b [branch-name]
● Switch to branch
will create a new branch and switch to it
> git checkout [branch-name]
● List branches
> git branch
● Delete branch
> git branch -d [branch-name]
Branches
● How do we fix this?
○ What do we need to do?
■ Create a “SPR.py” branch
● Move the 1st and 3rd commits we made to this branch
■ Create a “server.py” branch
● Move the 2nd commit we made to this branch
■ Restore the “master” branch to match upstream
Branches
● Let’s start with by moving the 2nd commit to the “server.py” branch
● We need to start our branch based on upstream/master
○ git checkout upstream/master
○ Create and checkout a new branch called “server.py”
○ To move the commit, we can “cherry-pick” it
Cherry picking!
● Super useful command
> git cherry-pick [commit]
○ Un-commits previous commit, files are still changed and staged for change, but changes are no
longer part of the commit history
● Mixed resetting
> git reset --mixed
○ Goes one step further, unstages commits, but changes are still there in working directory
● Hard resetting ***HERE BE DRAGONS***
> git reset --hard
● Making a PR!
○ On github, navigate to your fork. If you recently pushed, there should be a popup asking if you
want to make a new PR
○ If you didn’t recently push or you’re creating a PR from someone else’s fork:
■ Head to the main repository and click “New Pull Request”. Then specify the fork and
branch you want to use.
○ Good code review will be covered outside of this training
● Adding to a PR
○ Just make new commits and push to the same branch on the same remote, your PR will update
automatically
More notes on pushing
● Pushing from a specific branch
> git push [branch-name]:[branch-name]
○ First branch is what you want to push from, second is what you want to push to
■ Use this if your branch name doesn’t match the branch name on github
● Deleting a branch
> git push :[branch-name]
○ Same as above, but you’re pushing nothing to the branch, wiping it clean
Code review
● Let’s practice our new code review process
○ On github, we have labels to mark if a PR (pull request) needs review
■ On your PR, add the needs review label
● Look at the scout-QR-2019 README.md for a recap of the process
● First, pair up with someone for the buddy review
○ Adding your review to github
■ Click “Files Changed”
■ Click “Review changes”
● After everyone’s done, you’ll pair up with a different person for the peer review
○ Add your review to github
Other remotes
● We want to get code from David’s fork
● First, add his repo as a remote
○ git remote add david https://github.com/daviji/git-training
○ git fetch david
● Let’s checkout his function-rename branch
○ git checkout function-rename
● David’s last commit broke the server
○ How do we fix it?
■ We can’t remove the commit, it’s already on github
■ Let’s revert it
Reverting
● Revert to commit/branch
> git revert [commit ref]
modified: SPR.py
no changes added to commit (use "git add" and/or "git commit -a")
Let’s make one last change
● Oh no! We’re on the wrong branch!
● How do we take our changes over to a different branch?
○ We haven’t committed yet
○ We can do git stash to save uncommitted changes
●
> git stash
○ “Stores” changes without committing, you can then navigate and apply changes to
wherever. Be careful with this, as it is working with unsaved changes
Finishing up
● Create a new branch to put the stashed changes on
○ What do we need to do first?
● Once you have a branch, use git stash pop to “release” the changes
● Push this to Github and create a PR
○ We won’t review for this one
Professionalism with git(hub)
● All commit messages should be strictly professional
○ Both in the subject line and extended description
○ We want the information to be concise and as useful as possible
○ Includes PR commits
● Code review/testing messages
○ When approving, not as strict
■ Include the necessary information first
● (e.g. “Buddy review completed” or “User testing successful”)
■ You can (but do not need to) add other comments after
● (e.g. “Nice job!” or “Good to finally get this done!”)
○ When requesting changes, keep the information concise + useful
● Keep in mind that 1678 is the go-to example in FRC for electronic scouting
○ We open-source our code, lots of teams will be able to see these messages
Any Questions?
Feel free to ask me or Carl if anything else comes up