Friday, January 10, 2014

Git tips & trics

Skipping the staging Area

use $ git commit -a -m 'added some ASDoc comment' to stage every already tracked file automatically before doing commit and make commit with the comment and with skip git add part.


Stage all files at once

To stage all files from current folder at once use $ git add .
Using "
." it's the shortnes way to add the  current folder and all subfolders of it.
But don't forget to do
git status before it, to prevent adding of some unexpected files!


How to quit from Vim without the window closing :)

When u called $ git commit without -t , the Vim will be opened by default to add comment message to your commit.
So, enter the comment message, press Esc tnen :wq to write comment, quit from Vim and finish the commit or Esc then :q to just quit.


How to output content of the file into console.

Use $ cat some_file , to fully output file content into console.


How to copy/paste inside Git Bash

To copy from the window, RightMouse click on the console window, choose Edit->Mark, then drag abox on the text, then press Enter.
U can paste in to the window via the menu, but using
Shift+Insert is faster


Advanced log output

$ git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short is good enough to log out each commit in the oneline with nessesary description.Git book ref
Colorfull variant: 
$ git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold cyan)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all 
U can easily alias this advanced log output to some custom command e.g. hist via adding it to git aliases
$git config --global alias.hist' log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'

Then u can just call $ git hist instead of long git log...


Revert your changes in the right way

Revert or modify your last commit

Use $ git commit --amend.
If u had called
git commit --amend at once after the previous commit without any staged changes, then only your comment of the last commit will be changed on the new one.
If u want to rewrite your last commit with the new one stage your modified files at first:

$ git commit -m 'initial commit'
# Do this:
$ git add forgotten_config_file
$ git commit --amend -m 'initial commit with config'
As result u will see only one commit in the git log with your last changes & comment "initial commit with config".

Unstage files

Use $ git reset HEAD <file> to unstage some file, folder or all in current folder ($ git reset HEAD .).


Revert your uncommited changes

Use $ git checkout -- <file> to discard changes in working directory:

# Do this:
$ git checkout src/com/company/utils/some_modefied_util_file

# Or If u want to discard all changes in current directory:
$ git checkout .

Note: Staged files won't to be discarded!!! Use $ git reset HEAD . at first, if u want to discard all changes.


Revert your uncommited changes to any previous version

Receive hashes of the reguired commit versions (@see Advance log output):

# Equal to $ git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short:
$ git hist
* 7454c5f 2014-01-10 | Commit some forgotten expected changes 
* 9972eeb 2014-01-09 | New entity were added and output text
* 957057d 2014-01-09 | .gitignore file added 
* 46c985a 2014-01-09 | exclamation mark(!) added to the end 
* 2f049f1 2014-01-09 | initial project version 

# Do this to revert your changes to 'initial project version' locally:
$ git checkout 2f049f1 
To return to your last files version use $ git checkout masterTo revert all your changes to last version use $ git checkout .


Revert your directory back to any previous version by tag instead of hash

Make checkout to needed version.
Create the tag with
'v1' name call $ git tag v1
As result your HEAD commit will be marked with 'v1'
So, since now u will be able to revert your directory to this version by tag name via using
$ git checkout v1
U can also can checkout the previous version of this tag via using v1^ or v1~1 tag name.
Use
$ git tag to see all available tag names.Use $ git tag -d 'v1'if u won't to see that tag in your commit tags anymore.


Revert your commited changes

Use $ git revert HEAD to revert your commited changes and your directory files.

# Equal to $ git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short:
$ git hist
* 8ae9f33 2014-01-13 | Oops, unwanted text was commited 
* 7454c5f 2014-01-10 | Commit some forgotten expected changes (v1)
#...
$ git revert HEAD
$ git hist
* 26460a2 2014-01-13 | Revert "Oops, unwanted text was commited" (HEAD, master) 
* 8ae9f33 2014-01-13 | Oops, unwanted text was commited 
* 7454c5f 2014-01-10 | Commit some forgotten expected changes (v1) 
#...
Note: u can use $ git revert HEAD --no-edit to revert changes at once, without the editing generated 'revert commit...' message via editor.


Revert your commited changes with reseting the history

Use $ git reset to revert your commited changes and remove the commits history of them:

# Equal to $ git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short:
$ git hist
* 4c2715f 2014-01-13 | Oops Again, Add some other Unwanted text (HEAD, master)
* 26460a2 2014-01-13 | Revert "Oops, unwanted text was commited" 
* 8ae9f33 2014-01-13 | Oops, unwanted text was commited  
* 7454c5f 2014-01-10 | Commit some forgotten expected changes 
#...
$ git reset --hard v1
$ git hist
* 7454c5f 2014-01-10 | Commit some forgotten expected changes (HEAD, v1, master)
#...
Note: the --hard param means that working folder should be reverted accordingly the new HEAD.
Warning: The reset can confuse the other developers if u do it on remote repositories.

However u can use
$git hist --all to see the history log with the all (including removed) commits.
Note: If there no any tag references on the removed commits, they will be available only till garbage collector launching.



Remote repositories interactions

Push up an existing repository

Use $ git remote add <remote-name> <url> to push your existing local repository up to some remote storage:

$cd /path/to/my/repo
$git remote add origin https://someuser@bitbucket.org/someuser/experimental_repo.git
# Pushes up the repo and its refs for the first time:
$git push -u origin --all
# Pushes up any tags 
$git push -u origin --tags 
Use $ git remote -v to see shortened names of directory related remote git repos:

