Git: Difference between revisions

From Leo's Notes
This page was last edited on 17 February 2017, at 02:29.
m formatting
Line 7: Line 7:


Typically, you will want to stage all files for commit which can be done by running:
Typically, you will want to stage all files for commit which can be done by running:
{{Highlight|lang=bash|code=
{{Highlight|lang=terminal|code=
git add -A .
$ git add -A .
}}
}}


Run {{code|git status}} to see all files that are staged as well as any changed but unstaged files.
Run {{code|git status}} to see all files that are staged as well as any changed but unstaged files.
{{Highlight|lang=bash|code=
{{Highlight|lang=terminal|code=
git status
$ git status
}}
}}


To commit all the staged files with a message:
To commit all the staged files with a message:
{{Highlight|lang=bash|code=
{{Highlight|lang=terminal|code=
git commit -m 'message'
$ git commit -m 'message'
}}
}}


Line 25: Line 25:
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
## Fetch latest from remote
## Fetch latest from remote
# git fetch --all
$ git fetch --all
## Reset all changes fetched previously.
## Reset all changes fetched previously.
# git reset --hard HEAD
$ git reset --hard HEAD
}}
}}


To revert a specific file from a specific revision, use {{code|git checkout}}.
To revert a specific file from a specific revision, use {{code|git checkout}}.
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
# git checkout <revision> <filename>
$ git checkout <revision> <filename>
}}
}}


Line 39: Line 39:


To see your repository's remotes:
To see your repository's remotes:
{{Highlight|lang=bash|code=
{{Highlight|lang=terminal|code=
git remote -v
$ git remote -v
}}
}}


To add a new remote:
To add a new remote:
{{Highlight|lang=bash|code=
{{Highlight|lang=terminal|code=
git remote add origin git@gitlab-blah:user/repo.git
$ git remote add origin git@gitlab-blah:user/repo.git
}}
}}


To remove a remote:
To remove a remote:
{{Highlight|lang=bash|code=
{{Highlight|lang=terminal|code=
git remote rm origin
$ git remote rm origin
}}
}}


Line 64: Line 64:
==== Pushing / Pulling ====
==== Pushing / Pulling ====
After making a new change in your repository, you may want to push your changes out to other repositories. Do so by running:
After making a new change in your repository, you may want to push your changes out to other repositories. Do so by running:
{{Highlight|lang=bash|code=
{{Highlight|lang=terminal|code=
git push
$ git push
}}
}}


Line 71: Line 71:


Conversely, to retrieve changes from another repository, run
Conversely, to retrieve changes from another repository, run
{{Highlight|lang=bash|code=
{{Highlight|lang=terminal|code=
git pull
$ git pull
}}
}}



Revision as of 02:29, 17 February 2017

Quick Use Guide

As someone who used SVN for the last decade, switching over to git was slightly confusing since the terminology is different. Hopefully, this usage guide will help.

Committing Files

A commit is like a snapshot and allows you to save the current state of the repository. Unlike in SVN where files are manually included/excluded when a commit is made, git allows files to be staged or unstaged prior to the commit being created. Staged files will always be added to the next commit made.

Typically, you will want to stage all files for commit which can be done by running:

$ git add -A .

Run git status to see all files that are staged as well as any changed but unstaged files.

$ git status

To commit all the staged files with a message:

$ git commit -m 'message'

Reverting Changes

To revert all changes in the current repository, use:

## Fetch latest from remote
$ git fetch --all
## Reset all changes fetched previously.
$ git reset --hard HEAD

To revert a specific file from a specific revision, use git checkout.

$ git checkout <revision> <filename>

Remote Origins

Git allows you to push/pull changes from other remote git repositories. Remote repositories can be in another path or hosted somewhere on the internet.

To see your repository's remotes:

$ git remote -v

To add a new remote:

$ git remote add origin git@gitlab-blah:user/repo.git

To remove a remote:

$ git remote rm origin

If your remote uses SSH, you will most likely need to configure SSH in order to make use of SSH key based authentication. You can read more about this at SSH Configuration File, but the basic idea is to create a SSH configuration file at ~/.ssh/config defining the remote's host.

Host gitlab-remote
    User git
    HostName git.example.com
    IdentityFile ~/.ssh/gitlab-remote

Pushing / Pulling

After making a new change in your repository, you may want to push your changes out to other repositories. Do so by running:

$ git push

You may also specify a specific remote and branch as well. Eg: git push remote-name branch-name.

Conversely, to retrieve changes from another repository, run

$ git pull

Examples