Skip to content

Instantly share code, notes, and snippets.

@beneggett
Last active February 6, 2016 06:28
Show Gist options
  • Save beneggett/6619814 to your computer and use it in GitHub Desktop.
Save beneggett/6619814 to your computer and use it in GitHub Desktop.
Git techniques as discussed in class & learned by going through the code school class

Learning Git

Personalizing Git: Common settings

git config --list      // Checks your current git configuration settings
git config --global core.editor "sub --wait"     // Sets sublime text to be the default editor for commit messages
git config --global user.name "John Doe"   // Sets up the default name for Git Commits
git config --global user.email "[email protected]" // Sets up the default email for Git Commits

git help config       // Shows the git config manual for full documentation

Setting up the exercise

cd ~/Sites          // Change to Sites Directory
mkdir git-lesson    // Make a git-lesson project folder
cd git-lesson       // Change to git-lesson Directory

Initialize an new git directory

git init

git help init       // Shows the git init manual for full documentation            

Check the Status

git status

git help status       // Shows the git status manual for full documentation            

Create a new file

touch hello-world.txt
echo "Hello there world" >> hello-world.txt
cat hello-world.txt // Display the contents of hello-world.txt in your shell

Create a new folder with a few files in it

mkdir documents
touch documents/instructions.txt
echo "These are the instructions" >> documents/instructions.txt
touch documents/readme.txt
echo "These are the readme" >> documents/readme.txt
touch documents/info.markdown
echo "# Information" >> documents/info.markdown

Adding files to the stage

git add hello-world.txt   // Adds a specific file
git add documents         // Adds a folder and all its contents

git add *                 // Adds all files in the current directory, can be chained and appended to folder paths
git add .                 // Adds all files in the working directory
git add *.txt             // Adds all text files in current directory
git add "*.txt"             // Adds all text files in all directories
git add documents/*.txt  // Adds only the text files within the documents directory
git add hello*            // Adds all files starting with hello
git add -p                // Interactive git add function

git help add       // Shows the git add manual for full documentation

Committing your staged files to the repository

git commit                // The standard commit, will open up in your default editor
git commit -m "Message"   // Quick commits by passing the message flag

git commit -m             // Interactive git staging function

git help commit       // Shows the git commit manual for full documentation

Tips on writing good commits

Structure your commit message like this:

Summarize clearly in one line what the commit is about

Describe the problem the commit solves or the use
case for a new feature. Justify why you chose
the particular solution.

DO:

  • Write the summary line and description of what you have done in the imperative mode, that is as if you were commanding someone. Write "fix", "add", "change" instead of "fixed", "added", "changed".

  • Always leave the second line blank.

  • Line break the commit message (to make the commit message readable without having to scroll horizontally in @gitk@).

For more tips and tricks, see: http://who-t.blogspot.de/2009/12/on-commit-messages.html

History: The Git Log

git log                   // Displays the commit history of your repository
git log --pretty=oneline  --abbrev-commit // Displays a short one line summary and abbreviates the commit SHA hash

git help log       // Shows the git log manual for full documentation

Remote Repositories

git remote add origin [email protected]:beneggett/devpoint-git-lesson.git                   // adding a remote repository with the name 'origin'

git help remote       // Shows the git remote manual for full documentation

Pushing Remotely

git push -u origin master   // Push to your remote repository and set it as your default remote repository and master branch by passing the -u flag. 
git push                    // Will push to the default remote repository and branch

git help push               // Shows the git push manual for full documentation

Pulling from a remote repository

git pull origin master   // Pull from the origin remote repo into your master branch
git pull                 // Does the same as the above if a default was set with -u

git help pull            // Shows the git pull manual for full documentation

Viewing Differences

git diff                // View differences in unstaged files vs. HEAD
git diff --staged       // View differences in staged files vs. HEAD 
git diff HEAD           // View differences in latest commit
git diff 54c62fc        // View differences of a specific commit

git help diff            // Shows the git diff manual for full documentation

Resetting the stage

git reset hello-world.txt // Resets hello-world.txt and removes it from the stage; does not modify the file
git reset HEAD            // Resets all commit history back to the HEAD, does not modify any files

git reset 54c62fc         // Resets all commit history back to a specific commit, does not modify any files
git reset HEAD --hard            // Resets all commit history back to the HEAD, restores all tracked files to the state they were in at the commit
git reset 54c62fc --hard         // Resets all commit history back to a specific commit, restores all tracked files to the state they were in at the commit
git reset HEAD~1          // Resets all commit history back to one commit before the latest (HEAD), does not modify any files without the --hard flag
git reset HEAD~3         // Resets all commit history back to three commits before the latest (HEAD), does not modify any files without the --hard flag

git help reset            // Shows the git reset manual for full documentation

Undoing changes in a file

git checkout hello-world.txt    // Undos all changes and resets the file to the state it was in at the last saved commit

git help checkout            // Shows the git checkout manual for full documentation

Branching out

Creating a new branch

git branch clean_up_text

git help checkout            // Shows the git checkout manual for full documentation

Switching branches

git checkout clean_up_text

git help checkout            // Shows the git checkout manual for full documentation

Removing files from your repository

git rm *.txt documents/*.txt     // Removes all the .txt files from the current directory and the documents directory. Not only removes the physical files, but also removes the tracked files from git
git rm --cached *.txt            // Removes the tracked file from git repository, but does not remove the physical file from the disk
rm *.txt                         // Standard way of deleting all text files in the current directory in UNIX. would not remove the tracked file from git, would need to run git rm --cached *.txt as shown above

git help rm            // Shows the git rm manual for full documentation

Commiting Branch Changes

Make a commit per usual

git commit -m "Remove all the text files"

Switching back to the master branch

git checkout master

Merging branches

git merge clean_up_text            // Will merge the clean_up_text branch into the current branch
git merge clean_up_text master     // Explicitly merges the clean_up_text branch into the master branch

git help merge            // Shows the git merge manual for full documentation

Removing unnecessary branches

git branch -d clean_up_text       // Deletes the clean_up_text branch from your local repository

Pushing it to our remote repository

Simple as:

git push

Stashing files in GIT

Stashing is a great way to stash files into a temporary location.

git add (files-you-want-to-stash)       // Add the files you want to stash
git stash                               // Stash the files. You will now see those files cleared from your git status results
git stash list                          // List all the stashes you have stored on your system
git stash pop                           // Restore your last git stash
git stash pop stash@{2}                 // Restore a specific git stash
git stash show stash@{2}                // Show a specific stash
git stash drop stash@{2}                // Delete a specific stash
git stash clear                         // Clear your entire stash

git help stash                          // Shows the git stash manual for full documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment