How to Use Git

BASH

Basic BASH

which = Checks if commands are installed and availble
echo = Outputs provided variables. e.g. echo "Hello World" OR echo "$Path"
clear = Clears screen
exit = Exit terminal

Directory Navigation and Management

pwd = Present Working Directory
cd = Change Directory. e.g. cd desktop/ OR cd /c/program\ files\ \(x86\)/Notepad++)
cd .. = Back One Step
cd ../../.. = Back Multiple Steps
cd ~ = Return to home directory
mkdir = Make Directory. e.g. mkdir project OR mkdir project/assets/textures
rmdir = Remove Directory
ls = Lists files and folders in current directory
ls -l = More Detail

File Management

touch = create new empty files or changes the last edited timestamp
echo "hello world" >> demo.txt = Append text to the end of a file
echo "hello world" > demo.txt = Replace Contents
cat = Outputs contents of file into terminal
less = Outputs contents of file with better viewing for large files
mv = Move or Rename file. e.g. mv oldName.txt newName.txt)
rm = Remove File
rm -r -f = Remove File/Directory by Force. -r means Recursive -f means Force DANGEROUS!

GIT

Git's 4 States (3 Local & 1 Remote)

Working Directory

This includes files tracked by git and files ignored by git.
You can send files to the staging area by using the git add command.

Staging

The staging area only includes files that git tracks.
All new files, deletions, changes, etc. that are tracked by git will be moved to the staging area before commiting.
To send files to the Local Repository use the git commit command.

Local Repo

The local repository is you .git folder
It contains all history and changes to tracked files through snapshots called commits.

Remote Repo

The remote repo works just like your local directory only hosted remotely.
Interaction with the remote Repository invloves using the git clone, git push, git fetch, and git pull commands

Basic GIT Commands

git init . = Initializes current folder as a git repo
git init demoProject = Initializes a new folder named demoProject as a git repo
git status = Displays current status of repo

Add and Commit

git add New.File = If New.File exists in working directory, adds it to the Staging Area
git add -A = Add all files to staging area.
git add -u = Adds files with updates to staging area.
git commit -m"CommitMessageHere" = Adds any files from staging into Repo with a commit message
git commit -a = Commit modified files to repo. New Files Ignored
git commit -am = Both at once git reset HEAD demo.file = Resets the changes made in the staging area to last commit of branch
git checkout -- demo.file = Resets the changes made in the working directory to the last commit of the branch

Log and Show

git log = Display log of commits
git show = More info on last commit
git log --oneline = simplified commit history
git log --graph = Denotes branching hierarcy
git log --decorate = which commits are part of which directories
git log --all = provides history of all branches
git log --oneline --graph --decorate --all = All at once
git config --global alias.hist "log --oneline --graph --decorate --all" = Create shortcut
git mv oldName.File newName.File = "Move" file to its new name using git
git rm test.File = Delete File using Git
git diff COMMIT# COMMIT# = Compare differences between commits. e.g. git diff cf5154e d715826 OR git diff cf5154e HEAD

Branches

git branch = Lists branches
git checkout BranchName = move to branch
git checkout -b NewBranchName = Create new Branch and Move to it
git diff BranchName NewBranchName = Compare differences between branches
git merge NewBranchName = Merge NewBranchName with current branch
git branch -d NewBranchName = Delete Branch Label
git mergetool = Opens the merge tool defined in global config

Remote Repo Commands

git remote add RemoteDepoName RemoteDepoURL = Connect to Remote Repo
git remote rm RemoteDepoName = Disconnect from Remote Repo
git remote -v = Lists current connected Remote Repos
git push -u RemoteDepoName LocalBranchName = pushes local repo to remote repo
git fetch = updates local repo to be aware of changes on remote
git pull RemoteDepoName LocalBranchName = Pulls from remote depo to the local branch
git config credential.helper store = Stores your credentials so you dont have to enter them every time.

Tutorials

Setting up Notepad++

When using BASH it is a good idea to connect a text editor with a decent feature set. If you're using windows then Notepad is already connected by default... But do you really want to use Notepad to create scripts and deal with BASH?

1. Navigate to where Notepad++ is installed on your computer. In my case it was at "C://Program Files(x86)/Notepad++"

2. Copy that exact filepath

3. Add the filepath to the Environment Variables of your OS. For Windows: Right-click on "This PC" > Click on Advanced System Settings > Click on Environment Variables > Click Path in System Variables > Click Add New

4. Save and close both the System Settings menu and close and reopen BASH

5. Test if it works by typing "notepad++ test.txt" in BASH, it should open Notepad++ and prompt you to make a new file

Justin Schwieger@SPD3_01 MINGW64 ~
$ notepad++ text.txt

6. Setup Notepad++ as our global text editor. While we are here we might as well add notepad++ to the .gitconfig file to help things in the future.

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global core.editor "notepad++ -multiInst -nosession"

Creating a BASH script

Creating Bash Scripts can save you a tremendous amount of time when working with BASH and Git. Make sure you have followed the previous tutorial on setting up Notepad++ in your Systems Environment Variables.

1. Find out where BASH is by using the command: "which bash" This will output the location of the BASH terminal you are currently using e.g. "/bin/bash"

