Skip to content

Instantly share code, notes, and snippets.

@kdipaolo
Last active February 7, 2019 17:53
Show Gist options
  • Save kdipaolo/34617988ef37df26c41eda4c1561f42d to your computer and use it in GitHub Desktop.
Save kdipaolo/34617988ef37df26c41eda4c1561f42d to your computer and use it in GitHub Desktop.

Remember that the HEAD is Git’s way of referring to the current snapshot

git merge

  • Merging takes the contents of a source branch and integrated them with a target branch. In this process, only the target branch is changed.
  • This will create a "Merge commit" in the feature branch that holds the history of both branches

http://here.streamlinedstudio.com/35d39b314931

git rebase

git stash

  • git stash list lists out all stashes
  • git stash apply applies latest saved stash to the working area
  • git stash apply stash@{0} applies designated saved stash. Stash names look like stash@{<NUMBER>} in the stash list
  • git stash pop applies the stash (just like apply) but then removes that stash from the stash list
  • git stash --include-untracked wil stash any new files in the workina area, without this git stash will not stash any new files/folders in the working area.
  • git stash drop remove last stash
  • git stash drop stash@{<NUMBER>} remove designated stash
  • `git stash save "NAME YOUR STASH HERE"

git add

  • git add -p allows you to stage commits in hunks to pick and choose what changes you want to do

git reset

git checkout

A "checkout" is the act of switching between different versions of a target entity. It operates upon three distinct entities: files, commits, and branches.

  • git checkout - shortcut for checking out last branch

How to undo a commit with git checkout

git log: 
4444444 crazy commit that needs to be removed
3333333 third commit
2222222 second commit
1111111 first commit
  1. Check out the commit before the one you want to remove: git checkout 3333333 (Enters a detached HEAD state*)
  2. git log will not contain the commit or commits from 3333333 on
  3. Check out a new branch: git checkout -b new_branch_without_crazy_commit
  4. 4444444 commit is now gone on the new branch
  • This means you are no longer working on any branch. In a detached state, any new commits you make will be orphaned when you change branches back to an established branch. Orphaned commits are up for deletion by Git's garbage collector. The garbage collector runs on a configured interval and permanently destroys orphaned commits. To prevent orphaned commits from being garbage collected, we need to ensure we are on a branch.

git commit

--amend

--amend --no-edit allows you to edit the previous commit’s message without adding another commit --amend -m <COMMMIT MESSAGE> allows you to change the previous commit message name --no-edit lets you ammend changes without specifying a new commit message (just uses the old commit message) Amended commits are actually entirely new commits and the previous commit will no longer be on your current branch.

git reset

  • 3 options: --soft, --mixed (default), --hard
  • --soft Moves head and current branch
  • --mixed Moves head and current branch, resets the staging area
  • --hard Moves head and current branch, resets the staging area, and resets the working area

git reset -- <FILE> - Resets the staging area

git reset or git reset --mixed will move staged changes back to the working directory

`git reset --hard - Resets back to the commit, other commits above the specified commit will be removed from history.

If we have a shared remote repository that had the removed commits pushed to it, and we try to git push a branch where we have reset the history, Git will catch this and throw an error. Git will assume that the branch being pushed is not up to date because of it's missing commits. In these scenarios, git revert should be the preferred undo method.

git revert

The preferred method of undoing shared history is git revert. A revert is safer than a reset because it will not remove any commits from a shared history. A revert will retain the commits you want to undo and create a new commit that inverts the undesired commit. This method is safer for shared remote collaboration because a remote developer can then pull the branch and receive the new revert commit which undoes the undesired commit.

terminal commands

  • echo "HELLO" > hello.txt creates a hello.txt file and writes HELLO in it
  • cat hello.txt shows contents of hello.txt
  • vim hello.txt opens vim editor to edit. i to begin editing. esc to exit edit mode. :wq to save and exit. :q exit and not save.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment