Saturday, August 6, 2016

Notes on Git

Git is a distributed version control system developed by Linus Torvalds. It's basically the weapon of choice when it comes to version control these days.  So after using Subversion for the longest time, I made the jump to Git and haven't looked back.  I do tend to forget things though if I go a while in between uses and/or if OS upgrades have happened in between.  Yikes, it's been a year or so since I've pushed anything to GitHub.  In my defense, I have been and still am relying heavily on Syncthing to handle my data.  Anyhow, as of yesterday, I'm back up to speed with Git for my purposes.

Here's a few things I might find my future self forgetting again.

If I'm a dummy and forgot to save my ssh keys:
ssh-keygen -t rsa -b 4096 -C 'foo@bar.com'
If I want to put a local repository on GitHub, the first step is to create an empty repository on GitHub, and then, at the top level of my local repository, run:
git remote add origin git@github.com:AssumeACanOpener/some_project.git
Be sure to go with ssh and not https, unless you like typing usernames and passwords all the time.

On your initial push to GitHub do:
 git push -u origin master
Otherwise a pull is going to say you're not up to date, even though you are really.

In the past when I accidentally tracked files, I'd imagine I've probably just copied something, did a git rm, and then moved whatever it was back.  But there's a better way.  If you've accidentally added a file to version control, but you don't want to delete it and simply take it out of version control:
 git rm --cached file.txt
Put file names you don't want to track into a .gitignore file at the top level of your repository.  Funnily enough, you need to add .gitignore to the .gitignore file.

To check what files are currently under version control:
git ls-tree -r master --name-only
If you've forked something from github and still want to follow upstream changes, you'll need to add the upstream repository to your fork.  For example:
git remote add upstream https://github.com/gregmalcolm/python_koans.git
Then, to update:
git pull upstream master
And if I ever forget the init, add, status, and commit commands, I need to pack it up and go home.

No comments:

Post a Comment