Skip to content

Instantly share code, notes, and snippets.

@rainbowbird
Last active February 29, 2020 04:21
Show Gist options
  • Save rainbowbird/e1e603463a2a164624dc343f347bcbb1 to your computer and use it in GitHub Desktop.
Save rainbowbird/e1e603463a2a164624dc343f347bcbb1 to your computer and use it in GitHub Desktop.
Introduction to Git

Part 1

3 conceptual areas of a Git repository

  • Working Truee
  • Staging Area (Index)
  • History

diff of working tree and staging area

git diff

diff of staging area and latest commit

git diff --staged

Retrieve a file from the staging area into the working tree

git checkout -- <file>

Retrieve a file from the latest commit into the staging area

git reset HEAD <file>

retrieve a file from a previous commit

git log -- <file>
git checkout <changeset> -- <file>
git commit <file>

Part 2

branch

A branch is just a pointer to a SHA-1 hash.

HEAD

The way Git knows which branch we are on is a special pointer called HEAD. HEAD is a pointer normally points to a branch. Since HEAD usually points to a branch and NOT directly to a commit, it is sometimes called a symbolic pointer. In Git terminology, the HEAD pointer tells us what we have checked out.

git log --all --decorate --oneline --graph
alias graph="git log --all --decorate --oneline --graph"

diff between 2 branches

dit diff <branch 1>...<branch 2>

Fast-Forward merge

git merge <branch name>

Delete branches

git branch --merged
git branch -d <branch name>

3-Way merge

git merge <branch name>

Merge conflicts

git merge --abort

# to mark resolution
git add <file>
# to conclude merge
git commit

Detached HEAD

Usually HEAD points to a branch which in turn points to a commit. When HEAD is instead pointing directly to a commit, we have a detached HEAD state.

git checkout <SHA-1 hash>

Git Stash

git stash
git stash save <stash message>

git stash list
git stash list -p

git stash apply <stash label>

git stash pop <stash label>

Part 3

Define Git Remotes

When we are working in a repository, a remote is simply a repository in another location from where we are currently working.

Git clone

When we fetch or push updates we don't need to remember the full location of our remote. Instead, Git can hold a short name or alias to that location. The default first name is origin.

Git Remotes

git remote
git remote -v

remote-tracking branch

origin/master is a specialized branch. it is called a remote-tracking branch. The job of this remote-tracking branch is to tell us what the master branch looks like at origin.

The remote-tracking branch is not the same as a standard local branch. We can check it out with git checkout origin/master. Checking out the remote tracking branch works, but we end up in a detached HEAD state.

git fetch and git merge

git fetch <remote repo>
git merge origin/master

# "git pull" combines "git fetch" and "git merge" into a single command.
git pull

git push

# we are pushing our edit to our remote named origin, and to the master branch at origin.
git push origin master

Add a git remote

git remote add <short remote name> <remote URL>

git remote remove <remote name>

Sync with upstream

git fetch <remote name>
git merge
git push

Remote Branches

git branch -a

# displays remote-tracking branches only
git branch -r

git push

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment