Git best practice

Date: January 12th 2016
Last updated: January 12th 2016

Is there any best practice guidelines for using Git? No. However, this write-up, Commit Often, Perfect Later, Publish Once: Git Best Practices, will certainly help clean up my workflow. It's well worth a read, along with Pro Git.

The penny dropped for me when I realised I could be using git locally without pushing my work to a public remote repository. Each small change to my code using tools like pudb and pylint can be recorded with Git. Then, pushing the code to a remote repository when it's ready for public consumption.

The following list of commands provide a reference for starting out with Git: Make a repository
and/or clone to your local workspace. Note these examples use remote repositories from Github and Bitbucket. All commands are executed at the command line.

# CLONE a repository 
cd path/to-repo/destination-folder
git clone

# My example
git clone

# Or CREATE new project folder locally
cd path/to-repo/

mkdir REPO-NAME # create new folder
cd REPO-NAME # move into folder
git init # initialise git

# create and add readme file
echo "# REPO-NAME" >>
git add

# or add all files in your local repository
git add . 

# to REMOVE a file from your local repository  
git reset HEAD FILE-NAME

# HEAD is a symbolic reference to the repository you are making changes to
git reset HEAD # My example

# check staging area status
git status  

# commit changes with a message using -m flag
git commit -m "example commit"

# or include '-a' to commit changed files ("removes --git add .-- step")
git commit -a -m "example commit"

# add a remote repository 
git remote add origin

# specify remote location with -u flag
git push -u origin master

# future updates use...
git push origin master

Wheres my repository at...

# which branch are you looking at
git branch

# Or 
git status # first line

# look at commit history
git log

# Or, add basic stats to log
git log --stat

# see the difference of recent changes to last commit
git diff

# update remote repository
git remote update

# see which branches are updated
git remote -v update

# see the commits to master
git show-branch master

# Or, info on remote repository
git remote show origin # returns error if remote repo not available

# check if remote repo has changes to pull down locally
git fetch origin

# or just fetch if using one remote repository
git fetch

# accept changes from git fetch
git merge

# or just pull for the above two commands...
git pull

Removing files from a repository

# remove a file and update locally
rm test.txt # example removal of file
git commit -a -m "deleted test.txt as an example"

# Or, remove a directory and all its contents
git rm -r DIRECTORY # where DIRECTORY is your folder name
git commit -m "Remove duplicated directory example"
git push origin master

# remove directory from remote repository only
git rm -r --cached DIRECTORY # then commit and push

# remove one file from a folder in the remote repository
git rm --cached FOLDER/FILE #then commit and push

Renaming a remote repository locally

# rename existing repository
git remote rename origin github

# add another repository (e.g. bitbucket)
git remote add bitbucket https://[email protected]/rayblick/Virtual-Ecologist.git

# if you stuff up and need to remove a repository and its name association
git rm bitbucket destination

# point git to the new names (HEAD)
git push github HEAD 
git push bitbucket HEAD

# For all commands above replace origin with either bitbucket or github.

Branching from the command line

# create a new branch
git branch issue10

# now switch to that branch
git checkout issue10

# or
# create and switch to a new branch "issue10"
git checkout -b issue10

# or switch to existing branch "issue9"
git checkout issue9

# switch back to master
git checkout master

# delete a branch "issue10"
git branch -d issue10

# merge a branch
git merge issue9

# identify merge conflicts (e.g. independent changes to code in two places)
git status

# make changes to conflicting files so they are the same, then
git add FILENAME1
git add FILENAME2

# or resolve conflicts with GUI
git mergetool

# post-conflict resolution
git merge issue9

Revisit an old commit

# before looking at old commits: stash recent changes
# either 
git commit -a -m "msg"

git stash

# to come back to recent changes
git stash pop

# To investigate a commit by detaching HEAD
# you can look out the old commits
git checkout <commit>
git checkout 0dfg37588 # a snipped commit example

# go back to where you were
git checkout master # or branch

# Or make a new branch from an old commit to make changes
git checkout -b old-state <commit>

# go back to a previous commit 
git reset --hard <commit>

results matching ""

    No results matching ""