Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Created June 20, 2012 14:48
Show Gist options
  • Save ktilcu/2960243 to your computer and use it in GitHub Desktop.
Save ktilcu/2960243 to your computer and use it in GitHub Desktop.
Branching in Git

#Branching

In our world of Git we will need to solve problems that may require more than one commit. In order to simplify the process while protecting our stable environment we have implemented a very specific branching system. This system was adopted from this website:

http://nvie.com/posts/a-successful-git-branching-model/

===========

##We are Independent (but not really)

Think of branches as a different version of the same repository.

One may be the current production version while another branch might be a feature you are still working on. Each branch holds the same data in different stages.

For example:

Say you wanted to work on a new feature for git-training that is all about rebasing. In order to keep your commits out of the main stream and preserve commit history you would want to create a branch off of the current dev version.

  1. First you would make sure you are on dev:
    git checkout dev
  2. Then you would make sure you are up-to-date:
    git pull
  3. Then you would create and move to your new branch:
    git checkout -b feature/rebasing

This series of steps created a new branch that you can do whatever you want in without affecting anything else.

enter in this command:

touch newfile && git add . && git commit -m "newfile" && git log

You made you first commit to your new branch! You added newfile.

If you wanted to go back to the current dev version you can!

git checkout dev

This would change all of your files back to the current dev version while still remembering the changes you had made in your feature branch.

Now lets switch back over our feature branch and add another change.

git checkout feature/rebasing
echo "big time changes" > rebase.txt && git add . && git commit -am "huge changes"

##Time to Integrate

Say you wanted to integrate your feature branch.

The easiest way to do this is to merge your branch into the dev branch.

In order to do so you must be on the dev branch and call to the feature branch to merge itself without fast-forwarding.

In our case it would look like this.

  1. Checkout the current dev:
    git checkout dev
  2. Merge changes from specified branch into current branch:
    git merge --no-ff feature/rebasing
  3. Delete your branch:
    git branch -d feature/rebasing
  4. Push changes to GitHub so all can have:
    git push origin dev

##There's no place like origin

The last command in the sequence above took all of the work we had been doing and tried to push it to the origin. The origin is where you originally cloned the repository from.

Most likely you don't have the permissions to push to this repository so you would get an error however, this brings up a great teaching point between the difference of your local repository and the origin of the repository(oftentimes it is github).

Most of the time you will be working in your local repository moving it forward from the place that it was when you downloaded it. The files you are working on are all local. No matter what you do on your computer you can allways revert back to what is online.

In order to keep your changes to files but remove any erroneous commits you can run the below command.

git reset

If you want to remove changes to your files and just completly revert to what onine then you can run the following:

git reset --hard

Be careful with these commands as they can cause you to lose changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment