If you have homebrew installed
$ brew install git
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]
$ cd existing-code-directory
$ git init
$ git add *
$ git commit -m "Initial checkin"
$ git remote add https://github.com/cruft/my-repo.git
$ git clone ssh://[email protected]/your-name-here/your-repo.git
$ git add FileThatChanged.java
$ git commit -m "I implemented blah for flurp"
$ git push origin HEAD
$ git pull origin master
$ git checkout -b branch_name
$ git checkout master
$ git fetch
$ git checkout remote_branch
$ git merge branch_name
$ git branch -d <branch-name>
$ git branch -D <brach-name> # - force delete
$ git push origin :<branch-name>
$ git push origin --delete <branch-name>
$ git tag V1.0
$ git tag -a v1.1 -m "Creating version 1.1" # Creates a tag with a message
$ git tag -a v1.2 9fceb02 -m "Creating tag for 1.2 from commit hash"
$ git tag
$ git tag -l "v1.8.5*" # show tags of a particular pattern
$ git push origin v1.1 # Save tag to remote
$ git push origin --tags # Save all local tags to remote
$ git tag -d v1.1 # Delete local tag
$ git push origin --delete v1.1 # Delete remote tag
$ git checkout v1.1 # Check out tag in in “detached HEAD” state
$ git checkout -b version2 v2.0.0 # Checkout tag to branch
$ git fetch -p <remote-branch> # single branch
$ git fetch -p # Mutiple remote tracking deletes
$ git status
$ git add path/to/my/file/SomeFile.java
$ git reset HEAD path/to/my/file/SomeFile.java
$ git checkout -- path/to/my/file/SomeFile.java
$ git commit -m "I added code"
$ git commit -a -m "Commit Message"
This can be used to change a message, add, and/or remove files
$ git commit --amend -m "Change the commit message"
$ git add NewFile.java
$ git commit --amend
$ git push origin HEAD
$ git pull --rebase
$ git diff
$ git mv /path/to/my/file/SomeFile.java /new/path/SomeFile.java
$ git log
$ git reflog
$ git rm path/to/my/file/SomeFile.java
$ git rm --cached local.file
Find the commit you want to revert
$ git log
You should see an output of the repository history
commit 54533995148bc91424b7ca2493230a09f6cb9acb
Author: Homer Simpson <[email protected]>
Date: Mon Oct 10 14:19:04 2016 -0400
Added auto cool of tap.
commit 7de86787eabc55799085a7ab7e22cf04c83d31ab
Author: Krusty.Clown <[email protected]>
Date: Mon Oct 10 14:54:12 2016 -0400
PUB-1222 - Krusty C, Sideshow B - Added the humor tap squirt feature.
Select the commit hash to revert
$ git revert 54533995148bc91424b7ca2493230a09f6cb9acb
Add your revert message
$ vi ~/.gitconfig
Add entries like below
[alias]
st = status
pr = pull --rebase
pm = pull origin master
co = checkout
I assume you have git command line installed by now.
This should only be done prior to pushing to a remote branch. Caveat Emptor
$ git commit --amend -m "STORY-1234 - Updated commit message"
$ git show --name-only
If you have a commit that you want to undo, there is a simple workaround
$ git reflog
4c20138 HEAD@{0}: checkout: moving from feature/STORY-1234 to master
4c20138 HEAD@{1}: checkout: moving from master to feature/STORY-1234
4c20138 HEAD@{2}: pull origin master: Merge made by the 'recursive' strategy.
3c3dace HEAD@{3}: commit: STORY-1234 - Bad unit of work
$ git reset --soft HEAD~3
$ git add -p
The output will look something like below:
diff --git a/README.md b/README.md
index f00c19d..ef40e35 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
# Q Platform Vagrant Setup
## Prerequisites
-
+
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?
$ git mergetool
$ git log --follow -p -- path/to/file/SomeFile.java
You should resolve the conflicts using your merge tool. After that continue the rebase
$ git rebase --continue
This will cause local changes to be lost, Caveat Emptor
$ git fetch --all
$ git reset --hard origin/master
To see the unstaged file, check the status
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
new_file
nothing added to commit but untracked files present (use "git add" to track)
This shows that new_file is not tracked
$ git checkout -b new_branch
$ git add new_file
$ git commit -m "Added files to new branch"
$ git add .gitignore
$ git commit -m "Added git ignore file"
$ git push origin HEAD
Check out the Documentation for information how to set up the file
$ git clone https://my-git-repo.com/my-app.git -b my-tag-1.0
$ git clone https://my-git-repo.com/my-app.git -b my-branch
$ git clone https://my-git-repo.com/my-app.git --branch my-tag-1.0
$ git clone https://my-git-repo.com/my-app.git --branch my-branch
$ git clean -fd
$ git reset --soft HEAD~
Here are some useful tips and tricks for making working with Git easier.
$ git commit --reedit-message=HEAD --reset-author
$ git remote show origin
$ git commit --amend --no-edit
$ git stash
This will take all changed files in local repository and stash them locally in a stash stack
$ git stash list
This will list all of the stashed changes in your stash stack
$ git stash show -p stash@{1}
This will show all of the changes on that particular stash stack location
$ git stash pop
This will take stashed files and apply them to your working HEAD. This also removes them from the stash stack.
$ git stash apply
This will take all changed files stashed and apply them to your local HEAD. This leaves the stashed changes on the stash stack
$ git stash drop
This will take remove the changes at the top of your stash stack.
$ git stash clear
This clear your stash stack.
$ git branch -m "new_branch_name" # If you are on the branch locally
$ git branch -m old-name new-name # If you are not on the branch to rename
$ git push origin :old_branch # Delete the old branch
$ git push origin HEAD # Push the renamed branch to origin
This is when things go awry and you need some help.
Say you pushed code to the (master)
branch instead of (develop)
and you need to revert everything back to the last merge down to master.
$ git log
This will show you the commit history, like so:
commit 6290518ffd80d8dbb393ac144804f1069040f2e7
Merge: f428d60 e39a178
Author: Mickey Mouse <[email protected]>
Date: Tue May 2 16:02:16 2017 +0000
Merge pull request #183 in PRP/de-prp from bugfix/PRP-515-Integration_bug to release/v1.2.0
* commit 'e39a178c32ff35e41c13437951fbc3a8cfecc9be':
Fixed integration broken because of prp-515
commit e39a178c32ff35e41c13437951fbc3a8cfecc9be
Author: Donald.Duck <[email protected]>
Date: Tue May 2 11:45:52 2017 -0400
Fixed Broken Tests
commit dc71d964818c382b090bc90cb214d0d0b356c743
Merge: c6f68a8 6290518
Author: Mickey Mouse <[email protected]>
Date: Tue May 2 13:15:07 2017 -0400
Merge remote-tracking branch 'origin/release/v1.2.0' into master
Find the last good commit hash
$ git reset dc71d964818c382b090bc90cb214d0d0b356c743 --soft
You can do a --hard and it will remove all of the changes that are between where you are and that commit. A --soft will keep those changes as staged in your local.
$ git push --force HEAD
THIS IS A BIG NO NO, usually. Only do this if you are sure that no one is working off of the remote branch.