Table of Contents
  • Git Commands

    published: 2018-11-14

    tags: dev,git

    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/