• Stars
    star
    294
  • Rank 141,303 (Top 3 %)
  • Language
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This is the supplementary material for "How to Use GitHub" By Siraj Raval on Youtube

Overview

These are a list of useful commands from this video on Youtube by Siraj Raval.

Useful Git Commands

About it

Have you recently started using Git? This should give you the base commands you need to perform the most common actions in Git. If you find a command that is not here, or could be explained better, please don't hesitate in * Contributing. Cheers!

Table of contents

Git

Git is a distributed version control system, very easy to learn and supper fast!

Install Git

There are a few different ways to install git (from source or for Linux) but the purpose of this page is to focus on git commands, so I am going to assume you are installing git on a Mac.

To view other ways of installing it visit the Git official site

Click here to download and install Git

Setting up git
$ git config --global user.name "User Name"

$ git config --global user.email "email"
Applying colour to git
$ git config --global color.ui true
Initializing a repository in an existing directory

If you’re starting to track an existing project in Git, you need to go to the project’s directory and type:

$ git init

This creates a new subdirectory named .git that contains all of your necessary repository files β€” a Git repository skeleton. At this point, nothing in your project is tracked yet.

To start version-controlling existing files you should start by tracking those files and do an initial commit. To accomplish that you should start with a few $ git add that specifies the files you want to track followed by a commit.

$ git add <file>
$ git add README
$ git commit -m 'Initial project version'

Checking the status of your files

The main tool you use to determine which files are in which state is the $ git status command. If you run this command directly after a clone, you should see something like this:

$ git status
# On branch master
nothing to commit (working directory clean)

If you add a new file to your project, and the file didn't exist before, when you run a $ git status you should see your untracked file like this:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   README
nothing added to commit but untracked files present (use "git add" to track)

Staging files

After initializing a git repository in the chosen directory, all files will now be tracked. Any changes made to any file will be shown after a $ git status as changes not staged for commit.

To stage changes for commit you need to add the file(s) - or in other words, stage file(s).

# Adding a file
$ git add filename

# Adding all files
$ git add -A

# Adding all files changes in a directory
$ git add .

# Choosing what changes to add (this will got through all your changes and you can 'Y' or 'N' the changes)
$ git add -p

Stashing files

Git stash is a very useful command, where git will 'hide' the changes on a dirty directory - but no worries you can re-apply them later. The command will save your local changes away and revert the working directory to match the HEAD commit.

# Stash local changes
$ git stash

# Stash local changes with a custom message
$ git stash save "this is your custom message"

# Re-apply the changes you saved in your latest stash
$ git stash apply

# Re-apply the changes you saved in a given stash number
$ git stash apply stash@{stash_number}

# Drops any stash by its number
$ git stash drop stash@{0}

# Apply the stash and then immediately drop it from your stack
$ git stash pop

# 'Release' a particular stash from your list of stashes
$ git stash pop stash@{stash_number}

# List all stashes
$ git stash list

# Show the latest stash changes
$ git stash show

# See diff details of a given stash number
$ git diff stash@{0}

Committing files

After adding/staging a file, the next step is to commit staged file(s)

# Commit staged file(s)
$ git commit -m 'commit message'

# Add file and commit
$ git commit filename -m 'commit message'

# Add file and commit staged file
$ git commit -am 'insert commit message'

# Amending a commit
$ git commit --amend 'new commit message' or no message to maintain previous message

# Squashing commits together
$ git rebase -i
This will give you an interface on your core editor:
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

# Squashing commits together using reset --soft
$ git reset --soft HEAD~number_of_commits
$ git commit
** WARNING: this will require force pushing commits, which is OK if this is on a branch before you push to master or create a Pull Request.

Branching and merging

# Creating a local branch
$ git checkout -b branchname

# Switching between 2 branches (in fact, this would work on terminal as well to switch between 2 directories - $ cd -)
$ git checkout -

