Git Cheat

I go though phases where I do a bunch of coding and sync to git and then take a break and forget the basic git functions. I've learned git a few times now. The good news is it gets easier every time. That said here is my cheat sheet of setup to push. 


 

 

# Sets the default name for git to use when you commit
git config --global user.name "Your Name Here"

# Sets the default email for git to use when you commit
git config --global user.email "your_email@youremail.com"

# set editor for edit commands
git config --global core.editor vim

# edit global config
git config --global --edit

# Define the author email to be used for all commits by the current user
git config --global alias.<alias-name> <git-command>

# Sets the user's name for this specific repo
git config [--global --local] user.name "Different Name"

# Sets the user's email for this specific repo
git config user.email "differentemail@email.com"

# Set git to use the credential memory cache
git config --global credential.helper cache

# Set the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'

# View the current settings
git config --list

# Sets up the necessary Git files
# Change to the project working directory initiate the repo or work in a repo
git init

# Creates a file called "README" in your Hello-World directory
touch README

# Stages your README file, adding it to the list of files to be committed
git add README

# adds all files in the current directory
git add .

# List local branches of repository (top level)
git branch -d <branch>

# List local branches of repository
git branch
* master

# List all remote and local branches - branch is level 2 - Repository/branch1,branch2,etc
$ git branch -a
* master
  origin/1-2-stable
  origin/2-0-stable
  origin/2-1-stable
  origin/2-2-stable
  origin/3-0-unstable
  origin/HEAD
  origin/master

# act on remote tracking branches
$ git branch -r
  origin/1-2-stable
  origin/2-0-stable
  origin/2-1-stable
  origin/2-2-stable
  origin/3-0-unstable
  origin/HEAD
  origin/master

# remove branch
git branch -d <branch>

# change primary branch name
git branch -m NEW_NAME

# Commits your files, adding the message "first commit"
git commit -m 'first commit'

# The .gitignore file will prevent files from being synced
touch .gitignore

# Remove skipped file from repository
git rm --cached FILENAME

# View files in commit with recurse, name only prevents has from display
git ls-tree --name-only -r BRANCH

# Creates a remote (repository) named "origin" pointing at your GitHub repo
# New git repository must be created on github.com
git remote add REPOSITORY_NAME git@github.com/username/REPOSITORY.git

# test ssh key
ssh -T git@github.com

# add ssh key to agent
ssh-add
ssh-agent

# git can be authenticated with ssh using key pair and an ssh url
git@github.com:USER/REPOSITORY.git

# view url for current repository
git remote get-url REPOSITORY

# set url for current repository to be used by push etc
git remote set-url origin new.git.url/here

# Sends your commits in the "master" branch to GitHub
git push REPOSITORY BRANCH

# View existing remotes w/ verbose
git remote -v
origin  git@github.com:user/repo.git (fetch)
origin  git@github.com:user/repo.git (push)

# Change remote name from 'origin' to 'destination'
git remote rename origin destination

# Verify remote's new name
git remote -v
# destination  git@github.com:user/repo.git (fetch)
# destination  git@github.com:user/repo.git (push)

# get changes on server to local
git fetch REPOSITORY

# fetch does not merge the changes
git merge REMOTE/BRANCH

# fetch and merge in one command
git pull REMOTE/BRANCH

# destroy local changes to allow pull and sync
git reset --hard

# view changes between fetched and local
git diff REPOSITORY/BRANCH

Comments

Popular Posts