Tutorial on Git and GitHub for Absolute Beginners
Disclaimer: This tutorial is meant for absolute beginners. I have used terminology in a way that eases understandings, and hence certain words might have a (little) different, deeper meaning associated to it.
YouTube Video Tutorial
Steps
- What is Git and GitHub
- Install Git
- Create GitHub Account
- Connect to GitHub with SSH (Optional)
- Create a Repository locally
- Make Commits
- Explore its other capabilities
- Upload your Repository to GitHub (remote)
- Use and Understand the web Interface for your remote
- Learn more and Collaborate
0. What is git
What is GitHub
1. Install Git
Download the latest package from the official git website
In case you prefer simple commands for the terminal
- Linux
- sudo apt-get install git (Ubuntu)
- sudo yum install git (Fedora)
- MacOS
- Install Brew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- brew install git
- Install Brew:
Post installation, you can configure your name, email, and default text editor (for commit messages and some other use-cases)
git config --global user.name “Your Name”
git config --global user.email “Your Email”
git config --global core.editor “nano”
2. Sign-Up on GitHub
3. Connect to GitHub with SSH (Optional)
4. Create a Repository locally
You can create a repository in a folder. It can be empty or you can even have your pre-existing files that you wish to add in that project/repository. To initialize git tracking use command:
git init
Do note that while executing the above command, your terminal should be inside the appropriate project directory
5. Make Commits
git add filename
ORgit add --all
to stage files/directories
git commit -m "Your message here"
6. Some useful commands
git status
- to check what files are on/not-on stage
- to check if there have been any uncommitted changes
- to check for untracked files
git log
to see all the commits locally
7. Hosting your repository (GitHub)
git remote add origin git@github.com:Daksh/test-git-session.git
to add the address for your remote
git remote -v
to list all the remote addresses associated with a local repository
git push origin master
to push all the local comits on your remote, master branch (default branch)
git pull origin master
pulls the new changes which are present in the github repository but aren’t present locally.
git clone url
alternatively, in case you want to download a remote repository (to make a local instance of it)
Extras :)
git diff --color-words
to see the changes in words instead of sentencesgit checkout commitHash
to temporarily switch to a branch at that particular commit, helps in testinggit stash
andgit stash apply
to undo/redo the uncommitted changesgit diff HEAD~2
to see the changes done sinceHEAD~2
(two commit before HEAD). Refer to this post for more optionsgit branch
andgit checkout branch-name
to list and change to a particular branchgit commit --amend --reset-author --no-edit
git checkout <sha-1 of that commit>
git diff --color-words --no-index publications.py publications\ \(1\).py
.gitignore
This is a special type of file, called .gitignore
. It should be placed inside the main folder of your repository (top-most folder). What this file does, is indicate to git that there are some files (or some files with particular extensions) that I wish to ignore. They will be there in my local instance but don’t ask/tell me each time that they are untracked. Ignore them!
There is an awesome website gitignore.io which helps you to create such files. Suppose you do not want any extra files that show up with python(eg. *.pyc
)/mac (eg .DS_Store
), you can just enter python
and macOS
on the website to generate the .gitignore
file gitignore.io/api/macos,python
You can simply do subl .gitignore
to open a new file and copy paste the contents from the website. As soon as you add that to git tracking (git add .gitignore
), you will see that all the other extension files which you wanted to get rid of and were being shown in untracked section, vanish! :D
Ignore certain extension files
Adding the line *.ext
in the .gitignore
file will work. But, there is a small catch, if you already have commited a file with the same extension, then git will still continue to track it. In this scenario, if you want to remove that file and stop git from tracking it, run the command git rm --cached <FileName>
Useful Links/Resources
- GitHub Students Pack
- Atlassian - Comes in handy when you have a new command at hand, but no way to comprehend it
- CheatSheets
- ProGit - High Quality, book authored by Co-Founder of GitHub. Available for free
- git - the simple guide