Git Solutions

Git - It's a source code management system, it having lot more advantages.
With this system we can manage the source code like a tree(Source code can be managed with the version releases).

If you are very new to Git:
1. Create a folder to hold your repository code and make sure the folder name should not contain empty spaces.
2. Open the folder and open the git bash with right click->select git bash
3. Give the command - git init (This will create your local git reference and the .git folder will be created, it will be in hidden mode)
To Create new branch and push that branch to remote git:
1. Open your root folder and open git bash
2. give the command -  git checkout -b <branch-name>
Ex: git checkout -b develop
3. put your initial code into the root folder and use the command git push -u <remote-name> <branch-name>

Ex: 'git push origin develop <remote-name>' is typically origin, the name which git gives to the remote you cloned from
For Complete reference please go through this link https://www.atlassian.com/git/tutorials/setting-up-
a-repository/git-init

WARNING:UNPROTECTED PRIVATE KEY FILE while pulling the code from remote repo only in MAC machine
To give the permission to the folder - chmod 700 ~/.ssh
To give the permission to the file - chmod 700 ~/.id_rsa

To commit your changes to remote git:
1. Open the git bash and give the command - 'git pull origin <branchname>'
(Ex: git pull origin develop)
2. Merge your changes into the branch files or add your new files in to the branch
3. git status - This is to check your modified, newly added and deleted files are staged or not(This will list the modified and deleted files in red color)
4. 'git diff filepath/filename.java' - here you can see your applied changes line by line
5. Check whether the username and email is correct for the commit
    git config user.name
    git config user.email

  • To change the user name, use the command - git config user.name "username" or git config --global user.name "username"
  • To change the user email, use the command - git config user.email "xyz@domain.com" or git config --global user.email "xyz@domain.com"

6. git add . (or) git add * (or) git add --all - this is to stage your changes locally
'git add .' - is used to add the modified files
 'git add -A' or 'git add --all' - is used to add all the changes which includes newly added files, deleted files and modified files.
7. git status - This is to check whether your changes staged or not(It should be in green color if your changes staged)
8.  There are two cases we should monitor while committing
If we have issue tracker and having specific issue related with that commit then the following command will be helpful git commit -m "[Issue123] Issue Message"
If we do not have issue tracker then the following command is enough to commit git commit -m "Issue Message"
9. git log - It will show your last commits and you can check whether your changes committed properly or not

10. git push origin branchname - This is the last step of your committing process

To unstage your local changes:
1. Give the command - git stash
'git stash' - This command will revert all your local changes if you not committed those.
2. Or you can give give the command - git reset
'git reset' - This command will revert all your local changes if you not committed those.
 Difference between 'git stash' and 'git reset' is:
With 'git stash' command you can get back the removed changes by giving the command 'git stash pop' but with 'git reset' command you can not get back your changes.
To revert local changes before commit has done:
 There will be two cases,

1. If you not staged your changes locally(If you not given git add . command) then need to be use the following command, git checkout filename.ext
Ex: git checkout android/project/gitcode/dashboard.java
2. If you staged your local changes(Given git add . command) and needs to be removed a specific file in that, you need to give the following two commands
 - git reset HEAD filename.ext 
(Ex: git reset HEAD android/project/gitcode/dashboard.java)
- git checkout filename.ext
(Ex: git checkout android/project/gitcode/dashboard.java)
 To revert your local commit:
1. There are two cases, one is to remove your entire commit including changes and to remove only the commit but not code change of that commit
  • Open the git bash and give the command - 'git reset --hard HEAD~'
  • Open the git bash and give the command - 'git reset --soft HEAD~'
To get the all information from your branch or Repository:
1. Open the git bash from your branch and give the command - 'git fetch --all'
How to handle multiple branches of a repository in a single folder:
1. Clone your repository code and checkout to the another branch which you want to keep in the same folder.
git pull origin master
git checkout develop
git pull origin develop
Now both of the branches (master and develop) is in a single folder, whenever you needs to switch the branch you just needs to give the command 'git checkout <branch name>' and pull your updated code with 'git pull origin <branch name>'
git checkout develop
git pull origin develop
 To delete a branch from a repository:
Give the below command to delete the repository, git branch -d <branch name>
git checkout master (You can not able to delete your branch which is you currently On so, we should checkout master branch and then delete the branch whatever you need)
git branch -d testdevelop (This is to delete your branch locally)
git push origin testdevelop (Until you push your changes to remote other users can still access the branch, so you should push your changes to the remote)
Pulled into wrong repository???
If you are in master branch and accidentally pulled the develop branch then you will be facing issues of merged code,,, Don't worry, there is a command to help you out,
git reset --hard origin/master
It will revert you back to the updated branch
You are unable to revert the untracked files with all possible commands???
git clean -fdx
Use the above command to unstage all the local untracked files


Comments

Popular posts from this blog

Creating a .ssh key in window and Mac machines

Apigility Project Setup