tnplate

notes, interests, bits & bobs, blog

Git Commands

Index

Creating a new repository

Remote Repositories (aka Remotes)

Remotes are where pushes and pulls are sourced from. Local repository can reference multiple remote repositories. “origin” is standard for the primary repository

    git remote add [remote_name] [URL]

…then push with…

    git push [remote_name] [branch]

The Staging area

Undoing local changes

Commiting

Files are not automatically sent to yout remote repository. They are first ‘staged’ so that groups of files can be commited together

comment every commit!

    git commit -m '[commit message]'

Undoing a commit

The HEAD^ denotes the commit before the current HEAD version.

Pulling/Merging

Pulling gets remote commits and merges them into your local branch. You can use pull to merge branches back into master. E.g. if you have a branch called hotfix1 on the remote origin you can merge this back into master with…

    git pull origin hotfix1

The same as calling

    git fetch

and

    git merge hotfix1

Branching

You create new branches locally

    git branch feature1 and
    git checkout feature1

Or you can just create and checkout with

    git checkout -b feature1

When working in a branch make sure you push the branch using

    git push [remote_name] [branch_name]

Your branch won’t be available from the remote until you push it!

Tagging

Git uses two main types of tags: lightweight and annotated. A lightweight tag is very much like a branch that doesn’t change. It’s just a pointer to a specific commit

Annotated tags are stored as full objects in the Git database. They contain the tagger name, email, and date and have a tagging message (we should use these the majority of the time)

Stashing

To keep something that is not ready for commit you can “stash” local changes

When you are ready, you can re-apply the stash against the branch

You can have multiple stash stacks and pick which one you want to apply

Problems

Conflicts: if two people have commited changes to the same lines of the same file you will get a conflict. Easiest approach to resolve this is to use a merge tool

Detached head: When you checkout a specific commit you are detached from the HEAD which is a pointer to the latest commit in the branch. You just need to checkout the branch again to fix this.

References

The documentation on the main Git site https://git-scm.com/doc - book https://git-scm.com/book/en/v2

Useful information on a branching strategy http://nvie.com/posts/a-successful-git-branching-model/