Thursday, April 6, 2017

GIT configuration and basic command

Git practical
About: GIT is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

GERRIT: it is purely web based and relies on GIT for storing the source code.

Configure git and repo (on your local machine)
we will setup git on your local machine. This will include user specific configuration settings for git in the .gitconfig file and Gerrit setup.

Configure name and email in ~/.gitconfig file.
1. Start a terminal/shell. To configure your name and email execute these commands and replace with your values.

$ git config --global user.name <"FirstName Lastname">
$ git config --global user.email <username@xyz.com>

2. Run $ git config --global color.ui auto to configure colorization of git output.
3. Run $ git config --global core.editor nano to configure your standard git editor as nano/vim. Choose an editor that you are comfortable with. Nano is a good default choice.
4. Verify in your .gitconfig file that the right credentials have been set.
     $ cat ~/.gitconfig
You should now have a section in your ~/.gitconfig file that looks like this
                  [user]
                             name = Firstname Lastname                    
                             email = username@xyz.com
                        [color]
                               ui = auto
                        [core]
                                editor = nano
 Configure [url] section in ~/.gitconfig file.
 Set the URL
    $ git config --global url git://review.source_code.org/

   SSH key Generation
    In this step you will configure SSH Username and email in Gerrit, create an SSH keypair on your local client machine and add your public key to Gerrit.

1. Start a terminal/shell
2. Type the text below with your email address and press Enter.
$ ssh-keygen -t rsa -C username@xyz.com
3. You will now be requested to enter file in which to save the key: DON’T TYPE ANYTHING. Just press Enter when asked.
4. You will now be requested to enter name and password: DON’T TYPE ANYTHING. Just press Enter when asked.
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
5. Your public/private rsa key pair will be generated and stored in ~/.ssh
6. Go to Gerrit page https://review.source_code.org, Settings – SSH Public Keys
7. In the Profile section add (or verify) your username as <username > and then click “Change Username” button to confirm.
8. Click the  Add Key  button.
9. Open the file id_rsa.pub with a text editor by typing this in a terminal:
        $ vi ~/.ssh/id_rsa.pub
        Copy everything in that file, and paste it into the SSH Keys tab field in Gerrit. Click on Add.
        Your SSH key and SSH Username are now configured in Gerrit. The last step is to add a preferred email.
10. Go to Gerrit page https://review.source_code.org, Settings – Contact information
11.  Register New Email and add your email, if mail id is correct then skip it.
12. If you registered a new email you will receive a confirmation e-mail from Gerrit. There’s a link in the mail, copy the hyperlink into the browser for verification.


*** we will focus on how to reset your working directory and staging area. This is useful if something gets messed up and you need to start over again.

                      Figure 1

      Unstage particular files from the “Staging area”

         Create a new file and stage for commit. Also unstage it from the index.

1. Change active directory to ~/aosp/system/bt
2. Open your favorite editor and type something into it. Save the file as backup.txt
3. View repository status by running git status. Change file reside in “working directory” according to above figure1.
                                                                                                           
 4. Now add your file, git add backup.txt
 .                                       
5. Run git status again. Change file reside in “Staging area” according to above figure 1.
                                                                                                            
6. You want to unstage the file from the “staging area”.
You can do this by using git reset -- file. Now reset the file,
$ git reset -- backup.txt
Backup.txt file will get removed, if it is newly created, otherwise the changes what we have done, it will get reverted.
                                                                                                            

     Unstage all files from the “Staging area”.

1. Change work directory to ~/aosp/system/bt/. Modify 2 or more files and  Run git status, several files and added them to the staging area.
2. Now you want to unstage all files in the “Staging area”, this can be done with git reset. If no file/ files are specified, all files will be unstaged. Run git reset and then check the result by running git status.
                                                                                                            
How to restore all back to normal.
1. Change work directory to ~/aosp/system/bt/. If you run git status in here, you will find that we have modified several files.


Working directory
Staging area
Tracked
tracked-working.txt
tracked-added_to_staging.txt
Untracked
untracked-working.txt
untracked-added_to_staging.txt
                                             Figure 2
2. You want to undo your changes and restore everything to your latest commit (HEAD).  This can be done with git reset –hard(this will move the tracked file to untracked section of files).
                                                                                                          
5. Now undo your other changes to restore all remaining files to HEAD,
$ git reset --hard HEAD
5. Run git status and compare with figure 2.
We will not get any change files, we will get only the original files.

git log –Show commit logs
1. Change active directory to ~/aosp/system/bt.
2. Do a git log within the directory to view the history of the repository.
$ git log
3. List all local branches using git branch. The * in front of master indicates that this is the currently checked out branch. You will also find another branch in the list. Now view the history for the branch feature.
$ git log branch_name
4. It is possible to view history for specified files or directories.
$ git log -- file

git diff -Show changes between commits, commit and working tree etc.
1. Change active directory to ~/aosp/system/bt/ if not already set.

2. Choose one of the files. Open it in an editor and make a change to it.
3. Use git diff to show the difference between your working directory and the index.
$ git diff


Now we will learn how to amend a commit with faulty author. We will also upload a new patch set to an existing change in Gerrit.
Amend a commit with a wrong author. In this step we have already created a commit ready for upload.

1. Change active directory to ~/aosp/system/bt
2. Make sure that system_bt is checked out. On this branch we have prepared a commit for you to submit for code review in Gerrit.
3. Using repo upload . you will now upload changes to Gerrit.
When asked to confirm the upload, press y and enter.
If it is success then you will get the full gerrit URL
                                                                                                            
4. If you want to change the author of gerrit then we can change by below command.
$ git commit –-amend –-author="Firstname Lastname <username@xyz.com>"
When it is time to enter the commit message, you will find that the previous commit message is now prefilled for you. This is because you in fact are rewriting that commit. Just save and exit, there is no need to change the message.
Use git log to check that author of the commit now username@xyz.com
5. Now reattempt the upload command.
6. Please note down the URL that is given by the upload command. Review your change in Gerrit by surfing to the URL.

Upload a new patch set to an existing change in Gerrit. 

1. Make sure that you have system_bt branch checked out.
2. Open the file Makefile(or any file where you want to change it)  and correct the faulty line.
3. We now want to add this fix into the commit that already contains the original change. Add your file with git add, and then to include the fix in the the previous commit run this:
  $ git commit –-amend ( don’t change the CHANGE ID)

4. Verify that your change is complete, run git log and git diff to verify!
5. You now want to upload your amended commit as a new patch set to your change in Gerrit. This is done using the --replace option.
  $ repo upload 
6. Verify your new patch set in Gerrit.

other git command with one line desciption:

1. git rm test.c  --> it will remove the file from the git reprository
2. git commit --> to add the commit message and make the patch
3. git format-patch -1 --> it will create the patch for the last commit .

BRANCHES: A branch is only a pointer to a commit.
4. git checkout localbranch --> swithch between branch, switch to brnach localbranch
5. git branch mylocalbranch --> for creating local brnach
6. git commit --amend --> to apply the changes to git(if cherry pick being used), we can use this when we want to ammend the
change to the already existing gerrit.



No comments:

Post a Comment

Qualcomm Short Term

  113 is the SL. 1st Target by mid July.

Total Pageviews