Skip to content

damonJansenDexcom/git101

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

git101

Option 1: Git 101

Getting Git On Your Computer

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

Setting Up Our Shared Repo

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.

get link to clone from github

git clone <link you copied>

b. If you do not have authentication set up, you can just "Download ZIP". Unzip the file.

cd git101

(You'll be able to work locally, but you can ignore anything referring to push, remote, upstream)

First Steps: Branches, Commits, Log

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:

export GIT_EDITOR="code --wait"

(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:

branch 'test-branch-3' set up to track 'origin/test-branch-3'.

Switched to a new branch 'test-branch-3'

"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

git push

Copy the instructions given to create a remote (upstream) branch

git push --set-upstream origin <your-name>-from-test-branch-3

Note the initial error, telling us that the branch only exists locally:

fatal: The current branch damon-jansen-from-test-branch-3 has no upstream branch.

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. git status

Note the instructions git gives, telling you what you can do

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

(use "git restore <file>..." to discard changes in working directory)

modified: testfile-3.txt

no changes added to commit (use "git add" and/or "git commit -a")

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

git status

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".

"Change potatoes -> turnips" is a message/label that tells everyone that the commit was about.

git commit -m "Change potatoes -> turnips"
I want to see all the commits on this branch. git log Read everything in the top commit

git log shows git sha, branches, commit message

Follow the same steps, changing "apples" to "sardines", and make a commit, and git push

Temporary States

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.

git status

git stash save "<description of changes>"

git status

git stash list

git stash apply

Make some changes

git stash save "<description of changes>"

git stash list

git stash apply stash@{0}

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 (stash@{0})

git stash apply stash@{<number>} lets you apply a particular stash

Rewriting History

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

git push

git push --force

Read the error message. --amend rewrites history, so local remote cannot be integrated.

git push --force

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 pick to reword (or just r)

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

git rebase -i HEAD~2

Keep pick on the first commit

Change pick to squash (or just s) on the second commit

git log

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.

Time Traveling

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

git status

Often, a next step:

git checkout -b <your-name>-branching-from-a-git-hash

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>

git push origin <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

git log

Note that the tag is called out in the most recent commit

Getting Others' Changes

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:

git checkout dj-here-is-a-great-tag

git log

Note that the most recent commit (the one at the top) has the "dj-here-is-a-great-tag" tag

git checkout -b <your-name>-branch-to-rebase

Now, make any old commit.

git log

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.

git fetch origin

git rebase origin/main

git log

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

git checkout dj-here-is-a-great-tag

git checkout -b <your-name>-branch-to-merge

Now, make any old commit.

git fetch origin

git merge origin/main

Complete the commit message like an commit.

git log

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.

Sandboxes: Experimenting On My Own

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>

cd <name-of-sandbox>

git init

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:

create a new repo on github

Option 2: https://learngitbranching.js.org/

About

git101

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages