Brought to you by
Laurel Taylor
at
> mkdir ~/Documents/code
> cd ~/Documents/code/
> git clone <pasted link>
> git clone https://github.com/laurel-taylor/git-training.git
> cd git-training
> git branch FS-101-branch-name origin/main
> git checkout FS-101-branch-name
1) create a branch
2) check out that branch
3) make your changes in an IDE
Your local repo
Remote repo
> git status
Your changes should be in red ("unstaged")
> git diff
See what changes you've made
> git add .
> git status
Your changes should be in green ("staged")
> git commit
OMG i'm in VI what do??
(ESC :q!)
> git commit -m "FS-101 | my change description"
> git log
You can see your commit
(or your list of commits on this branch)
> git fetch
If origin/main has not been updated, you are free to push!
> git push origin FS-101-my-branch
Note the SPACE between origin and the branch name.
main
main
I have a branch up on origin, but I needed to rebase... now it rejects my push.
> git push -f origin FS-101-my-branch
NOTE if you are using a shared branch (with other people) this can be dangerous if there's no communication!
1) Create a branch that you can work off of, and push it to origin. Only one person should do this, and it should just be a clone of master.
> git fetch
> git checkout -b our-shared-branch origin/main
> git push origin our-shared-branch
2) "Hey guys, I made a branch called 'our-shared-branch'. It's up on origin."
> git fetch
> git checkout -b our-shared-branch origin/our-shared-branch
3) When pushing changes, always fetch and see if main or the shared branch has been updated
> git fetch
4) If main or the shared branch has been updated, rebase off the SHARED branch FIRST, THEN main, then IMMEDIATELY force push:
> git fetch
> git rebase origin/our-shared-branch
> git rebase origin/main
> git fetch #has it updated again????
> git push -f origin our-shared-branch
The worst thing you can do is overwrite another person's changes by force pushing.
If you (and everyone else) rebase from the shared branch this will be avoided!
VS Code works
IntelliJ works better
I also like diffmerge
(resolving merge conflicts is one thing that is garbage through the terminal)
Fix your conflicts
> git rebase origin/main
> git rebase origin/shared-branch
> git add .
> git rebase --continue
Fix your conflicts
(repeat)
Get me out of this
> git rebase --abort
When things go wrong
> git branch
> git status
> git log
> git reflog
These are all things you can do that won't affect any of yours or others' files. Use them as often as you like, to figure out where you are and where you've been.
> git checkout -- filename.js
If the file is unstaged (red):
> git reset --soft HEAD~1
> git push -f origin branch-name
You can also "unstage" changes with
git reset HEAD
If you accidentally committed to a remote branch, or a shared branch
> git revert
> git fetch
> git reset --hard origin/main
Boom, everything is gone. This means all your commits, all your staged and unstaged changes for this branch.
> git reflog
Reflog is your best friend.
> git reset --hard u84938ru89u47839
You have to go talk to them, see if they can find the commit in their reflog, and they can push it up to the branch.
> git reflog
> git reset --hard jfiu8r94wrrf9r9w8
> git fetch
> git rebase origin/our-shared-branch
> git rebase origin/main
> git push -f origin our-shared-branch
Don't forget to say you're sorry :)
Tough luck my friend.
If you've stashed, staged, or committed you can get your changes back.
But not if you've never told git about them.
(You might have the file open in an IDE? try using undo? good luck :) commit next time!)
> open ~/.gitconfig
[alias]
co = checkout
s = status
unstage = !echo "git reset HEAD" && git reset HEAD
explode = !git fetch origin && git reset --hard origin/main
undo = reset --soft HEAD~1
ac = !git add -A && git commit
alias = config --get-regexp ^alias\\.
Make a new branch and check it out:
> git checkout -b new-branch-name origin/main
[core]
editor = open
> open ~/.gitconfig
Now when you say "git commit" you won't get VI as your default editor.
Maybe try diffmerge
> git config --global diff.tool diffmerge
> git config --global difftool.diffmerge.cmd
"/usr/local/bin/diffmerge \"\$LOCAL\" \"\$REMOTE\""
> git config --global merge.tool diffmerge
> git config --global mergetool.diffmerge.trustExitCode true
> git config --global mergetool.diffmerge.cmd
"/usr/local/bin/diffmerge --merge --result=\"\$MERGED\"
\"\$LOCAL\" \"\$BASE\" \"\$REMOTE\""
.bash_profile (or .zshrc)
alias cleanmergefiles="find . -name '*REMOTE*' -type f -delete -o -name '*LOCAL*' -type f -delete -o -name '*BACKUP*' -type f -delete -o -name '*BASE*' -type f -delete -o -name '*.orig' -type f -delete "
Command line helper
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
inside ~/.zshrc:
plugins=(git zsh-autosuggestions)
> git branch -r | grep "FS-101"
I made a commit, I can't find it
I made a branch, I can't find it
> git reflog | grep "FS-101"