Git

Create a BAT file that runs multiple commands to make commits quicker (Windows)

  1. Creat a .bat file either using the command line (git bash touch)
  2. Add the following commands to the bat file using a text editor or code editor and save the file
  3. Store the .bat file in an accessible area (like the root of C:)
  4. From the command line navigate to the local folder your repo is stored in
  5. Call the .bat file using the entire path (ex: c:/bat-commit.bat)
  6. The .bat file will ask you to enter your comment
  7. Navigate to your repo's URL to make sure the commit went through

@echo off
git status
set /p comment="Enter Comment and press [ENTER]:"
git add .
git commit -m "%comment%"
git push origin master

bash-commit.sh (Linux)

#!/bin/bash
git status
echo Enter Comment and press [ENTER]:
read comment
git add .
git commit -m "$comment"
git push origin master

Make sure it is set as executable. If it doesn't want to run, try

>bash /path/bash-commit.sh

Git Basics

Clone an existing repo

git clone [dir]
git clone git@github.com:username/repo.git

Create a new repo from the command line

echo "# test1" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:eschwelgin/test1.git
git push -u origin master

Push an existing repository from the command line

git remote add origin git@github.com:eschwelgin/test1.git
git push -u origin master

Updating your repo as you work on it (commit often!)

git status
git add .
git commit -m "message"
git push origin master

git init [dir]

Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.

git clone [repo]

Clone repo located at [repo] onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH .

git config user.name [name]

Define author name to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user. Use the command with [name] to set user.name, use the command without [name] and git will return any name already set.

git config user.email [email]

Define author email to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user. Use the command with [email] to set user.email, use the command without [email] and git will return any email already set.

git add [directory, file or operator]

Stage all changes in [directory] for the next commit. Use [file] to change a specific file, or [.] to add all.

git commit -m "message"

Commit the staged snapshot

git status

List which files are staged, unstaged, and untracked.

git log

Display the entire commit history using the default format. For customization see additional options.

git diff

Show unstaged changes between your index and working directory.

Undoing Changes

git revert [commit]

Create new commit that undoes all of the changes made in [commit] , then apply it to the current branch

git reset [file]

Remove [file] from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.

git clean -n

Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.

Rewriting Git History

git commit --amend

Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.

git rebase [base]

Rebase the current branch onto [base]. [base] can be a commit ID, a branch name, a tag, or a relative reference to HEAD.

git reflog

Show a log of changes to the local repository’s HEAD . Add --relative-date flag to show date info or --all to show all refs.

Git Branches

git branch

List all of the branches in your repo. Add a [branch] argument to create a new branch with the name [branch].

git checkout -b [branch]

Create and check out a new branch named [branch]. Drop the -b flag to checkout an existing branch.

git merge [branch]

Merge [branch] into the current branch.

Remote Repos

git remote add [name] [url]

Create a new connection to a remote repo. After adding a remote, you can use [name] as a shortcut for [url] in other commands.

git fetch [remote] [branch]

Fetches a specific [branch], from the repo. Leave off [branch] to fetch all remote refs.

git pull [remote]

Fetch the specified remote’s copy of current branch and immediately merge it into the local copy.

git push [remote] [branch]

Push the branch to [remote], along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.

git config

git config --global user.name [name]

Define the author name to be used for all commits by the current user.

git config --global user.email [email]

Define the author email to be used for all commits by the current user.

git config --global alias.[alias-name] [git-command]

Create shortcut for a Git command. E.g. alias.glog log --graph --oneline will set git glog equivalent to git log --graph --oneline.

git config --system core.editor [editor]

Set text editor used by commands for all users on the machine. [editor] should be the command that launches the desired editor (e.g., vi).

git config --global --edit

Open the global configuration file in a text editor for manual editing.

git log

git log -[limit]

Limit number of commits, e.g. git log -5 will limit to 5 commits

git log --oneline

Condense each commit to a single line

git log -p

Display the full diff of each commit

git log --stat

Include which files were altered and the relative number of lines that were added of deleted from each of them

git log --author=[pattern]

Search for commits by a particular author

git log --grep="[pattern]"

Search for commits with a commit message of [pattern]

git log [since]..[until]

Show commits that occur between [since] and [until]. Arguments can be a commit ID, branch name, Head, or any other kind of revision reference.

git log --[file]

only display commits that have the specified file

git log --graph --decorate

--graph flag draws a text based graph of commits on left side of commit messages. --decorate flag adds names of branches or tags of commits shown

git diff

git diff HEAD

Shows the difference between working directory and the last commit

git diff --cached

shows difference between staged changes and last commit

git reset

git reset

Reset staging area to match the most recent commit, but leave the working directory unchanged

git reset --hard

Reset staging area and working directory to match most recent commit. Overwrites all changes in the working directory

git reset [commit]

Move the current branch tip backword to [commit], reset the staging area to match, but leave the working directory alone

git reset --hard [commit]

Same as previous, but resets both the staging area and working directory to match/ Deletes uncommited changes and all commits after [commit]

git rebase

git rebase -i [base]

Interactively rebase current branch onto [base], Launches editor to enter commands for how each commit will be transferred to the new base

git pull

git pull --rebase [remote]

Fetch the remote's copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches

git push

git push [remote] --force

Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you're absolutely sure that you know what you're doing

git push [remote] --all

Push all of your local branches to the specified remote

git push [remote] --tags

Tags aren't automatically pushed when you push a branch of use the --all flag. The --tags flag sends all of your local tags to the remote repo