Continuing with the theme of using version control in conjunction with a remote repository, let’s go through some practical examples. Create a new project and play around! Also check out ohshitgit for some handy examples of recovering from a mess.

Creating a new project, and committing changes

$ mkdir MyProject
$ cd MyProject
$ git init
$ echo "initial checkin text to foo.txt" > foo.txt
$ git add foo.txt
$ git commit -m "Add foo.txt"

Configuring a remote repository

# Add an incorrect remote repository
$ git remote add origin git://[email protected]/andythemoran/MyProject.git 

# Show remote repos
$ git remote show
origin

# Show details of a repote repo
$ git remote show origin
* remote origin
  Fetch URL: git://[email protected]/andythemoran/MyProject.git
  ...

# Let's remove the incorrect remote repository and add the correct address
$ git remote rm origin
$ git remote show
$ git remote add git://[email protected]/andythemoron/MyProject.git
$ git remote show
origin
$ git remote show origin
* remote origin
  Fetch URL: git://[email protected]/andythemoron/MyProject.git
  ...

Working with a remote repository

# Push changes to remote repo: -u sets the "upstream" of the master branch to the origin
$ git push -u origin master

# I went to bitbucket and manually added "add some text manually on bitbucket" to foo.txt

# Let's pull in the changes
$ git pull
Updating 19e3208..74742b5
Fast-forward
 foo.txt | 1 +
 1 file changed, 1 insertion(+)

# Let's verify the file was updated
$ cat foo.txt
initial checkin
add some text manually on bitbucket

Working with branches

# List branches for project
$ git branch
* master

# Checkout a new branch
$ git checkout -b new_branch
Switched to a new branch 'new_branch'
$ git branch
  master
* new_branch

# Make a change in the branch
$ echo "\nadded this on new_branch" >> foo.txt
$ git add foo.txt
$ git commit -m 'Make a change on new_branch'

# Pull change from new_branch into master
$ git checkout master
$ git merge new_branch
$ cat foo.txt
initial checkin
add some text manually on bitbucket
added this on new_branch

# Let's create a merge conflict...
$ echo "\nchange added in master" >> foo.txt
$ git add foo.txt
$ git commit -m 'add change in master'
[master 368c198] add change in master
 1 file changed, 1 insertion(+)
$ git checkout new_branch
Switched to branch 'new_branch'
$ echo "\nchange added in new_branch" >> foo.txt
$ git add foo.txt
$ git commit -m 'add change in new_branch'
[new_branch 0f415d0] add change in new_branch
 1 file changed, 1 insertion(+)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
$ git merge new_branch
Auto-merging foo.txt
CONFLICT (content): Merge conflict in foo.txt
Automatic merge failed; fix conflicts and then commit the result.

# Git has updated the file to show the conflicts
$ cat foo.txt
initial checkin
add some text manually on bitbucket
added this on new_branch
<<<<<<< HEAD
change added in master
=======
change added in new_branch
>>>>>>> new_branch

# From here you'll need to edit foo.txt accordingly and determine how you want to resolve the conflict

# Check in the fix
$ git add foo.txt
$ git commit -m 'fix merge conflict'
[master dff498d] fix merge

Undoing mistakes

# "Accidentally" delete foo.txt
$ rm foo.txt
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    foo.txt

no changes added to commit (use "git add" and/or "git commit -a")

# Revert changes to foo.txt
$ git checkout -- foo.txt
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

# Revert all local changes
$ rm foo.txt
$ git reset --hard
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

# Revert a specific commit
$ echo "change to be undone" > foo.txt
$ git add foo.txt
$ git commit -m 'change to undo'
[master 3b1fe26] changes to undo
 1 file changed, 1 insertion(+), 6 deletions(-)
$ cat foo.txt
change to be undone
$ git revert 3b1fe26
[master f5ec950] Revert "changes to undo"
 1 file changed, 6 insertions(+), 1 deletion(-)
$ cat foo.txt
initial checkin
add some text manually on bitbucket
added this on new_branch
change added in master
change added in new_branch

You’ll undoubtedly come across many other aspects of git if you end up using it significantly, but I hope the examples have provided some valuable hands on examples for the most common use cases, at least in terms of maintaining your own personal projects.