Skip to content

A collection of useful GitHub commands and procedures

License

Notifications You must be signed in to change notification settings

rtbarber/github_reference

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 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.







Collaborating

[back to top]



The Pull-Request workflow

[back to top]

Below, you'll find a brief overview of the pull-request workflow: The general workflow for collaborating on GitHub.

1)

The creator creates a new project (e.g., "https://github.com/creator/hello-world.git").

2)

A developer forks the project to his own GitHub account (e.g., "https://github.com/developer/hello-world.git") via the web interface.

3)

Next, the developer clones the forked GitHub repository to his local machine.

  • git clone https://github.com/developer/hello-world.git

4)

The developer adds an "upstream" remote to get any changes in the master branch from the original project.

  • git remote add upstream https://github.com/creator/hello-world.git

Now, changes from the master branch of the original project can be pulled and merged via

  • git fetch upstream
  • git merge upstream/master

5)

The developer creates a new branch to implement a new feature

  • git branch featX (creates new branch)
  • git checkout featX (switches to new branch in order to work on it)

After you made changes in the featX branch, push it to your remote.

  • git add .
  • git commit -m 'implemented feature X'
  • git push origin featX

6)

Now, the developer can send the creator a pull request (via the web interface), and the creator merges the topic branch (here: featX) into the master branch after reviewing the changes.



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	


Keeping both files separate at merge conflict

[back to top]

 * branch            master     -> FETCH_HEAD
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

In cases of an automatic merge conflicts which causes hundreds or thousands of conflicts to fix manually, you maybe want to decide to keep both files separate, or either one of them (without the conflict notes inserted).

Here is how to keep your current local file:

git checkout --ours README.md 

And this command can be used to keep just the file that was about to merged in:

git checkout --theirs README.md

Auto-merged index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result. There’s two unmerged files here. According to the git checkout manpage, there’s a --theirs and --ours options on the command. The former will keep the version of the file that you merged in, and the other will keep the original one we had. The following commands will keep the original file for index.html, and then use the merged in file only for _layouts/default.html. git checkout --ours index.html git checkout --theirs _layouts/default.html



Getting updates from the original project after forking

[back to top]

  1. Add the original repository as a remote

    cd onyourlocaldrive/repo_name git remote add upstream git://github.com/username/repo_name.git

  2. Then you can pull from it via:

    git fetch upststream git merge upstream/master master



Resetting a remote back to the last push

[back to top]

Accidents happen: Let's say we pushed changes accidentally to the remote (here: master) and want to reset it to the previous state (i.e., the last push).

git reset --hard <git hash>
git push -f origin master




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