HOWTO: Quick Git Guide

Forum rules
Share your brain ;)
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

HOWTO: Quick Git Guide

Unread post by machinebacon » Fri Feb 20, 2015 9:21 am

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
..gnutella..

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: HOWTO: Quick Git Guide

Unread post by wuxmedia » Fri Feb 20, 2015 9:42 am

nice, should use this more, I clone like crazy, but fail at updating via the command line.
Thanks Both!
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: HOWTO: Quick Git Guide

Unread post by GekkoP » Mon Apr 11, 2016 9:00 am

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

User avatar
kondor
runs Stable
Posts: 35
Joined: Thu Jan 29, 2015 6:11 am

Re: HOWTO: Quick Git Guide

Unread post by kondor » Mon Apr 11, 2016 6:33 pm

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

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: HOWTO: Quick Git Guide

Unread post by wuxmedia » Tue Apr 12, 2016 7:21 am

relevant XKCD: http://xkcd.com/1597/
although, with this guide should be a sea change.
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
sgtbigman
Oyster-Slurper
Posts: 25
Joined: Sun Jan 24, 2016 2:31 am

Re: HOWTO: Quick Git Guide

Unread post by sgtbigman » Thu Apr 14, 2016 2:35 am

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.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: HOWTO: Quick Git Guide

Unread post by GekkoP » Thu Apr 14, 2016 8:42 am

^ 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.

User avatar
sgtbigman
Oyster-Slurper
Posts: 25
Joined: Sun Jan 24, 2016 2:31 am

Re: HOWTO: Quick Git Guide

Unread post by sgtbigman » Sat Apr 16, 2016 9:46 pm

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 :)

Post Reply