# Pushing local branch to remote
$ git push -u origin branchname

# Deleting a local branch - this won't let you delete a branch that hasn't been merged yet
$ git branch -d branchname

# Deleting a local branch - this WILL delete a branch even if it hasn't been merged yet!
$ git branch -D branchname

# Remove any remote refs you have locally that have been removed from your remote (you can substitute <origin> to any remote branch)
$ git remote prune origin

# Viewing all branches, including local and remote branches
$ git branch -a

# Viewing all branches that have been merged into your current branch, including local and remote
$ git branch -a --merged

# Viewing all branches that haven't been merged into your current branch, including local and remote
$ git branch -a --no-merged

# Viewing local branches
$ git branch

# Viewing remote branches
$ git branch -r

# Rebase master branch into local branch
$ git rebase origin/master

# Pushing local branch after rebasing master into local branch
$ git push origin +branchname

Fetching and checking out remote branches

# This will fetch all the remote branches for you.
$ git fetch origin

# With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:
$ git checkout -b test origin/test

# Deleting a remote branch
$ git branch -rd origin/branchname
$ git push origin --delete branchname  or  $ git push origin:branchname

Merging branch to trunk/master

# First checkout trunk/master
$ git checkout trunk/master

# Now merge branch to trunk/master
$ git merge branchname

# To cancel a merge
$ git merge --abort

Updating a local repository with changes from a Github repository

$ git pull origin master

Tracking existing branch

$ git branch --set-upstream-to=origin/foo foo

Resetting

# Mixes your head with a give sha
# This lets you do things like split a commit
$ git reset --mixed [sha]

# Upstream master
$ git reset HEAD origin/master -- filename

# The version from the most recent commit
$ git reset HEAD -- filename

# The version before the most recent commit
$ git reset HEAD^ -- filename

# Move head to specific commit
$ git reset --hard sha

# Reset the staging area and the working directory to match the most recent commit. In addition to unstaging changes, the --hard flag tells Git to overwrite all changes in the working directory, too.
$ git reset --hard

Git remote

# Show where 'origin' is pointing to and also tracked branches
$ git remote show origin

# Show where 'origin' is pointing to
$ git remote -v

# Change the 'origin' remote's URL
$ git remote set-url origin https://github.com/user/repo.git

# Add a new 'origin'
# Usually use to 'rebase' from forks
$ git remote add [NAME] https://github.com/user/fork-repo.git

Git grep

# 'Searches' for parts of strings in a directory
$ git grep 'something'

# 'Searches' for parts of strings in a directory and the -n prints out the line numbers where git has found matches
$ git grep -n 'something'

# 'Searches' for parts of string in a context (some lines before and some after the grepped term)
$ git grep -C<number of lines> 'something'

# 'Searches' for parts of string and also shows lines BEFORE the grepped term
$ git grep -B<number of lines> 'something'

# 'Searches' for parts of string and also shows lines AFTER the grepped term
$ git grep -A<number of lines> 'something'

Git blame

# Show alteration history of a file with the name of the author
$ git blame [filename]

# Show alteration history of a file with the name of the author && SHA
$ git blame [filename] -l

Git log

# Show a list of all commits in a repository. This command shows everything about a commit, such as commit ID, author, date and commit message.
$ git log

# List of commits showing commit messages and changes
$ git log -p

# List of commits with the particular expression you are looking for
$ git log -S 'something'

# List of commits by author
$ git log --author 'Author Name'

# Show a list of commits in a repository in a more summarised way. This shows a shorter version of the commit ID and the commit message.
$ git log --oneline

# Show a list of commits in a repository since yesterday
$ git log --since=yesterday

# Shows log by author and searching for specific term inside the commit message
$ git log --grep "term" --author "name"

Checking what you are committing

# See all (non-staged) changes done to a local repo
$ git diff

# See all (staged) changes done to a local repo
$ git diff --cached

# Check what the changes between the files you've committed and the live repo
$ git diff --stat origin/master

