Git


git diff master --name-only


Diffs for the past two three commits
git diff HEAD~1 file
git diff HEAD~2 file
git diff HEAD~3 file
git diff HEAD~4 file

Stop tracking a file:
$ git update-index --assume-unchanged path/to/file
Resume tracking the file
 $ git update-index --no-assume-unchanged path/to/file


$ git log -p --color
$ git branch -av # to list all branches

Setup git

git config --global user.name "mshahriarinia" 
git config --global user.email "info@mshahriarinia.com" 
git config --global credential.helper cache 
git config --global credential.helper 'cache --timeout=3600'             # Set the cache to timeout after 1 hour (setting is in seconds)
git config --global color.ui true                                                          # for git diff to be colorfull
git config --global push.default simple  
git config --global core.editor /usr/bin/vim


Branch

$ git branch mybranch  master          # Creates a new branch called "mybranch" from the latest commit of master.
$ git checkout mybranch  # Makes "mybranch" the active branch
OR $ git checkout -b mybranch master  # Creates a new branch called "mybranch" from the latest commit of master and makes it the active branch

$ git push --all    #save all the branches

$ git checkout mybranch   # Makes "mybranch" the active branch

Merge from branch back to master branch
$ git checkout master            # Makes "master" the active branch 
$ git merge --no-ff  mybranch            # Merges the commits from "mybranch" into "master"  
--no-ff flag causes the merge to always create a new commit object, even if the merge could be performed with a fast-forward.
$ git branch -d mybranch        # Deletes the "mybranch" branch

Checkout remote branch feat-entityDictionary   
$ git checkout -b feat-entityDictionary  origin/feat-entityDictionary

Branch from a previous commit
git branch branchname <sha1-of-commit>    



clone a repository and its branches

$ git clone https://mshahriarinia:yourpassword@github.com/mshahriarinia/gatordsr.git
clone all remote branches with Git?

First, clone a remote git repository and cd into it:

$ git clone git://example.com/myproject
$ cd myproject

Next, look at the local branches in your repository:

$ git branch
* master

But there are other branches hiding in your repository! You can see these using the -a flag:

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental

If you just want to take a quick peek at an upstream branch, you can check it out directly:

$ git checkout origin/experimental

But if you want to work on that branch, you'll need to create a local tracking branch:

$ git checkout -b experimental origin/experimental

Now, if you look at your local branches, this is what you'll see:

$ git branch
* experimental
  master

You can actually track more than one remote repository using git remote.

$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental
  remotes/win32/master
  remotes/win32/new-widgets

At this point, things are getting pretty crazy, so run gitk to see what's going on:

$ gitk --all &




Tag

$ git tag -a tagTitle -m "Description."      # Use tags: http://www.rockstarprogrammer.org/post/2008/oct/16/git-tag-does-wrong-thing-default/
$ git push --tags
You can download the whole snapshot of the project regarding a specific tag here: https://github.com/mshahriarinia/gatordsr/tags



Undo any unsaved changes
$ git reset --hard # removes staged and working directory changes
$ git clean -f -d # remove untracked files


Setup kdiff3 as mergetool in Mac
download kdiff3

git config --global --add merge.tool kdiff3
git config --global --add mergetool.kdiff3.path "/Applications/kdiff3.app/Contents/MacOS/kdiff3"
git config --global --add mergetool.kdiff3.trustExitCode false

git config --global --add diff.guitool kdiff3
git config --global --add difftool.kdiff3.path "/Applications/kdiff3.app/Contents/MacOS/kdiff3"
git config --global --add difftool.kdiff3.trustExitCode false








To pull out a single file out of an old commit:
$ git log            # determine commit sha-1
$ git ls-tree SHA-1_OF_THE_COMMIT
$ git cat-file -p SHA-1_OF_THE_SPECIFIC_FILE

$ git log -p --color

$ git branch -av       #   to list all branches

https://help.github.com/articles/remove-sensitive-data

git push -f origin master 
git fetch upstream; git checkout master; git rebase upstream/master 
git rebase --continue; git rebase --skip; git mergetool


Link to file or its line numbers: you only need 4 characters of the commit SHA in the URL.. it figures it out. i usually truncate to 7ish characters.

Nice: github.com/jquery/jquery/blob/27291ff/src/css.js#L171-185

clone a repository

$ git clone https://mshahriarinia:yourpassword@github.com/mshahriarinia/gatordsr.git
clone all remote branches with Git?

First, clone a remote git repository and cd into it:

$ git clone git://example.com/myproject
$ cd myproject

Next, look at the local branches in your repository:

$ git branch
* master

But there are other branches hiding in your repository! You can see these using the -a flag:

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental

If you just want to take a quick peek at an upstream branch, you can check it out directly:

$ git checkout origin/experimental

But if you want to work on that branch, you'll need to create a local tracking branch:

$ git checkout -b experimental origin/experimental

