Page 1 of 1

HOWTO: Quick Git Guide

Posted: Fri Feb 20, 2015 9:21 am
by machinebacon
This was originally a post by dkeg, I copy-paste it here to have GIT stuffin one place. Thanks Drew for the nice tutorial :)
dkeg wrote:disclaimer, I'm sure Joe would have a more proper tutorial for this.

Generally speaking ... we're looking at two separate things here, using git locally and with a service such as GitHub or Bitbucket.
Using locally:

Code: Select all

sudo apt-get install git
initialize the directory, add the items, commit

Code: Select all

cd /dir/for/git
git init
git add -A 
#above is all to add all items. You can add specific items too
git add file1 file2 file3
git commit -m 'm is for message, add your message here'
git status #check current status
First things first, create you ssh key

Code: Select all

ssh keygen -t rsa "email"
Copy the public key and use that for your key in GitHub/Bitbucket

Code: Select all

cd ~/.ssh
<editor> id_rsa.pub
Connecting with a remote server. Pretty much the same as above, of course we need to specify our remote location

Code: Select all

cd /dir/for/git
git remote add origin [email protected]:gituser/reponame.git
git push origin master
When we push origin master, we push up to our remote GitHub account.

When you are happy with you current state, you can create a branch, and work off the branch.

Code: Select all

git branch <branchname>
Move to the new branch

Code: Select all

git checkout <branchname>
Create and move to all in one

Code: Select all

git checkout -b <branchname>
Now you can do all your work on the branch, when you're happy, commit and merge it with master

Code: Select all

git commit -m 'commit message'
git checkout master
# check the current working branch
git branch
git merge <branchname>
You can either keep the branch or delete

Code: Select all

 git branch -d <branchname>
For cloning a repo

Code: Select all

git clone [email protected]:gituser/reponame.git
cd reponame
# track it
git remote add upstream git://github.com/gituser/reponame.git
git fetch upstream
# I generally do
git pull
To help keep my clones organized I create a git directory

Code: Select all

mkdir git
cd git
git clone .....
be happy and organized

Other cool stuff

Code: Select all

git diff
git log --graph
More disclaimer, please don't by shy about questions. Again, I am not a pro at this, its mainly from what I've picked up from personal use. There is obviously so much more that can be done, and possibly better ways to do stuff. For my configs and such I generally don't branch, but when I was coding, I would definitely branch.

Hopefully I didn't leave anything important out.

Edit: IIRC when you add a remote origin,if its added as https, not ssh. You can change that with set-url

Code: Select all

git remote set-url origin [email protected]:gituser/reponame.git

Re: HOWTO: Quick Git Guide

Posted: Fri Feb 20, 2015 9:42 am
by wuxmedia
nice, should use this more, I clone like crazy, but fail at updating via the command line.
Thanks Both!

Re: HOWTO: Quick Git Guide

Posted: Mon Apr 11, 2016 9:00 am
by GekkoP
Git lets you GPG sign your commits. Not a bad thing, so this is what's needed:

As long as you have your GPG key already set up, add this line to your ~/.gitconfig or ~/.config/git/config:

Code: Select all

signingKey = <your-public-key-id>
You can find out the key-id using:

Code: Select all

gpg --list-public-keys
It's the one after the "/" on the line that starts with "pub".

Now all that's left is:

Code: Select all

git commit -S -m <your commit message>
The "-S" does the trick:
-S[<keyid>], --gpg-sign[=<keyid>]
GPG-sign commit.
As a plus, GitHub confirms you did it right:
2016-04-11-100131_1024x96_scrot.png

Re: HOWTO: Quick Git Guide

Posted: Mon Apr 11, 2016 6:33 pm
by kondor
Thanks for yet another tasty guide, Bacon.

I've been looking for a quick n' dirty, no bullshit rundown like this for a while now.

I also enjoyed 'Git Happens' to wrap my brain around Git in general, including the lesser known features:
https://www.youtube.com/watch?v=Dv8I_kfrFWw

Re: HOWTO: Quick Git Guide

Posted: Tue Apr 12, 2016 7:21 am
by wuxmedia
relevant XKCD: http://xkcd.com/1597/
although, with this guide should be a sea change.

Re: HOWTO: Quick Git Guide

Posted: Thu Apr 14, 2016 2:35 am
by sgtbigman
GekkoP wrote:Git lets you GPG sign your commits. Not a bad thing, so this is what's needed:
Just wanted to link to the official Git documentation as well (I enjoy using GitHub, but want to make others aware of other sources).
GekkoP wrote: As long as you have your GPG key already set up, add this line to your ~/.gitconfig or ~/.config/git/config:

Code: Select all

signingKey = <your-public-key-id>
You can find out the key-id using:

Code: Select all

gpg --list-public-keys
It's the one after the "/" on the line that starts with "pub".
Note that instead of editing the config, you can use

Code: Select all

git config --global user.signingkey <your-public-key-id>
To use a specific key while in a specific local repository, remove the '--global' bit.

Re: HOWTO: Quick Git Guide

Posted: Thu Apr 14, 2016 8:42 am
by GekkoP
^ Thanks for the clarification.
That "git config" line is easier and quicker. Although I prefer editing the config by myself to see what's going on.

Re: HOWTO: Quick Git Guide

Posted: Sat Apr 16, 2016 9:46 pm
by sgtbigman
I recently came across yet another post on git aliases (and actually decided to put them in my .gitconfig this time).

Here's what I snagged from the site, GitHowTo:
(Add to your .gitconfig)

Code: Select all

[alias]
	co = checkout
	ci = commit
	st = status
	br = branch
	hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
Now I just need to remember to use them :)