Useful commands

# Check if a sha is in production
$ git tag --contains [sha]

# Number of commits by author
$ git shortlog -s --author 'Author Name'

# List of authors and commits to a repository sorted alphabetically
$ git shortlog -s -n

# List of commit comments by author
$ git shortlog -n --author 'Author Name'
# This also shows the total number of commits by the author

# Number of commits by contributors
$ git shortlog -s -n

# Undo local changes to a File
$ git checkout -- filename

# Shows more detailed info about a commit
$ git cat-file sha -p

# Show number of lines added and removed from a repository by an author since some time in the past.
$ git log --author="Author name" --pretty=tformat: --numstat --since=month | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

Useful alias

To add an alias simply open your .gitconfig file on your home directory and include the alias code

# Shows the log in a more consisted way with the graph for branching and merging
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push -u origin my-new-feature
  5. Submit a pull request - cheers!

More Repositories

1

Learn_Machine_Learning_in_3_Months

This is the code for "Learn Machine Learning in 3 Months" by Siraj Raval on Youtube
7,557
star
2

learn_math_fast

This is the Curriculum for "How to Learn Mathematics Fast" By Siraj Raval on Youtube
Python
3,183
star
3

Learn_Data_Science_in_3_Months

This is the Curriculum for "Learn Data Science in 3 Months" By Siraj Raval on Youtube
2,701
star
4

Learn_Deep_Learning_in_6_Weeks

This is the Curriculum for "Learn Deep Learning in 6 Weeks" by Siraj Raval on Youtube
2,680
star
5

Learn_Computer_Science_in_5_Months

This is the Curriculum for "Learn Computer Science in 5 Months" By Siraj Raval on Youtube
1,856
star
6

Learn_Blockchain_in_2_months

This is the code for "Learn Blockchain in 2 Months" by Siraj Raval on Youtube
1,725
star
7

YOLO_Object_Detection

This is the code for "YOLO Object Detection" by Siraj Raval on Youtube
Python
1,687
star
8

tensorflow_chatbot

Tensorflow chatbot demo by @Sirajology on Youtube
Python
1,422
star
9

Learn_Computer_Vision

This is the curriculum for "Learn Computer Vision" by Siraj Raval on Youtube
1,078
star
10

Learn-Natural-Language-Processing-Curriculum

This is the curriculum for "Learn Natural Language Processing" by Siraj Raval on Youtube
1,054
star
11

Machine_Learning_Journey

This is the Curriculum for "Machine Learning Journey" By Siraj Raval on Youtube
973
star
12

deepfakes

This is the code for "DeepFakes" by Siraj Raval on Youtube
Python
946
star
13

100_Days_of_ML_Code

These are the instructions for "100 Days of ML Code" By Siraj Raval on Youtube
865
star
14

How-to-Predict-Stock-Prices-Easily-Demo

How to Predict Stock Prices Easily - Intro to Deep Learning #7 by Siraj Raval on Youtube
Jupyter Notebook
764
star
15

Programming_Interview_Study_Plan

This is the Programming Interview Study Plan by Siraj Raval on Youtube
689
star
16

Reinforcement_Learning_for_Stock_Prediction

This is the code for "Reinforcement Learning for Stock Prediction" By Siraj Raval on Youtube
Python
610
star
17

ChatGPT_Trading_Bot

This is the code for the "ChatGPT Trading Bot" Video by Siraj Raval on Youtube
Jupyter Notebook
587
star
18

capsule_networks

This is the code for "Capsule Networks: An Improvement to Convolutional Networks" by Siraj Raval on Youtube
Python
569
star
19

tensorflow_image_classifier

TensorFlow Image Classifier Demo by @Sirajology on Youtube
Python
530
star
20

Make_Money_with_Tensorflow_2.0

This is the code for "Make Money with Tensorflow 2.0" by Siraj Raval
Jupyter Notebook
527
star
21

LearnML

This is the Study Guide for Learn Machine Learning in 3 Months (PyTorch Curriculum) by Siraj Raval on Youtube
520
star
22

Mathematics_for_Beginners

This is the formula sheet for "Mathematics for Beginners" by Siraj Raval on Youtube
484
star
23

How_to_make_a_text_summarizer

This is the code for "How to Make a Text Summarizer - Intro to Deep Learning #10" by Siraj Raval on Youtube
Jupyter Notebook
481
star
24

Pokemon_GAN

This is the code for "Generating Pokemon with a Generative Adversarial Network" by Siraj Raval on Youtube
Python
432
star
25

Watch-Me-Build-a-Trading-Bot

This is the code for "Watch Me Build a Trading Bot" by Siraj Raval on Youtube
JavaScript
419
star
26

Learn_Physics_in_2_Months

This is the curriculum for "Learn Physics in 2 Months" by Siraj Raval on Youtube
414
star
27

How_to_simulate_a_self_driving_car

This is the code for "How to Simulate a Self-Driving Car" by Siraj Raval on Youtube
Python
413
star
28

Your_First_Decentralized_Application

This is the code for "A Guide to Building Your First Decentralized Application" by Siraj Raval on Youtube
Jupyter Notebook
413
star
29

AI_in_Finance

This is the code for "AI in Finance" By Siraj Raval on Youtube
JavaScript
410
star
30

The_Math_of_Intelligence

This is the Syllabus for Siraj Raval's new course "The Math of Intelligence"
394
star
31

tensorflow_speech_recognition_demo

This is the code for 'How to Make a Simple Tensorflow Speech Recognizer' by @Sirajology on Youtube
Python
383
star
32

Convolutional_neural_network

This is the code for "Convolutional Neural Networks - The Math of Intelligence (Week 4)" By Siraj Raval on Youtube
Jupyter Notebook
368
star
33

AI_Startup_Prototype

This is the code for "Watch Me Build an AI Startup" By Siraj Raval on Youtube
Python
364
star
34

Neural_Network_Voices

This is the code for "Neural Network Voices" by Siraj Raval on Youtube
Python
358
star
35

A_Guide_to_Running_Tensorflow_Models_on_Android

This is the code for"A Guide to Running Tensorflow Models on Android" By SIraj Raval on Youtube
Java
344
star
36

pose_estimation

This is the code for "Webcam Tracking with Tensorflow.js" By Siraj Raval on Youtube
TypeScript
318
star
37

Everybody_Dance_Now

This is the code for "Everybody Dance Now!" By Siraj Raval on Youtube
Python
313
star
38

Q-Learning-for-Trading

Python
311
star
39

ethereum_future

This is the Code for "Ethereum Future Prices" by Siraj Raval on Youtube
Jupyter Notebook
299
star
40

AI_Composer

AI Composer for Machine Learning for Hackers #2
Python
294
star
41

how_to_deploy_a_keras_model_to_production

This is the code for the "How to Deploy a Keras Model to Production" by Siraj Raval on Youtube
Python
288
star
42

predicting_stock_prices

This is the coding challenge for "Predicting Stock Prices" by @Sirajology on Youtube
Python
282
star
43

How-to-Deploy-a-Tensorflow-Model-in-Production

This is the code for the "How to Deploy a Tensorflow Model in Production" by Siraj Raval on YouTube
Python
280
star
44

linear_regression_live

This is the code for the "How to Do Linear Regression the Right Way" live session by Siraj Raval on Youtube
Python
269
star
45

Chatbot-AI

Chatbot AI for Machine Learning for Hackers #6
Lua
261
star
46

Predicting_Winning_Teams

This is the code for "Predicting the Winning Team with Machine Learning" by Siraj Raval on Youtube
Jupyter Notebook
249
star
47

Music_Generator_Demo

Music Generator Demo by @Sirajology on Youtube
Python
246
star
48

