Git: Difference between revisions

From Leo's Notes
This page was last edited on 28 September 2015, at 04:45.
Created page with " == Quick Use Guide == === Adding/Removing remote origin === git remote add origin git@gitlab-blah:user/repo.git git remote remove origin To use public key authentication w..."
 
No edit summary
Line 1: Line 1:


== Quick Use Guide ==
== Quick Use Guide ==
=== Adding/Removing remote origin ===
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.
git remote add origin git@gitlab-blah:user/repo.git
git remote remove origin


To use public key authentication with SSH, you must configure it inside <code>~/.ssh/config</code> instead of passing the key as you would with SSH (ie: <code>ssh -i ~/.ssh/privkey</code>) like so:
=== 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.
<syntaxhighlight lang="text">
Host gitlab-blah
  HostName gitlab.cpsc.ucalgary.ca
  User git
  IdentityFile ~/.ssh/gitlab-leo
</syntaxhighlight>
 
=== Pushing to remote origin ===
git push origin '''branch'''
Where branch can be the master.


=== Committing Files ===
Typically, you will want to stage all files for commit which can be done by running:
Ensure all files you want to commit are staged. To add all files:
{{Highlight|lang=bash|code=
git add -A .
git add -A .
}}


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


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


=== Reverting Changes ===
=== Reverting Changes ===
Line 32: Line 25:
  git reset  
  git reset  


=== 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:
{{Highlight|lang=bash|code=
git remote -v
}}
To add a new remote:
{{Highlight|lang=bash|code=
git remote add origin git@gitlab-blah:user/repo.git
}
To remove a remote:
{{Highlight|lang=bash|code=
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 <code>~/.ssh/config</code> defining the remote's host.
<syntaxhighlight lang="text">
Host gitlab-remote
    User git
    HostName git.example.com
    IdentityFile ~/.ssh/gitlab-remote
</syntaxhighlight>
==== 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:
{{Highlight|lang=bash|code=
git push
}}
You may also specify a specific remote and branch as well. Eg: {{code|git push remote-name branch-name}}.
Conversely, to retrieve changes from another repository, run
{{Highlight|lang=bash|code=
git pull
}}




[[Category:Linux]]
[[Category:Linux]]

Revision as of 04:45, 28 September 2015

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 any changes in the current repository, use (???)

git reset 

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: {{Highlight|lang=bash|code= 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