Now, if you look at your local branches, this is what you'll see:

$ git branch
* experimental
  master

You can actually track more than one remote repository using git remote.

$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental
  remotes/win32/master
  remotes/win32/new-widgets

At this point, things are getting pretty crazy, so run gitk to see what's going on:

$ gitk --all &


gitk     shows the commits tree from different branches

$ git diff --name-only              # Get list of all modified files:
$ git pull        #get latest files and updates
$ git remote -v                # Address of the repository

Setup git

$ git config --global user.name "mshahriarinia"
$ git config --global user.email "info@mshahriarinia.com"# Sets the default email for git to use when you commit
$ git config --global credential.helper cache                                     # Set git to use the credential memory cach
$ git config --global credential.helper 'cache --timeout=3600'             # Set the cache to timeout after 1 hour (setting is in seconds)

$ git init                         #make a repository

Tags

$ git tag -a tagTitle -m "Description."      # Use tags: http://www.rockstarprogrammer.org/post/2008/oct/16/git-tag-does-wrong-thing-default/
$ git push --tags
You can download the whole snapshot of the project regarding a specific tag here: https://github.com/mshahriarinia/gatordsr/tags



Save Changes:

$ git add changed_file_path
OR    $ git add -A                           # Git add all files modified, deleted, and untracked
$ git commit -m "commit message put here"

OR $ git commit -am "changed greeting"             //commit filesto the local copy of the repository
-a all files     -m  message

$ git push


Undo to a specific version

$ git log                         # see the git log. check which commit is one prior the merge. Then you can reset it using:
$ git reset --hard commit_SHA_you_picked_from_log        # removes staged and working directory changes

You undone a commit but you want it back?  restoring lost commits
$ git fsck --lost-found    or     $ git reflog       will show you that it still is there
just do a        $ git merge 7c61179       and you're set!


Undo any unsaved changes
$ git reset --hard # removes staged and working directory changes
$ git clean -f -d # remove untracked files









How to update GitHub forked repository?

In your local clone of your forked repository, you can add the original GitHub repository as a "remote". ("Remotes" are like nicknames for the URLs of repositories - origin is one, for example.) Then you can fetch all the branches from that upstream repository, and rebase your work to continue working on the upstream version. In terms of commands that might look like:
$ git remote add upstream https://github.com/cegme/gatordsr.git          # Add the remote, call it "upstream":
$ git fetch upstream                    # Fetch all the branches of that remote into remote-tracking branches, such as upstream/master:
         If you see the following, just do: $ git remote rm upstream         then add the upstream as above again.
      fatal: remote error: 
          Repository not found.
$ git checkout master                    # Make sure that you're on your master branch:
$ git rebase upstream/master # Rewrite your master branch (any commits of yours that aren't already in upstream/master are replayed on top of that other branch
If you've rebased your branch onto upstream/master you may need to force the push in order to push it to your own forked repository on GitHub. You'd do that with:
$ git push -f origin master               # You only need to use the -f the first time after you've rebased.
OR $ git merge --no-ff upstream/master              # If you don't want to rewrite the history of your master branch, (for example because other people may have cloned it)

$ git fetch upstream; git checkout master; git rebase upstream/master 
[$git pull does a $git fetch followed by a $git merge.] 
You can do a git fetch at any time to update your local copy of a remote branch. This operation never changes any of your own branches and is safe to do without changing your working copy.


Open Source Contribution

username mshahriarinia is going to contribite to the repository  https://github.com/cegme/gatordsr

1. Fork the project on github (making a repository of your own that you can push changes to!)
2. Download the source code     
$ git clone git clone https://github.com/mshahriarinia/gatordsr.git
To eclipsify the scala project that uses sbt: $ sbt eclipse
3. Set up the remotes
$ cd gatordsr
$ git remote add upstream https://github.com/cegme/gatordsr.git             
  # Assigns the original repository to a remote called "upstream" . add a remote for the main repo that you forked
$ git fetch upstream        # Pulls in changes not present in your local repository, without modifying your files
4. Make a commit  (to push your commits directly to the upstream) otherwise to di it locally just git push then resolve any changes or conflicts as shown below then make a pull request. Anybody with a push privilege to the upstream can accept pull request.
then 
$ git push origin master

5. Finally do a Pull request to let the project maintainer integrate your commits. Change the code locally package it up as a "patch/diff" file and send that file onto the project team for integration at a later time. You'll want to just use the website for spelling changes and easy stuff. In Git you'll push your code to a central place and send a "pull request" that says "hey, the code is over here, pull it in, m'kay?"

Use Branches