Data_Science_Interview_Guide

These are the tips for "5 Steps to Pass Data Science Interviews" By Siraj Raval on Youtube
245
star
49

quantum_machine_learning

This is the code for "Quantum Machine Learning" By Siraj Raval on Youtube
HTML
245
star
50

AI_Freelancing

This is the code for "How to Do Freelance AI Programming" By Siraj Raval on Youtube
245
star
51

AI_Artist

AI Artist for Machine Learning for Hackers #5
Python
237
star
52

bitcoin_prediction

This is the code for "Bitcoin Prediction" by Siraj Raval on Youtube
Jupyter Notebook
234
star
53

tensorflow_demo

Tensorflow Demo for my TF in 5 Min Video on Youtube
Python
227
star
54

Neural_Differential_Equations

This is the code for "Neural DIfferential Equations" By Siraj Raval on Youtube
Jupyter Notebook
225
star
55

Stock_Market_Prediction

This is the code for "Stock Market Prediction" by Siraj Raval on Youtube
Jupyter Notebook
218
star
56

Build-an-AI-Startup-with-PyTorch

This is the code for 'Build an AI Startup with Pytorch" by Siraj Raval
Java
217
star
57

ChatGPT_Sports_Betting_Bot

This is the code for "I Built a Sports Betting Bot with ChatGPT" by Siraj Raval on Youtube
Jupyter Notebook
216
star
58

Move_37_Syllabus

This is the syllabus for "Move 37", Siraj Raval's new course at School of AI
215
star
59

Classifying_Data_Using_a_Support_Vector_Machine

This is the code for the "Classifying Data using Gradient Descent" by Siraj Raval on Youtube
Jupyter Notebook
212
star
60

A-Guide-to-DeepMinds-StarCraft-AI-Environment

This is the code for "A Guide to DeepMind's StarCraft AI Environment" by Siraj Raval on Youtube
Python
210
star
61

Landing-a-SpaceX-Falcon-Heavy-Rocket

This is the code for "Landing a SpaceX Falcon Heavy Rocket" By Siraj Raval on Youtube
Python
209
star
62

How_to_Build_a_healthcare_startup

This is the code for "How to Build a Healthcare Startup" by Siraj Raval on Youtube
Dart
203
star
63

AI_For_Music_Composition

This is the code for "AI for Music Composition" by Siraj Raval on Youtube
Python
202
star
64

How_to_make_a_chatbot

This is the code for "How to Make a Chatbot - Intro to Deep Learning #12' by Siraj Raval on YouTube
Python
199
star
65

LSTM_Networks

This is the code for "LSTM Networks - The Math of Intelligence (Week 8)" By Siraj Raval on Youtube
Jupyter Notebook
194
star
66

Make_a_neural_network

This is the code for the "Make a Neural Network" - Intro to Deep Learning #2 by Siraj Raval on Youtube
Python
193
star
67

AI_For_Business_Curriculum

This is the curriculum for the "AI for Business" Course By Siraj Raval on Youtube
189
star
68

Time_Series_Prediction

This is the code for "Time Series Prediction" By Siraj Raval on Youtube
Jupyter Notebook
187
star
69

3D_Pose_Estimation

This is the code for "Machine Vision" By Siraj Raval on Youtube
Python
185
star
70

Financial_Forecasting_with_TensorflowJS

This is the code for "Financial Forecasting with Tensorflow.js" By Siraj Raval on Youtube
JavaScript
184
star
71

linear_regression_demo

This is the code for "How to Make a Prediction - Intro to Deep Learning #1' by Siraj Raval on YouTube
Python
182
star
72

ethereum_demo

This is the code for "Ethereum Explained" by Siraj Raval on Youtube
Jupyter Notebook
178
star
73

chatbot_tutorial

This is the code for "Chatbot Tutorial" by Siraj Raval on Youtube
Python
176
star
74

