How to use Git for code management

👋 Thanks for reading! Things have changed since this was written, take it with a grain of salt ;)

Gitting Started

Setup Git

git config user.name "Firstname Lastname"
git config user.email "[email protected]"

Initialize a get repo

git init

Create a .gitignore file to keep unneeded or confidential information from the repository.

touch .gitignore

Alternatively you can pre-create one here https://www.gitignore.io, or copy and past into the .gitignore.

Add Content

From GitHub

If the project already exists on GitHub and you plan to contribute it can be pulled.

git remote add origin https://github.com/username/project_name.git
git pull

To take a copy of the code for your own use

git clone https://github.com/username/project_name.git

Create Branch

Run this command to get a list of branches and the current branch.

git branch

To create a new branch

git branch <my_new_branch>

To change current working branch

git checkout <my_branch>

Create the branch from GitHub locally and switch to it

git checkout -b <my_branch>

Update a branch if the remote has changed

git fetch origin

Merge a branch back to master

git checkout master
git merge <my_branch>

To delete a branch

git branch -d

To reset a branch to master

git reset --hard master

Want to get a file from another branch? Checkout the branch you want to copy the file(s) too. Then checkout the files from the source branch. Don’t forget to commit when finished adding files.

git checkout <destination_branch>
git checkout <source_branch> <paths>
git commit -a -m "added a couple files from the other branch"

Commit Code

To Git

Run these commands to commit latest changes.

git add .
git commit -m 'comment about latest changes'

Or more condensed

git commit -a -m 'comment about latest changes'

To GitHub

Add GitHub as a remote

git remote add origin https://github.com/username/project_name.git

If you want to change remote or had a spelling mistake (oops)

git remote rm origin
git remote add origin https://github.com/username/project_name.git

# to see what the remote is... in case you forgot :)
git remote -v

Push to remote

git push origin <branch name>
# if you need to force the update..
git push origin <branch name> --force

Removing files that are in .gitignore

To see files that are not excluded by .gitignore

git ls-files -ci --exclude-standard

To remove excluded files from commit

git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached

Create Alias for this command so you can run ‘git apply-gitignore’ to fix any commit that missed updates in gitignore.

# change editor to nano
git config --global core.editor "nano"

# edit file
git config --global --edit

# add following text
apply-gitignore = !git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached