Skip to content

Instantly share code, notes, and snippets.

@gabriel-dehan
Created May 3, 2013 18:46
Show Gist options
  • Save gabriel-dehan/5512688 to your computer and use it in GitHub Desktop.
Save gabriel-dehan/5512688 to your computer and use it in GitHub Desktop.

To install & configure github, please read the followings :

If you are using Windows, don't forget the $ git config --global core.autocrlf true to avoid Windows trailing line endings.

And then... Let's CLONE !

Although you may be familiar with the basic ways of git, I recommend you reading the followings, as it will contain instructions on how we organise the work here, at CoHoop.

Basic commands - Using the command line

Retrieving a repository

Once git is installed and configured, you need to retrieve the project via the `clone` command : ```bash git clone https://github.com/CoHoop/CoHoop-Rails.git ``` (if using ssh use `git clone git@github.com:CoHoop/CoHoop-Rails.git` instead)

This command will clone the content of the 'master' branch of the repository in a local working directory on your machine.

Adding modified files to the index

When one or several files have been modified, you must use the `add` command to prepare them for the incoming commit : ```bash git add modified_file_name ```

Commiting your change

Once files are added, you need to create a commit, using the `commit` command.

The commit will use every single file you added, deleted or renamed, so be sure to add related file when staging files for a commit.

For instance, if you made 3 modifications which are not related, you MUST create multiple commits and therefore add only the files related to a change, commit the change, add the files related to another change, commit this other change, etc...

Commits are important, and a commit must reflect a single set of related modifications. If you updated the User model and the Feed module, those are two different commits, not a single one. Please be careful with this and add relevant commit messages

# Bad
git commit -m "Modifications"

# Still Bad
git commit -m "Modifications to the Feed module"

# Good
git commit -m "Feed#display method added to the Feed module"

# Even better
git commit -m "Feed#display method added to the Feed module, this method allows the Feed to represent itself in a view"

Pushing your change to the branch

Once you have made commits and you're done with one or several functionnalities, components or fixes, you may push you work to the current branch you are working on with the `push` command. ```bash git push ```

Retrieving modifications made by other users

1. Make sure you have commited all your changes (don't bother to push them) 2. Use the `git push` command

Branching

tl;dr : One feature = One branch.

When working with git, we use branches. Branches are self contained entities well segregated. The default branch is master, but we are using some other branches to :

  • Master : the default branch, this is the working, stable 'ready to release' application.
  • Develop : the development branch, the working, stable but still 'under development' application.

By default when cloning a repository, you are on the Master branch (git status shows it). This you will need to get the Develop branch using :

git checkout -b develop --track origin/develop

The git checkout develop will switch the current branch to develop and the -b option will create the branch if you don't have it already. origin/develop is the current branch we want to be linked to in the repository.

git status will now return 'On branch Develop', which is what we want. You may want to use the git pull command to get any recent modifications.

Now that we have a working Develop branch, let's dive into the working process : When developing a new feature, debugging one or fixing a bug, you MUST create a new branch :

git checkout -b branch-name

Choose wisely the branch name. Here are some recommandations :

  • feature-module-featurename : Every feature branch should start with the feature word. For instance, if you are doing the user authentication module, name your branch feature-user-authentication
  • hotfix-module-fixname : Same for hotfixes (temporary fixes on important features).
  • fix-module_-fixname : Same for fixes.

Some advices :

  • If a branch already exists, don't re-create it.
  • Don't push your branch on github if you are working alone on it.
  • Push your branch on github if you are not alone on the feature so others can pull it to be on the same page as you are.

Once the feature is ready, all the commits are done and the feature is working and stable (this is important, the Develop branch is STABLE, every feature added must be in a stable state before merging into it, you can merge it with the Develop branch.

git checkout develop
git pull
git merge branch-name

Always pull after switching branches, this should avoid many conflicts as other people might be adding features at the time.

If the merge goes well, you can push the develop branch to github.

Again : One feature = One branch.

Useful commands

git status

Use this, like, all the time. It allows you to see on which branch you are, your commits, which file has been added or deleted, and many other informations.

git reset HEAD <file>

This command will undo any commit you've done on a single file. You can use git reset HEAD to unstage all recent commits on all files.

git rm <file>

Delete a file from the repository, but not from you disk.

git rm $(git ls-files --deleted)

Delete from the repository all the files you deleted locally.

git branch -v

Show all your branches

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