Watch-Me-Build-a-Finance-Startup

This is the code for "Watch Me Build a Finance Startup" by Siraj Raval on Youtube
Java
172
star
75

word_vectors_game_of_thrones-LIVE

This is the code for the "How to Make Word Vectors from Game of Thrones (LIVE) " Siraj Raval on Youtube
Jupyter Notebook
170
star
76

AI_in_Medicine_Clinical_Imaging_Classification

This is the code for "AI in Medicine " By Siraj Raval on Youtube
Python
165
star
77

deep_q_learning

This is the Code for "Deep Q Learning - The Math of Intelligence #9" By Siraj Raval on Youtube
Jupyter Notebook
163
star
78

AI_Writer

AI Writer for Machine Learning for Hackers #8
Python
163
star
79

Bitcoin_Trading_Bot

This is the code for "Bitcoin Trading Bot" By Siraj Raval on Youtube
Jupyter Notebook
163
star
80

Unity_ML_Agents

This is the code for "Unity AI" by Siraj Raval on Youtube
Python
161
star
81

how_to_convert_text_to_images

This is the code for "How to Convert Text to Images - Intro to Deep Learning #16' by Siraj Raval on YouTube
Python
155
star
82

Game-AI

Game AI for Machine Learning for Hackers #3
Python
154
star
83

Intro_to_the_Math_of_intelligence

This is the code for "Intro - The Math of Intelligence" by Siraj Raval on Youtube
Python
152
star
84

Sentiment_Analysis

This is the code for "Sentiment Analysis - Data Lit #1" by Siraj Raval on Youtube
Jupyter Notebook
151
star
85

recommender_live

Jupyter Notebook
149
star
86

recurrent_neural_network

This is the code for "Recurrent Neural Networks - The Math of Intelligence (Week 5)" By Siraj Raval on Youtube
Jupyter Notebook
147
star
87

How-to-Learn-from-Little-Data

This is the code for "How to Learn from Little Data - Intro to Deep Learning #17' by Siraj Raval on YouTube
Python
144
star
88

How_to_generate_music_in_tensorflow_LIVE

Python
140
star
89

Kaggle_Earthquake_challenge

This is the code for the Kaggle Earthquake Challenge by Siraj Raval on Youtube
Jupyter Notebook
139
star
90

OpenAI_Five_vs_Dota2_Explained

This is the code for "OpenAI Five vs DOTA 2 Explained" By Siraj Raval on Youtube
Python
138
star
91

Learn_Synthetic_Biology

137
star
92

How-to-Build-a-Biomedical-Startup

This is the code for "How to Build a Biomedical Startup" by Siraj Raval on Youtube
Dart
134
star
93

Gaussian_Mixture_Models

This is the code for "Gaussian Mixture Models - The Math of Intelligence (Week 7)" By Siraj Raval on Youtube
Jupyter Notebook
134
star
94

Make_Money_with_Tensorflow

This is the code for "Make Money with Tensorflow" by Siraj Raval on Youtube
Python
129
star
95

Machine-Learning-API-Tutorial

This is the code for "Machine Learning API tutorial" By Siraj Raval on Youtube
Python
128
star
96

AI_Supply_Chain

This is the code for "AI for Supply Chain" by Siraj Raval on Youtube
Jupyter Notebook
128
star
97

how_to_build_a_bitcoin_startup

This is the code for "How to Build a Bitcoin Startup" by Siraj Raval on Youtube
JavaScript
126
star
98

machine_learning_and_neuroscience

This is the code for "Machine Learning & Neuroscience" By Siraj Raval on Youtube
JavaScript
123
star
99

k_means_clustering

This is the code for "K-Means Clustering - The Math of Intelligence (Week 3)" By SIraj Raval on Youtube
Jupyter Notebook
122
star
100

alphago_demo

This is the code for "How Does DeepMind's AlphaGo Zero Work?" Siraj Raval on Youtube
Python
120
star