Friday, April 6, 2018

GIT




























Date Dec/15/2016

https://support.beanstalkapp.com/article/834-choosing-your-git-client

For Mac

For Windows



Distributed Version Control System: GIT 

GIT is called distributed version control system as it maintains local & remote repository






















  1. Working directory :Where you are working which git can track
  2. Staging area/index :Git maintains a .git folder to store git specific files.It maintains index file with .git folder
  3. GIT Repository: Local repository
  4. GIT Remote Repository



  • Github,Bitbucket,Gitlab




  • There are three kind of branches 

    Local branch
          -master(on local system)
    Remote Tracking branch
          -origin/master(on local system)
    Remote branch
         -master

    git fetch 

    updates remote -tracking branch from remote branch


















    Create branch from the master













    Important Git Commands 


    CommandsDescription
    git init
    Create git repository on locale machine.
    git Status
    View list of files modified.
    git clone username@host:/path/to/repository

    Checkout exiting git repository
    git checkout
    Switch to existing branch
    git checkout -b
    Create new branch and switch to that branch
    git add
    Add single file to staging
    git add *
    Add all file to staging
    git commit -m "commit message "
    Commit files in staging // this is committed to local repository and not to remote
    git push
    Publish changes to remote repository
    git pull
    Pull changes from remote branch to your branch (this will fetch and merge changes)
    git branch 
    List all branches on local respository and current active branch name as highlited
    git log
    git log -2
    View commit history
    git merge
    Merge branch with your local branch
    git diff
    View changes done before pushing it to remote.
     git remote show
    eg git remote show origin
    View git remote url or remote branch details. 
    git checkout -b  Create branch from Tag in Github
    git cherry-pick
    eg git cherry-pick ...31ff5e.....
     Get Cherry picking commit
    if you have to cherry pick commit from one branch to another in Git repository with Commit id.
    git branch newBranchName  
    Create new Git branch from Git commit
    Create new branch from git commit withouth checkingout new branch
    git checkout -b newBranchName
    Create new branch from git commit with checkingout new branch

    A Moving branches/tags from one git repository to another
    1. Clone existing git repo (source repository)
    git clone --mirror
    eg
    git clone --mirror git@github.com:source/source.git
    2.
    got to
    cd
    3. Add remote repository ( destination repository )
    git remote add new-origin
    eg
    git clone --mirror git@github.com:destination/destination.git
    4. Move all changes to destination repository
    git push new-origin --mirror

    B If you only want to move some branches

    1. git clone
    2. checkout all branches to be moved to new git.
    git checkout

    3. verify branches to be moved using output of git branch command
    git branch 

    4. add destination repository 
    git remote add new-origin git@github.com:destination/destination.git

    5. move all local branches to destination repository
    git push --all new-origin 

    6. move all tags to destination repository
    git push --tags new-origin

    C Move branch to new git repository and changing remote
    after doing steps mentioned above to move branch from one git to another , use following command to set new
    origin in git.
    git remote rm origin
    git remote rename new-origin origin


    Note :
    If you have already moved some branches and want to move one more branch from source git.
    Running following command might override destination branche or give error.
    It is always good to move all branches and tags to new repository and start using new repository.

    Moving branches/tags from one git repository to another

    You have branch having latest changes “latestBranch” and if you want to override master with latestBranch changes.
    Use following commands
    1. Checkout latestBranch.
    git checkout latestBranch
    2. Merge latestBranch with master with strategy as ours. Current branch changes will be used in case of conflicts.
    git merge -s ours master
    3. Checkout master branch.
    git checkout master
    4. Merge master with latestBranch
    git merge latestBranch
    5. Push master with latest code.
    git push (git push origin master)
    you can also use git force push “git push -f origin master”. Please check branch history before running git push
    Replace/override Master branch with your latest branch in git

    https://github.com/DharmendraRathor/gitCommands
    https://dzone.com/articles/git-strategies-for-software-development

    _______________________________________________________________

    GIT Extensions

    1)Generate SSH keys:

    Tools -> Putty -> Generate or import key


    2)Cloning repository:
    start -> clone repository -> paste link 
    select destination 
    ______________________________________________________________________________

    Tortoise GIT



    1) Tortoise GIT: Cloning GIT repository  :

    2) Tortoise GIT: add,stage,push repository  :

    3) Tortoise GIT: fetch,merge or pull repository:



    8) Tortoise GIT: Stash(save local changes) repository:
    stash - save[give some name]
    switch the branch [do your work ]
    switch back

     [stash pop| applies changes to the working directory
      stash list| shows the list of saved changes   ]
    stash apply




















    9) Tortoise GIT: reset with hard option repository:

    Right click file

    10) Tortoise GIT: clean repository:
    git clean:

    Reset with hard option:


    ______________________________________________________________________________

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

    C:\AccelaLocalSetup\AA_Prod>git branch

    * AA_10.0.0_DEVINT

    C:\AccelaLocalSetup\AA_Prod>git checkout AA_10.0.0_DEVINT

    M       biz/.project
    M       biz/modules/aamain/java/com/accela/aa/aamain/cap/CapTypeDAOOracle.java


    C:\AccelaLocalSetup\AA_Prod>git branch Branch_AA_10.0_TAKSHILA


    C:\AccelaLocalSetup\AA_Prod>git branch Branch_AA_10.0_TAKSHILA

    fatal: A branch named 'Branch_AA_10.0_TAKSHILA' already exists.

    C:\AccelaLocalSetup\AA_Prod>git checkout Branch_AA_10.0_TAKSHILA

    M       biz/.project
    M      biz/modules/aamain/java/com/accela/aa/aamain/cap/CapTypeDAOOracle.java



    C:\AccelaLocalSetup\AA_Prod>git branch

      AA_10.0.0_DEVINT
    * Branch_AA_10.0_TAKSHILA

    C:\AccelaLocalSetup\AA_Prod>git branch

      AA_10.0.0_DEVINT
    * Branch_AA_10.0_TAKSHILA

    C:\AccelaLocalSetup\AA_Prod>git branch

      AA_10.0.0_DEVINT
    * Branch_AA_10.0_TAKSHILA

    C:\AccelaLocalSetup\AA_Prod>

    ____________________________________________________________________________


    1.1
     To initialize a Git repository here, type the following command:
    git init


    1.2 Checking the Status

     let's type the git status command to see what the current state of our project is:
    git status


    1.4 Adding Changes

    To tell Git to start tracking changes made to octocat.txt, we first need to add it to the staging area by using git add.
    git add octocat.txt

    1.6 Committing

    N
    To store our staged changes we run the commit command with a message describing what we've changed. Let's do that now by typing:
    git commit -m "Add cute octocat story"

    1.7 Adding All Changes

    Great! 
    I put some in a directory named "octofamily" and some others ended up in the root of our "octobox" directory. Luckily, we can add all the new files using a wildcard with git add. Don't forget the quotes!
    git add '*.txt'


    1.8 Committing All Changes

    Okay, you've added all the text files to the staging area. Feel free to run git status to see what you're about to commit.
    If it looks good, go ahead and run:
    git commit -m 'Add all the octocat txt files'

    $ git commit -m 'Add all the octocat txt files'
     

    [master 3852b4d] Add all the octocat txt files
    4 files changed, 4 insertions(+)
    create mode 100644 blue_octocat.txt
    create mode 100644 octofamily/baby_octocat.txt
    create mode 100644 octofamily/momma_octocat.txt
    create mode 100644 red_octocat.txt


    1.9 History

    So we've made a few commits. Now let's browse them to see what we changed.
    git log

    1.10 Remote Repositories

    Great job! We've gone ahead and created a new empty GitHub repository for you to use with Try Git at https://github.com/try-git/try_git.git. To push our local repo to the GitHub server we'll need to add a remote repository.
    This command takes a remote name and a repository URL, which in your case is https://github.com/try-git/try_git.git.
    Go ahead and run git remote add with the options below:
    git remote add origin https://github.com/try-git/try_git.git

    git remote add origin https://github.com/try-git/try_git.git

    1.11 Pushing Remotely

    The push command tells Git where to put our commits when we're ready, and now we're ready. So let's push our local changes to our origin repo (on GitHub).
    The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do. Go ahead and push it
    git push -u origin master
    git push -u origin master

    1.12 Pulling Remotely

    Let's pretend some time has passed. We've invited other people to our GitHub project who have pulled your changes, made their own commits, and pushed them.
    We can check for changes on our GitHub repository and pull down any new changes by running:
    git pull origin master


    1.13 Differences

    Uh oh, looks like there have been some additions and changes to the octocat family. Let's take a look at what is different from our last commit by using the git diff command.
    In this case we want the diff of our most recent commit, which we can refer to using the HEAD pointer.
    git diff HEAD

    1.14 Staged Differences

    Another great use for diff is looking at changes within files that have already been staged. Remember, staged files are files we have told git that are ready to be committed.
    Let's use git add to stage octofamily/octodog.txt, which I just added to the family for you.
    git add octofamily/octodog.txt




    1.15 Staged Differences (cont'd)

    Good, now go ahead and run git diff with the --staged option to see the changes you just staged. You should see that octodog.txtwas created.
    git diff --staged

    1.16 Resetting the Stage

    So now that octodog is part of the family, octocat is all depressed. Since we love octocat more than octodog, we'll turn his frown around by removing octodog.txt.
    You can unstage files by using the git reset command. Go ahead and remove octofamily/octodog.txt.
    git reset octofamily/octodog.txt

    1.17 Undo

    git reset did a great job of unstaging octodog.txt, but you'll notice that he's still there. He's just not staged anymore. It would be great if we could go back to how things were before octodog came around and ruined the party.
    Files can be changed back to how they were at the last commit by using the command: git checkout -- . Go ahead and get rid of all the changes since the last commit for octocat.txt
    git checkout -- octocat.txt

    1.18 Branching Out

    When developers are working on a feature or bug they'll often create a copy (aka. branch) of their code they can make separate commits to. Then when they're done they can merge this branch back into their main master branch.
    We want to remove all these pesky octocats, so let's create a branch called clean_up, where we'll do all the work:
    git branch clean_up

    1.19 Switching Branches

    Great! Now if you type git branch you'll see two local branches: a main branch named master and your new branch named clean_up.
    You can switch branches using the git checkout  command. Try it now to switch to the clean_up branch:
    git checkout clean_up

    1.20 Removing All The Things

    Ok, so you're in the clean_up branch. You can finally remove all those pesky octocats by using the git rm command which will not only remove the actual files from disk, but will also stage the removal of the files for us.
    You're going to want to use a wildcard again to get all the octocats in one sweep, go ahead and run:
    git rm '*.txt'


    1.21 Commiting Branch Changes

    Now that you've removed all the cats you'll need to commit your changes.
    Feel free to run git status to check the changes you're about to commit.
    git commit -m "Remove all the cats"

    1.22 Switching Back to master

    Great, you're almost finished with the cat... er the bug fix, you just need to switch back to the master branch so you can copy (or merge) your changes from the clean_up branch back into the master branch.
    Go ahead and checkout the master branch:
    git checkout master

    1.23 Preparing to Merge

    Alrighty, the moment has come when you have to merge your changes from the clean_up branch into the master branch. Take a deep breath, it's not that scary.
    We're already on the master branch, so we just need to tell Git to merge the clean_up branch into it:
    git merge clean_up

    1.24 Keeping Things Clean

    Congratulations! You just accomplished your first successful bugfix and merge. All that's left to do is clean up after yourself. Since you're done with the clean_up branch you don't need it anymore.
    You can use git branch -d  to delete a branch. Go ahead and delete the clean_up branch now:
    git branch -d clean_up

    1.24 Keeping Things Clean

    You can use git branch -d  to delete a branch. Go ahead and delete the clean_up branch now:
    git branch -d clean_up




    1.25 The Final Push

     To push everything you've been working on to your remote repository, and you're done!
    git push



    References:

    https://dzone.com/articles/git-strategies-for-software-development

    No comments:

    Post a Comment