Skip to content

Instantly share code, notes, and snippets.

@marius92mc
Forked from neilgee/git.css
Last active December 4, 2015 08:56
Show Gist options
  • Save marius92mc/2e9b7e588d0fa24b1fc3 to your computer and use it in GitHub Desktop.
Save marius92mc/2e9b7e588d0fa24b1fc3 to your computer and use it in GitHub Desktop.
Git commands and tutorial
Awesome git tutorial, learn by doing in browser.
https://try.github.io/levels/1/challenges/1
/* Set up Git Configuration */
git config --global user.email "you@yourdomain.com"
git config --global user.name "Your Name"
git config --global core.editor "vi"
git config --global color.ui true
/* See Git configuration */
git config --list
/* This will unstage all files you might have staged with git add */
git reset
git reset file_name /* to unstage the file named file_name */
/* This will revert all local uncommitted changes (should be executed in repo root) */
git checkout .
git checkout file_name /* to drop the modifications for the specified file */
/*
git remote add upstream <link to original repo, from where I've forked it>
git pull origin master
git checkout master
git pull upstream master
git branch develop
git checkout develop
...modify files...
git add .
git commit -m "..."
git push origin develop
...make Pull Request from github website...
git checkout master
git pull upstream master
git push origin master
git checkout develop
git merge master develop
? git add .
? git commit -m "..."
git push origin develop
...make PullRequest from github website, after importing latest commits from upstream repo...
git checkout master
git merge master develop
git branch -d develop
git add .
git commit -m "..."
git push origin master
*/
/* You can also revert uncommitted changes only to particular file or directory */
git checkout [some_dir|file.txt]
/* Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory) */
git reset --hard HEAD
/* This will remove all local untracked files, so only git tracked files remain */
/* WARNING: -x will also remove all ignored files! */
git clean -fdx
# Resets index to former commit; replace '56e05fced' with your commit code
git reset 56e05fced
# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"
# Updates working copy to reflect the new commit
git reset --hard
/*  to initialise a local repository */
git init 
/* copy git repository */
example
git clone https://github.com/marius92mc/Information_retrieval_app.git
/* adds a file to the repo */
git add .
git add <file_name/directory>
/* commit the change to git */
git commit -m "Message goes here"
/* see the status of current changes */
git status
/* push changes to remote repository */
git push origin master
git push origin <branch_name>
/* fetch latest updates from branch master */
git pull origin master
/* see the commits */
git log
git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
Or to add in ~/.gitconfig
"
[alias]
log1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
log2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git lg1"
"
and then to use
git log1
git log2
git log
/* Git has a 3 Tier Architecture: Working - Staging Index - Respository
Changes to files are put in a Checksum SHA-1 hash 40digit value containing parent hash, author and message.
HEAD is the latest commit of the checked out branch */
/* Basic Commands */
git status /* the command 'git status' tells which files are not added or committed from Working to Staging to Repository */
git commit -m "" /* Commits and changes to all files that are in Staging Tier into Repository */
git diff /* show changes between Working and Repository, no file supplied shows all files */
git diff --staged /* shows changes between Staged and Respository */
git rm file.txt /* will remove file from working then git commit -m "" to also remove from Repo */
git rm --cached file.txt /* leaves copy of file in Working but removes from Staging and Repo */
git mv /* rename or move files - then git commit -m "" to move to Repo */
git commit -am "text goes here" /* adds all files straight to Repo from Staging if they have changes - meaning they skip git add */
git checkout -- file.txt /* restore Repo file to Working Directory using current branch */
git reset HEAD file.txt /* Move a Stage file out of Stage back to Working */
git commit --amend -m "message" file.txt /* Change last commit to Repo (only last one can change) */
/* Reverting --soft --mixed --hard will go back to previous commits*/
git log /* gets the sha1s so you can see the coomits where you want revert back to */
git reset --soft sha /* changes Repo but not STaging or Working */
git reset --mixed sha /* changes Repo and STaging but not Working */
git reset --hard sha /* changes all 3 Tiers */
git clean -f /* remove untracked files from Working */
.gitignore /* ignores files to track in Working / track the .gitignore file */
Example to add in .gitignore file:
.DS_Store
details.txt
*~
*.o
*.class
build/
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
Global Ignore /* create in home folder */
.gitignore_global
/* Add in */
.DS_Store
.Trashes
.Spotlight_V100
git config --global core.excludesfile ~/.gitignore_global /* add to gitconfig */
/* Stop tracking changes */
git rm --cached file.txt /* leaves copy in Repo and Working */
/* Track Folders changes
Add an invisble file to a folder like .gitkeeper then add and commit */
/* Commit Log */
git ls-tree HEAD
git ls-tree master
git log --oneline
git log --author="Neil"
git log --grep="temp"
/* Show Commits */
git show dc094cb /* show SHA1 */
/* Compare Commits
Branches */
git branch /* Show local branches * is the one we are on */
git branch -r /* Shows remote branches */
git branch -a /* Shows local and remote */
git branch newbranch /* creates a new branch */
git checkout newbranch /* switch to new branch */
git checkout -b oldbranch /* creates and switches to new branch */
/* Diff in Branches */
git diff master..otherbranch /* shows diff */
git diff --color-words master..otherbranch /* shows diff in color */
git branch --merged /* shows any merged branches */
/* Rename Branch */
git branch -m oldname newname
/* Delete Branch */
git branch -d nameofbranch
/* Merge Branch */
git merge branchname /* be on the receiver branch */
/* Merge Conflicts between the same file on 2 branches are marked in HEAD and other branch */
git merge --abort /* Abort basically cancels the merge */
/* Manually Fix Files and commit
The Stash */
git stash save "text message here"
git stash list /* shows whats in stash */
git stash show -p stash@{0} /* Show the diff in the stash */
git stash pop stash@{0} /* restores the stash deletes the tash */
git stash apply stash@{0} /* restores the stash and keeps the stash */
git stash clear /* removes all stash */
git stash drop stash@{0}
/* Remotes
You fetch from the remote server, merge any differences - then push any new to the remote - 3 branches work remote server branch, local origin master and local master
Create a repo in GitHub, then add that remote to your local repo */
git remote add origin https://github.com/neilgee/genesischild.git /* origin can be named whatever followed by the remote */
git remote /* to show all remotes */
git remote show origin /*to see remote URL*/
git remote remove origin /* to remove remote */
git remote rm origin /* to remove remote */
/* Push to Remote from Local */
git push -u origin master
/* Cloning a GitHub Repo - create and get the URL of a new repository from GitHub, then clone that to your local repo, example below uses local repo named 'nameoffolder' */
git clone https://github.com/neilgee/genesischild.git nameoffolder
/* Push to Remote from Local - more - since when we pushed the local to remote we used -u parameter then the remote branch is tracked to the local branch and we just need to use... */
git push
/* Fetch changes from a cloned Repo */
git fetch origin /* Pulls down latest committs to origin/master not origin, also pull down any branches pushed to Repo
Fetch before you work
Fetch before you pull
Fetch often */
/* Merge with origin/master */
git merge origin/master
/* git pull = git fetch + git merge
Checkout/Copy a remote branch to local */
git branch branchname origin/branchname /* this will bring the remote branch to local and track with the remote */
/* Delete branch */
git branch -d branchname
/* Checkout and switch branch and track to remote */
git checkout -b nontracking origin/nontracking
/* Remove remote branch */
git push origin --delete branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment