Wednesday, May 31, 2017

Git howto

Clone the master

git clone https://git/com.company.api.core.git
git clone https://git/com.company.api.core.git []

Create a new branch

git checkout -b my_hello_world_branch
git add .
git commit -m "B012345 Fixed Hello World"
git push -u origin my_hello_world_branch

Checkout

git checkout master
git checkout 
git checkout 

Merge a fresh master into your branch

git checkout master
git pull --rebase
git checkout some_fix_bug
git merge master
git push
or
git fetch
git pull origin master
git push
Before push you might need to resolve conflicts, then add and commit them

Stash

Save not staged (not git added changes) changes in local stack storage
git stash
Pop them back
git stash pop

Reset

Reset staged files, to get them back to local (NOTHING LOST).
git reset HEAD

If you have uncommitted changes that you want to discard (you'll LOST ALL UNCOMMITTED), use this:
This removes all the local uncommitted changes.
git reset --hard 
which is equivalent to
git reset --hard HEAD 

If you want to remove some offending commits from your local branch (you'll LOST PREVIOUS COMMIT), try rewinding it:
git reset --hard HEAD^ #moves HEAD back by one commit 
or e.g. (you'll LOST PREVIOUS three COMMITS)
git reset --hard HEAD~3 #moves HEAD back by 3 commits 
Use these with caution, as you won't be able to undo these operations. Once you're done cleaning up your local branch, use git pull to get the latest code.
Then 
git push -f origin 
If you have it in ignore, use 
 git clean -xf.
You can do 
 git clean -df 
but that will also remove un-tracked directories. Use -n for a dry-run.

Delete branches

Delete branch local
git branch -D 
Delete branch remote
git push origin --delete 

Copy a commit

Apply any commit done in the some branch to the local current branch
git cherry-pick c1qwlkjfcnc432289a42d34ab4b23803c9

Show list all

git branch
git tag
git branch -r
git remote show origin

Checkout Tags

After the clone, you can list the tags with 
 git tag -l 
and then checkout a specific tag: 
 git checkout tags/

Diff

Show changes between commits (Diff man )
git diff 4223732fhkjsdve1ffce96563367e8fdc^
git diff HEAD^
git diff HEAD~2

Rename a tag

Here is how to rename a tag old to new:
git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags

Show staged diff

You can show changes that have been staged with the --cached flag:
git diff --cached

Show diff latest 10 commit in current folder

git log -10 . | grep commit | sed 's/commit //' | xargs  git diff

Squashing commits with rebase