$ git remote -v
origin https://someuser@bitbucket.org/someuser/experimental_repo.git (fetch)
origin https://someuser@bitbucket.org/someuser/experimental_repo.git (push)
Use $ git remote show <remote-name> to see more information about the particular remote.


Remote Branch creation

Use $ git branch <branch-name> to create the local branch.
Use
$ git checkout <branch-name> to switch on new created branch.

# Creates the branch with name 'stick'
$git branch stick
# Switch on 'stick' branch
$git checkout stick
The branch was created from master and it doesn't contain your uncommited changes that were in your directory on the creation moment.
Commit your files to branch, then switch back to master and u will see that there no your last commited changes in master(cause all of them are in branch)

# Add and commit some 'stick_file' 
$git add stick_file
$git commit -m 'Stick file added'
# Switch on 'master' branch
$git checkout master
#'stick_file' has missed from your directory,
#cause here is no such file at all in master, yet.
Use $git push -u <remote-name> <branch-name> to create remote branch

# Switch on your branch
$git checkout stick
# Push it with '-u' upstreamed
$git push -u origin stick

@see Why do i need to track remote branch?


Branch conflicted changes


Assume that u have the same empty 'readme_file' in both your master and your branch with name 'stick'. Make the following steps:
  • add 'Hello Git Master World' text on first line in master and commit all;
  • switch on 'stick' branch;
  • add 'Hello Git Stick branch World' text on first line in 'stick' branch and commit all;
Call $ git log --graph --all --oneline to see result:

$ git hist --all
* a9806ca 2014-01-15 | Add 'Hello Git Master World' in readme_file
| * 4926960 2014-01-15 | Add 'Hello Git Stick branch World' in readme_file
| * 7d17fd0 2014-01-15 | stick_file was added 
|/
* 7454c5f 2014-01-10 | read_me file was added
* 2f049f1 2014-01-09 | initial project version

Megre 'master' into your branch:

#Switch on 'stick' branch
$git checkout stick
#Merge master into your stick branch
$git merge master 
Auto-merging readme_file
CONFLICT (content): Merge conflict in readme_file
Automatic merge failed; fix conflicts and then commit the result.

Resolve conflict in your readme_file via changing text on 'Hello Git Merged World', then stage your changes and commit them:

#Stage changes in 'stick'
$git add .
#Check the status
$git status
# On branch stick
# All conflicts fixed but you are still merg
#   (use "git commit" to conclude merge)
nothing to commit, working directory clean
$ git commit -m 'Stick merged with master, conflict fixed'
$ git hist --all
*   d640dea 2014-01-15 | Stick merged with master, conflict fixed(HEAD, stick)
|\
| * a9806ca 2014-01-15 | Add 'Hello Git Master World' in readme_file(master)
* | 4926960 2014-01-15 | Add 'Hello Git Stick branch World' in readme_file
* | 7d17fd0 2014-01-15 | stick_file was added 
|/
* 7454c5f 2014-01-10 | read_me file was added
As result the text 'Hello Git Merged World' will be in your 'stick' branch (master will not be affected)
Switch on master 

Rename your local & remote branch

Assume that u have a branch 'stick' Then your dev team decides to change their feature branch naming convention. So u have to rename your loacal & remote branch to the 'feature/stick_branch'.

First of all rename your local branch by using 
$ git branch <old-branch-name> <new-branch-name>:


#Rename local 'stick' branch into 'feature/stick_branch'
$git branch -m stick feature/stick_branch

U can't directly rename the remote branch. So u have to remove the remote branch with the old name and recreate it.
First of all get the remote name:


#Get the remote name
$ git remote -v
origin  https://andrewmur@bitbucket.org/andrewmur/experimental_lab.git (fetch)
origin  https://andrewmur@bitbucket.org/andrewmur/experimental_lab.git (push)
#so remote is 'origin'

Then delete the remote branch with the old name and create the new one with the new name:

#Delete the remote branch with the old name 'stick':
git push origin :stick

#Re-create the remote branch with the new name 'feature/stick_branch':
git push origin feature/stick_branch

Note: this is only recommended if you don't have any other users working on this branch, or if you coordinate in advance with them....


Get data from your remote

Use $ git fetch <remote-name> to receive any new work that has beet pushed to that service since you cloned (last fetched/pulled) it. So u'll have references to all branches from that remote and the changes history, but note that fetch doesn't automatically merge that changes with your local changes that u're working on.

Use $ git pull to receive any new work and merge it automatically into your local changes (like svn 'update').So you just saw that  $ git pull is equals to $ git fetch and $ git merge origin/master

Delete your remote & local branch

Use the $ git push <remote-name> :<branch-name> or more explicit form $ git push <remote-name> --delete <branch-name> to delete the remote branch (your loacal branch will not be affected).

#Delete the remote branch with the old name 'stick':
git push origin :stick

#Other way to delete the remote branch with the name 'stick':
git push origin --delete stick

To delete your local branch use $ git branch -d <branch-name>. Note: git will notify u whether the branch changes were not merged into master. Whatever if u want to delete it in any way use capital -D argument:

#Delete the local branch with the name 'stick':
git branch -d stick
error: The branch 'stick' is not fully merged.
If you are sure you want to delete it, run 'git branch -D stick'.

#Force delete in anyway:
git branch -D stick
Deleted branch stick (was 261ee41).

...

1 comment:

  1. Bloom Lab: Git Tips And Trics >>>>> Download Now

    >>>>> Download Full

    Bloom Lab: Git Tips And Trics >>>>> Download LINK

    >>>>> Download Now

    Bloom Lab: Git Tips And Trics >>>>> Download Full

    >>>>> Download LINK wa

    ReplyDelete