Don't commit your changes to your local master branch. As the project evolves in it's upstream location(repository) (the actual owner's URL), your mster's branch will stay lagged and you will have difficulty checking what's going on with the project up-to-date. 
So besides keeping a master un-touched just for checking new features purposes, make a branch push all your commits there then make a pull request from that branch to the project maintainer. Here!

With your contribution done, don’t merge it back into master. master is your way of receiving changes from the original (upstream) repo.
First push your branch to Github, so you can share it with others.
$ git push origin fix_for_this_or_that

Branch

Branching allows you to build new features or test out ideas without putting your main project at risk. In git, branch is a sort of bookmark that references the last commit made in the branch. This makes branches very small and easy to work with. Branches are pretty easy to work with and will save you a lot of headaches, especially when working with multiple people. To create a branch and begin working in it, run these commands:
$ git branch mybranch  master          # Creates a new branch called "mybranch" from the latest commit of master.
$ git checkout mybranch  # Makes "mybranch" the active branch
OR $ git checkout -b mybranch master  # Creates a new branch called "mybranch" from the latest commit of master and makes it the active branch

$ git push --all    #save all the branches

To switch between branches
$ git checkout master       # Makes "master" the active branch 
$ git checkout mybranch   # Makes "mybranch" the active branch

Once you're finished working on your branch and are ready to combine it back into the master branch
$ git checkout master            # Makes "master" the active branch 
$ git merge --no-ff  mybranch            # Merges the commits from "mybranch" into "master"  
--no-ff flag causes the merge to always create a new commit object, even if the merge could be performed with a fast-forward.
$ git branch -d mybranch        # Deletes the "mybranch" branch

Good to know: When you switch between branches the files that you work on (the "working copy") are updated to reflect the changes in the new branch. If you have changes you have not committed, git will ensure you do not lose them. Git is also very careful during merges and pulls to ensure you don't lose any changes. When in doubt, commit early and commit often.

Checkout remote branch feat-entityDictionary   
$ git checkout -b feat-entityDictionary  origin/feat-entityDictionary

Push new branch to server and others use it:
$ git checkout -b mynewfeature
... edit files, add and commit ...
$ git push -u origin mynewfeature

To use
$ git fetch origin
$ git checkout --track origin/
mynewfeature


Merge

if you face 
CONFLICT (content): Merge conflict in code/src/main/scala/edu/ufl/cise/Pipeline.scala
Failed to merge in the changes.
try 
$ git rebase --continue
if not solved 
$ git mergetool
$ git merge --no-ff new_branch

git merge --no-ff upstream/master  # merge back with the repository you got the code from


Still have work on them?

If you aren’t sure which branches still have unique work on them – so you know which you need to merge and which ones can be removed, there are two switches to git branch that help:
$ git branch --merged            # Shows branches that are all merged in to your current branch
$ git branch --no-merged       $ # Shows branches that are not merged in to your current branch






Rebase
The difference between merge and rebase is that merge tries to resolve the changes and create a new commit that blends them. Rebase tries to take your changes since you last diverged from the other branch and replay them from the HEAD of the other branch. However, don’t rebase after you’ve pushed a branch to a remote server – this can cause confusion/problems.


once your work on the forked project is done, then is the time to get everything back to the upstream.
$ git fetch upstream; git checkout master; git rebase upstream/master 
if everything is ok this will go through hands-down. if there are merge conflicts you will be prompted, then
$ git rebase --continue; git rebase --skip; git mergetool 
also every once in a while execute the following command to get rid of the .orig files that would appear after a merge
find . -name "*.orig" -print0 | xargs -0 rm

note: You can use kdiff3 as your merge tool.








e favorite

Is it possible to create a commit message with link to other repository commit?

Eg. something like [Username\Repo\commitHash]

Usage example: if some bc break would happen in some framework's commit, I'd like to link to this very commit in my own commits updating my code which uses this framework.

share|improve this question
asked Sep 30 '10 at 15:38
Mikulas Dite
2,41611445


You accepted the wrong answer bud... – Robin Winslow Sep 10 '12 at 12:47
1 
@RobinWinslow I'm pretty sure it was accurate back then. Anyway, you are right it would be appropriate to update it. Done. – Mikulas Dite Sep 10 '12 at 15:36

2 Answers

up vote 20 down vote accepted

I was searching for something like this myself. Found it on:

http://github.github.com/github-flavored-markdown/

<<username>>/<<repo>>@<<commitHash>>

Without the angle brackets of course :)



Cross-Repository Issue References

Sure, GitHub's all about social coding, but you can have social repositories, too. You can reference issues between repositories by mentioning user/repository#number in an issue. Once we see something like that — say, github/enterprise#59 — we'll make sure to update issue #59 in github's enterprise repository and let you know where it was referenced from. This is a part of GitHub Flavored Markdown, which also has a few tricks of its own.


https://github.com/blog/967-github-secrets

Error

$ git push

error: failed to push some refs to 'https://github.com/mshahriarinia/gatordsr.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you did not intend to push that branch, you may want to
hint: specify branches to push or set the 'push.default' configuration
hint: variable to 'current' or 'upstream' to push only the current branch.

$ git config --global push.default current
$ git push


















Subpages (1): MarkDown
Comments