Github Tutorial and Basic Commands

GitHub Tutorial
1)Introduction to GitHub
GitHub is a highly used software which is typically used for version control. It is helpful when more than just one person 
is working on a project. Say for example, a software developer team wants to build a website and everyone has to update their
 codes simultaneously while working on the project. In this case, Github helps them to build a centralized repository where 
everyone can upload, edit and manage the code files.

Step 2: Creating a GitHub Repository
  a)Go to the link: https://github.com/ . Fill the sign up form and click on “Sign up for Github”.
  b)Click on “Start a new project”.
c)Enter any repository name and click on “Create Repository”. You can also give a description to your repository (optional).

Step 3: Create Branches and Perform Operations
Branching: Branches help you to work on different versions of a repository at one time. Let’s say you want to add a new feature 
(which is in the development phase), and you are afraid at the same time whether to make changes to your main project or not.
 This is where git branching comes to rescue. Branches allow you to move back and forth between the different states/versions
 of a project. In the above scenario, you can create a new branch and test the new feature without affecting the main branch.
 Once you are done with it, you can merge the changes from new branch to the main branch.

To create a branch in GitHub, follow the below steps:

a)Click on the dropdown “Branch: master”
 b)As soon as you click on the branch, you can find an existing branch or you can create a new one. In my case, I am creating 
a new branch with a name “readme- changes”. Refer to the below screenshot for better understanding.

CreateBranches - how to use GitHub - Edureka


Commit Command:
This operation helps you to save the changes in your file. When you commit a file, you should always provide the message, 
just to keep in the mind the changes done by you. Though this message is not compulsory but it is always recommended so 
that it can differentiate the various versions or commits you have done so far to your repository.


Pull Command

Pull command is the most important command in GitHub. It tell the changes done in the file and request other contributors 
to view it as well as merge it with the master branch. Once the commit is done, anyone can pull the file and can start a
 discussion over it. Once its all done, you can merge the file. Pull command compares the changes which are done in the 
file and if there are any conflicts, you can manually resolve it



Git task Notes Git commands
Tell Git who you are Configure the author name and email address to be used with your commits.

Note that Git strips some characters (for example trailing periods) from user.name.

git config --global user.name "Sam Smith"

git config --global user.email sam@example.com

Create a new local repository  
git init
Check out a repository Create a working copy of a local repository:
git clone /path/to/repository
For a remote server, use:
git clone username@host:/path/to/repository
Add files Add one or more files to staging (index):
git add <filename>

git add *
Commit Commit changes to head (but not yet to the remote repository):
git commit -m "Commit message"
Commit any files you've added with git add, and also commit any files you've changed since then:
git commit -a
Push Send changes to the master branch of your remote repository:
git push origin master
Status List the files you've changed and those you still need to add or commit:
git status
Connect to a remote repository If you haven't connected your local repository to a remote server, add the server to be able to push to it: git remote add origin <server>
List all currently configured remote repositories: git remote -v
Branches Create a new branch and switch to it:
git checkout -b <branchname>
Switch from one branch to another:
git checkout <branchname>
List all the branches in your repo, and also tell you what branch you're currently in:
git branch
Delete the feature branch:
git branch -d <branchname>
Push the branch to your remote repository, so others can use it:
git push origin <branchname>
Push all branches to your remote repository:
git push --all origin
Delete a branch on your remote repository:
git push origin :<branchname>
Update from the remote repository Fetch and merge changes on the remote server to your working directory: git pull
To merge a different branch into your active branch:
git merge <branchname>
View all the merge conflicts:

View the conflicts against the base file:

Preview changes, before merging:

git diff

git diff --base <filename>

git diff <sourcebranch> <targetbranch>
After you have manually resolved any conflicts, you mark the changed file:
git add <filename>
Tags You can use tagging to mark a significant changeset, such as a release:
git tag 1.0.0 <commitID>
CommitId is the leading characters of the changeset ID, up to 10, but must be unique. Get the ID using:
git log
Push all tags to remote repository:
git push --tags origin
Undo local changes If you mess up, you can replace the changes in your working tree with the last content in head:

Changes already added to the index, as well as new files, will be kept.

git checkout -- <filename>
Instead, to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it, do this:
git fetch origin

git reset --hard origin/master
Search Search the working directory for foo(): git grep "foo()"
GITHub Project Push Commands
0) git config --global user.email "Your-Email"
1) git init
2) git status
3) git add .  // add all current directory files
4) git commit -m "First Commit"
5) git remote add origin "repository URL"
6) git push origin master

Pull Single file from GITHub
   git checkout -b Branch_name

Delete Single file from GITHub
   git branch -d Branch_name

To update local repository to the newest commit
   git pull
   
  To create new branch please follow below steps

Note : while creating new branch, switch to develop or Your latest code branch and create your branch by referring that specific branch.

1. Switch to develop branch (git checkout develop)
2. Update your local develop branch by taking a pull request (git pull origin develop)
3. Created new branch eg. NotificationTest (git checkout -b NotificationTest)
4. Write a code in that branch
5. Perform git add . and git commit 
6. Push the code to remote repo (git push origin branch_name )
7. Raised Pull request for “NotificationTest”

Refer this https://confluence.atlassian.com/bitbucketserver/basic-git-commands-776639767.html

How to review PR:-

1. Open the PR
2. Go to Files changed tab ( This is where you will see what changes were made in this PR. What files were deleted/added etc )
3. Click on green Review Changes  button ( This is after you finished reviewing everything )
4. Write a comment ( Generally “LGTM” which means Looks Good To Me ) in Leave a comment box.
5. Select Approve option ( To approve )
6. Click Submit review



Basic Git commands | Bitbucket Data Center and Server 8.1 | Atlassian Documentation
https://confluence.atlassian.com

Git CheatSheet.