Justin Schwieger@SPD3_01 MINGW64 ~
$ which bash
/usr/bin/bash

2. Copy this location

3. Open Notepad++ & Create a new file with the command "notepad++ helloworld.sh" or you can setup an alias to type something shorter for the command

Justin Schwieger@SPD3_01 MINGW64 ~
$ notepad++ helloworld.sh

4. Setup UNIX Formatting In Notepad++ go to Edit > EOL Conversion > UNIX Format

5. Write the shebang line At the top of your document type: "#!/bin/bash" (Replace everything after #! with the location you coppied in step 2 and do not use quotations.) This tells the kernel which interpreter to use when executing the script. Basically it makes sure your scripts are executed in the correct version of BASH.

6. Write your Script Everything after the shebang can be written as 1 command per line into the terminal. Use # to start comments. Copy and paste the following into notepad++ to create your first script (Be careful to change the shebang line if yours differs).

7. Save and Test your new Script! You can run your script by entering using the location as a command. In our case we can type ./helloworld.sh since we created the file in the Present Working Directory. You can also run a .sh script by double clicking the file but this will open a terminal, execute the command, and immedietly close it.

Justin Schwieger@SPD3_01 MINGW64 ~
$ ./ helloworld.sh
hello world

If your script does not work it may not be executable. To make your script executable use the chmod +x command on your file.

Justin Schwieger@SPD3_01 MINGW64 ~
$ chmod +x helloworld.sh

Setting up Git Configs for use

Git Requires your username and email to set this up we modify the git global config file.

1. Use git config --global user.name "YourName" to add your username to the global config file

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global user.name "Justin Schieger"

2. Use git config --global user.email"Your@email.com" to add your email to the global config file

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global user.email "me@jschwieger.com"

3. Use -e to check the file and potentially modify it.

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global -e

This will open a file that looks something like this:

Setting up your Diff/Merge tool with Git Config

Before we can try to merge branches in our repos we should setup our Diff & Merge tool for less headaches.

1. Setup your Diff Tool. Use the commands below to add your Diff tool to your global config settings. Make sure you replace p4merge and the file path with whatever diff tool you want to use and the location it is installed.

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global diff.tool p4merge
	
Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global difftool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global difftool.prompt false

2. Setup your Merge Tool. Use the commands below to add your Merge tool to your global config settings. Make sure you replace p4merge and the file path with whatever merge tool you want to use and the location it is installed.

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global merge.tool p4merge

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
	
Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global mergetool.prompt false

3. Use -e to check the file and potentially modify it.

Justin Schwieger@SPD3_01 MINGW64 ~
$ git config --global -e

Creating a new EMPTY Repo

This tutorial will show you how to create a repo that initializes with no files. Great for when you have not started work yet, or if you are going to pull down a remote repo into a new directory.

1. Use the cd command and navigate to an area that you want to keep your projects.

Justin Schwieger@SPD3_01 MINGW64 ~
$ cd /c/Users/Justin\ Schwieger/Documents/

Justin Schwieger@SPD3_01 MINGW64 /d/_documents 
$ _

2. Create a new Directory with mkdir for your projects. Then move into it with cd.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents 
$ mkdir projects/

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/ 
$ cd projects/

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects 
$ _

3. Create and Initialize a new Git Repo with the git init REPONAME command. Then you can move into it with cd. Congrats, you now have a git repository setup.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects 
$ git init project1
Initialized empty Git repository in D:/_documents/projects/project1/.git/

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/ 
$ cd project1/

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (master)
$ _

Creating a new Repo in an existing directory

1. Use the cd command and navigate to an area that you want to keep your projects.

Justin Schwieger@SPD3_01 MINGW64 ~
$ cd /c/Users/Justin\ Schwieger/Documents/projects

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects 
$ _

2. Create a new Directory with mkdir for your project. Then move into it with cd.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects 
$ mkdir project1/

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects 
$ cd project1/

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 
$ _

3. Initialize a new Git Repo by using the Git Init . command. Using . to signify your current directory.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 
$ git init .
Initialized empty Git repository in D:/_documents/projects/project1/.git/

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (master)
$ _

Creating and Managing Branches

1. Check your current Branches.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (master)
$ git branch_
* master

2. Create and then move to (checkout) a new Branch.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (master)
$ git checkout -b feature
Switched to a new branch 'feature'

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (feature)
$ _

This can also be done with git branch branchName but using git checkout -b branchname also moves you to the branch right after creation, which is usually what I need to do anyways.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (master)
$ git branch feature

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (master)
$ _

git checkout branchName without the -b is used to switch between branches that already exist.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (feature)
$ git checkout master
Switched to branch 'master'

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (master)
$ _

3. Now Check Branches again to see the updated list.

Justin Schwieger@SPD3_01 MINGW64 /d/_documents/projects/project1 (feature)
$ git branch_
  master
* feature

Merging

Connecting to a Remote Server

Interacting with a Remote Server

Git Related Downloads and Websites

Git

Text Editor

Notepad++

GUI Tools

SourceTree

Merge/Diff Tools

p4Merge
KDiff3

Remote Repository Hosting

GitHub
BitBucket
Perforce Helix

Related Articles

14 tips and tools to resolve conflicts with Git

What does the shebang line do?