git init <dir> git clone <repoURL> git config user.name <name> git add <file or dir> git commit -m "<message>" git commit --amend git tag -a TagName -m "TagMessage"
Git Guide
Commonly Used Commands
Important Configurations
Use a global .gitignore
file:
git config --global core.excludesfile ~/.config/git/.gitignore
Set Git User details:
git config --global user.name "Michael Habib" git config --global user.email 1233256+MichaelHabib@users.noreply.github.com git config --global push.default simple git config --global core.editor vim
Command Samples
git status && git add -A && git commit -a -m "updated …" && git push _origin _master
git pull origin master --allow-unrelated-histories
Pull remote with unrelated histories into a local repo. (follow with git push origin master -f
to force upload to remote.
Basic Snapshotting
Command | Description |
---|---|
|
Check status |
|
Add a file to the staging area |
|
Add all new and changed files to the staging area |
|
Commit changes |
|
Remove a file (or folder) |
|
Remove all files or specified file. |
|
Untrack file or folder. |
Branching & Merging
Command | Description |
---|---|
|
List branches (the asterisk denotes the current branch) |
|
List all branches (local and remote) |
|
Create a new branch |
|
Delete a branch |
|
Delete a remote branch |
|
Create a new branch and switch to it |
|
Clone a remote branch from |
|
Switch to a branch |
|
Switch to the branch last checked out |
|
Discard changes to a file |
|
Merge a branch into the active branch |
|
Merge a branch into a target branch |
|
Stash the changes in a dirty working directory away |
|
Remove all the stashed states |
Sharing & Updating Projects
Command | Description |
---|---|
|
Push a branch to your remote repository |
|
Push changes to remote repository (and remember the branch) |
|
Push changes to remote repository (remembered branch) |
|
Delete a remote branch |
|
Update local repository to the newest commit |
|
Pull changes from remote repository |
|
Add a remote repository |
|
Set a repository’s origin branch to SSH |
Tags & Releases
Command |
Description |
|
add tag locally only |
|
push all tags |
|
push a specific tag |
|
Delete local tag |
|
Delete REMOTE tag |
Git Submudules
sample .gitmodules
files
More about Git Submodules
Inspection & Comparison
Command | Description |
---|---|
|
View changes |
|
View changes (detailed) |
|
Preview changes before merging |
Scenarios
Delete repo files
To remove any file or folder from a local or remote branch
- run git rm -r --cached LocalFileOrFolderName
to remove the files
- commit then push to remote branch .
This will remove the files but will NOT alter the repo history, which means the files will be accessable from previous commits & branches .
Global .gitignore
Template
# gitignore Template by MichaelHabib # www.michaelhabib.name ## Project Specific Rules ################################################## **/node_modules_local **/vendor_local ## IDE & Dev Tools File to ignore ################################################## **/nbproject **/*.sublime-* **/.project **/Vagrantfile **/.vagrant* **/.idea/ ## PHP files and folders ################################################## **/vendor ## Common developer tools ################################################## **/composer.phar **/php-cs-fixer.phar **/scrutinizer.phar ## Node & Front-end Dev ################################################## **/.sass-cache **/bower_components **/node_modules **/npm-debug.log ## File-system cruft and temporary files ################################################## **/.*.swp **/.buildpath **/.swp **/__* ## OS generated files # ################################################## **/.DS_Store **/.DS_Store? **/._* **/.Spotlight-V100 **/.Trashes **/ehthumbs.db **/Thumbs.db ## Packages # ################################################## # it's better to unpack these files and commit the raw source # git has its own built in compression methods **/*.7z **/*.dmg **/*.gz **/*.iso **/*.jar **/*.rar **/*.tar **/*.zip ## Credit & Sources ## - https://gist.github.com/octocat/9257657 ## - https://stackoverflow.com/questions/18393498/gitignore-all-the-ds-store-files-in-every-folder-and-subfolder