Skip to content

datascibox/github_reference

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Table of Contents





About this repository

[back to top]

This repository is meant to be a collection of useful commands, tools, and tips & tricks around Git and GitHub.
There are a lot of the not-so-basic things I have to look up in certain cases, or particular questions people are asking me. This repository should serve as a place to collect all those things.

I am looking forward to your contributions, suggestions, and ideas

[back to top]

If you have any suggestions or want to make additions, I would be very happy if you could send me an email, leave me a message on google+, or even send me a tweet on twitter (given you can fit it within the 140 character limit ;)).
Or even better: It would be great if you would simply fork this project and send me a pull request.







Pull and fetch & merge changes

[back to top]



Automatic pull for subdirectories

[back to top]

Shell command to pull changes from the remote for all subdirectories in the current directory.

find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \;

For your convenience - since the syntax may not be easy to remember - you can put this command into a shell script and save it into your main GitHub directory to conveniently pull all changes for all your contained GitHub repositories via

sh pull_all.sh


Force to overwrite local files

[back to top]

Replace local changes with most recent contents from HEAD.

git checkout -- <file>

Fetches the most recent files from the remote and resets the master branch to those by overwriting all local changes.

git fetch --all
git reset --hard origin/master


Purging a file or directory from the entire git history

[back to top]

Deleting a file or directory from the Git history is - in my opinion - was an option that was not intended by its creator. However, sometimes large binary files can cause a repository to grow over multiple Gigabytes in size, and in this case it might be useful to delete those.

git filter-branch --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch MY-BIG-DIRECTORY-OR-FILE' --tag-name-filter cat -- --all

(for more info, see the original article)

After that, you might want to commit your changes and clone the "slimmer" repository:

git clone --no-hardlinks file:///Users/yourUser/your/full/repo/path repo-clone-name	


Connecting to GitHub and GitLab

[back to top]



###Connecting to GitHub and GitLab servers via SSH [back to top]

If you want to communicate to the GitHub/GitLab servers via a "more secure" connections or if your are using a terminal that doesn't support https.



Branching

[back to top]



Branching Basics

[back to top]

Create a new branch called "develop"

git branch develop

Switch to new branch

git checkout develop

Alternative shorthand for creating and switching:

git checkout -b develop

Delete the branch

git branch -d develop

Push branch to the remote server:

git push origin develop

Preview differences between branches

git diff <source_branch> <target_branch>

Merge changes from new branch back into the master branch

git checkout master
git merge develop


Committing Changes

[back to top]



Committing Basics

[back to top]

Add individual files

git add <file>

Recursively add everything in the current directory

git add .

Commit changes

git commit -m 'some commit message'

Shorthand for adding all files and committing them in 1 command

git commit -a -m 'some commit message'


Remove files from commit

[back to top]

Removes staged (added files from the commit)

git rm --cached <file>


View the commit log with different levels of detail

[back to top]

  1. one line

    $git log --pretty=oneline

    7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c changes to a.txt ca6b353428d78dd3d8f03f03ee6128df703462d9 created a.txt and b.txt

  2. short

    $git log --pretty=short

    commit 7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c Author: rasbt [email protected]

    changes to a.txt

  3. full

    $git log --pretty=full

    commit 7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c Author: rasbt [email protected] Commit: rasbt [email protected]

    changes to a.txt

  4. fuller

    $git log --pretty=fuller

    commit 7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c Author: rasbt [email protected] AuthorDate: Sat Jun 7 11:34:00 2014 -0400 Commit: rasbt [email protected] CommitDate: Sat Jun 7 11:34:00 2014 -0400

    changes to a.txt



Summarizing/squashing commits

[back to top]

Warning: This should only be done for local commits that you haven't been uploaded to a remote, yet.

Here, we want to squash those 3 commits into 1 single one to keep the commit history "lean":

$git log --pretty=oneline

d3abc1933ed9782ddae0ef07505b2a0cd54dbe77 changes to a again
7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c changes to a.txt
ca6b353428d78dd3d8f03f03ee6128df703462d9 created a.txt and b.txt

We will use rebase for the HEAD (latest commit) up to 3 commits ago HEAD~3

$git rebase -i HEAD~3

And editor will open with the following example contents:

pick 3abc193 changes to a again
pick 7d8d551 changes to a.txt
pick ca6b353 created a.txt and b.txt

# Rebase d3abc1..4dbe77 onto 60709da
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Now, we change the first 3 lines to summarize all 3 commits into the latest commit:

pick 3abc193 changes to a again
squash 7d8d551 changes to a.txt
squash ca6b353 created a.txt and b.txt

After we save and close the editor, a new editor window will open where we can modify the commit message and the rebase should be successfully completed.





Links to useful external resources

[back to top]

Git Cheat Sheet
This cheat sheet is probably something that you want to print out and pin to your wall right next to your monitor: All the most important Git commands on one page.

git - the simple guide
A very brief overview of the Git essentials as it headline says: "just a simple guide for getting started with git. no deep shit ;)".
In my opinion it is a little bit too "concise" to be a good introduction to Git for absolute beginners, but I think that this is a very good look-up reference for people who are just getting started.

Pro Git
Probably the most thorough resource about Git you can find. Its 200+ pages (in the free PDF eBook version) might be a little bit overwhelming for Git newbies, but I would still recommend the first 2 chapter as a good introduction to Git - and the rest of the book to the more advanced users.

help.github.com
GitHub help pages are especially useful for everything that is about setting up a connection to the GitHub servers and managing your repositories.



About

A collection of useful GitHub commands and procedures

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%