Katte.io
Published in

Katte.io

Getting along with Git & Github

A no non-sense reference for developers by a developer.

Photo by Annie Theby on Unsplash

But first, make yourself comfortable with the terminal

So then what is git?

Arent git and GitHub the same?

OK, but what is a repository (a.k.a Git Repo)?

What is a commit?

Do I need to install git?

The basics — foundational commands

How to initialize a git repository?

$ git init <name of repo>
$ git init .

Let’s understand the Git States

Check the current status

$ git status

Add selected file to the staging area

$ git add <file name>
$ git add .

Committing changes to git

$ git commit -m "Commit message"
$ git commit

How to see the commit history

$ git log
$ git show

List all files git is tracking

$ git ls-files

Express commit to already tracked files

$ git commit -am "commit message"
$ git reset HEAD filename.ext

Reverting back to the last good commit for a file

git checkout -- filename.txt

Git help command

$ git help commandname

A better way of seeing commit history

$ git log --oneline --graph --decorate --all

Create your own git history command with git Alias

$ git config --global alias.hist "log --oneline --graph --decorate --all"
$ git hist
$git hist -- filename.ext

Show git global config file

$git config --global --list

Rename, move and delete files

$git mv filename.ext newfilename.ext
$git rm filename.ext

Renaming and deleting files outside git using OS commands

$ git add -A
$ git add -u

Excluding unwanted files with .gitignore

Advanced Git commands

Checking the difference between two commits

$ git diff commitid commitid
$git difftool commitid commitid

Branching and merging

Types of merge

Special markers

Simple branching

$ git branch
$git checkout -b branchname

Merging: Integrating changes from any branch to the main branch

$ git checkout branchname
$git merge newbranch
$ git branch -d nameofbranch

Marking special events with tagging

$ git tag tagname
$ git tag --list
$ git tag -d tagname
$ git tag -a tagname -m "commit message"
$ git show tagname

Saving your unfinished work

$ git stash
$ git stash list
$ git stash pop

Time travel with reset and reflog

$ git reset a816c62 --soft
$ git reset d4a3d08 --mixed
$ git reset d4a3d08 --hard
$ git reflog

Working with Github

Adding an existing local repo to GitHub.

$git remote -v
$ git remote add origin git@github.com:gitusername/reponame.git

Pushing changes to GitHub

$ git push -u origin main --tags

Cloning a git repo

$ git clone https://repo url

Pushing back changes to Github

$ git push origin main

Fetch and pull

$ git fetch
$ git pull

Updating reference to the origin

$ git remote set-url origin newrepourl

Show the details on remote

$ git remote show origin

Pushing local branches to Github

$ git push -u origin branchname 

Prune dead branches

$ git fetch -p

Conclusion

--

--

Technology and Leadership for Humans

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shashank Katte

Full-Stack Developer and Agile-DevOps Coach from Toronto. I write about programming, technology and leadership | katte.io