Option 1: Git 101
Mac | git --version
If you get a version, you’re good. If not, do the following: Install homebrew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install git |
|
Windows / Linux | Instructions here: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git |
Log in to your github account.
Go to: https://github.com/damonJansenDexcom/git101.git In the upper right, click "Fork" Either: a. Go to "Code" and copy the link for how you clone repos.
b. If you do not have authentication set up, you can just "Download ZIP". Unzip the file.
(You'll be able to work locally, but you can ignore anything referring to push, remote, upstream) |
Motivation: Why would I do this? | Commands to run in terminal | Notes |
I would prefer not to use vim as my git editor. | Optional: if you are not familiar with vim, you may want to use a different editor for git.
To use VS Code:
(Note that this only affects the shell you run it in. You'll need to add it to your .bashrc / .zshrc / similar to ) |
There are other options here. |
git branch
|
Locally, there is only one branch – main. | |
git branch -a
|
-a: all
You can see the remote and local branches |
|
I want to start at a place where there has been some particular work done already. | git checkout test-branch-3
|
Note the output:
"set up to track" -> we have a link between our local branch and the remote branch, and they both have the same name |
I want to let others have access to the changes I have made. | git checkout -b <your-name>-from-test-branch-3
Copy the instructions given to create a remote (upstream) branch
|
Note the initial error, telling us that the branch only exists locally:
|
I want to do some editing and I want feedback about which files have changed. | 1. in all files, change all instances of "potatoes" to "turnips"
2. |
Note the instructions git gives, telling you what you can do
|
I think my changes to testfile-3.txt are worth keeping. The add command tells git that testfile-3.txt should be included in my next commit. If you don’t add it, it won’t be included. Git does not assume all your changes are golden.
|
git add testfile-3.txt
|
Continue noting what output git gives you. It often gives you hints for next steps |
Adding commits lets me keep track of my progress and changes as I work. Git considers each commit a “change point” or "save point".
|
git commit -m "Change potatoes -> turnips"
|
|
I want to see all the commits on this branch. | git log | Read everything in the top commit
|
Follow the same steps, changing "apples" to "sardines", and make a commit, and git push |
Motivation: Why would I do this? | Commands to run in terminal | Notes |
I want to save the changes I currently have, but not make a commit. | Make some changes to the files.
Make some changes
|
the stash is a place to put changes so that they are retrievable
git stash apply puts back the changes for the most recent stash (
|
Motivation: Why would I do this? | Commands to run in terminal | Notes |
I'd like to change the last commit message | git commit --amend
Change the text of your commit message
…
|
Read the error message. --amend rewrites history, so local remote cannot be integrated.
to rewrite over the remote, choosing the version that is local |
I'd like to change the last commit message | git rebase -i HEAD~1
Change Change the commit message and save |
HEAD is the snapshot of the last commit
HEAD~1 is one commit before that |
I'd like to turn two commits into one (or similar) | git log
Note the first two commits
Keep
Change …
|
The instructions are helpful. You'll want only one line without a '# ", and that will be the new commit message of the combined commits.
Note in the git log that there is only one commit where there used to be two. |
I'd like to rewrite the history in a number of ways. | git rebase -i
|
There are many options with interactive rebase. You might like to try some more out. |
Motivation: Why would I do this? | Commands to run in terminal | Notes |
I'd like to go to a particular time/place in the code (with a git hash) | git checkout c4d6c8c9091ccdee4734c3b156c5e9da01af203b
Often, a next step:
|
This is the HEAD detached state. I like to think of it as "not on a branch." It's great for starting from a known state.
Often, a next step is to checkout a new branch. |
I'd like to mark a particular place in the code with a recognizable name, and let others know about it. | git tag <a-great-name-for-a-tag>
|
|
I'd like to go to a particular time/place in the code (with a tag) | git checkout dj-here-is-a-great-tag
|
Note that the tag is called out in the most recent commit |
Motivation: Why would I do this? | Commands to run in terminal | Notes |
I want to get changes from another branch onto my branch. (Example 1: Using rebase) | Set ourselves up:
Note that the most recent commit (the one at the top) has the "dj-here-is-a-great-tag" tag
Now, make any old commit.
Notice that (reading top to bottom), it now has your commit, then the tagged one. Now, we want to get some recent changes from `main` branch.
Notice that your commit is still at the top, but there are commits in between it and the tagged commit. The history has changed. |
The rebase put your commit "on top of" what is on main.
This kind of rebase can be hard if there are a lot of conflicts between what you've done and what's on the other branch. See the next example for a solution |
I want to get changes from another branch onto my branch. (Example 2: Using merge) | Set up just like Example 1
Now, make any old commit.
Complete the commit message like an commit.
Note that the history has not changed. There is just a new "merge commit" that represents the bringing of that new material in. |
Some folks really like to use rebase, some like to use merge. This is a good one to learn how to do both and talk with your co-workers about it. |
Motivation: Why would I do this? | Commands to run in terminal | Notes |
I want to try out git commands in a safe place. | mkdr <name-of-sandbox>
|
This is all it takes to have a local git repo. |
I want to try out git commands, including pushing/pulling to a remote. | Go to https://github.com/ and login to your account. You can make a new repo: |
Option 2: https://learngitbranching.js.org/