Introduction to Git and GitHub
Git is a version control system.Git helps you keep track of code changes.
Git is used to collaborate , tracks changes, and allows reverting to previous project states.
Used to store our project in a server.
Git-Hub is one of the example of remote repository hosting service.it means using/storing other person's project
file in our own server.
Why Git?
Over 70% of developers use Git!
Developers can work together from anywhere in the world.
Developers can see the full history of the project.
Developers can revert to earlier versions of a project.
Setting Up Git
1 Install Git 2 Verify Setup
Linux: git - -version
sudo apt-get install git (git Successfully installed)
3 Configure Git:
git config --global user.name "Your Name"
git and git-hub has 4 parts
1. VCS and Git repositories (version controlling system)
2. Staging area and Commits
3. Working with branches
4. Issues and pull requests
In git we have 4 phases
1. Remote repository - remote means server. repository means folder for storing our project .
2. Local repository - storing in our own computer
3. Staging area
4. Working directory
Initialization
Now create a new folder and open it in terminal. Enter below commands
`git init`
-This will initialize a Git repository in your working directory. (Use this folder as Git
repository)
`git status`
- Check Repository Status : Use the command git status to see the current state
of your working directory.
This will show you any new or modified files that Git has detected.
Add Files to Git
- Create some new text files in your repository folder. Then run git status again -
you should see the new files listed.
This is the first step to tracking your changes with Git.
These are the Commands to initiate in Git for tracking.
Basic Git Commands
" git init - Initialize a repository
" git clone - Clone a repository
" git status - Check repository status
" git add - Add files to staging
" git commit -m "message" - Commit changes
" git log - View commit history
" git reset <commit-id> - Undo commits
Staging
git add example.txt -- Use the command git add example.txt to add your files to the
staging area , where Git keeps track of your changes.
git status -- Run git status to see which files are ready to be committed to your local
repository.
git commit -m "committing code and example" -- When you're ready, commit your
changes with git commit -m "committing code and example". This saves a snapshot of
your files in your local repository.
git status
git remote add origin 'copied link' -- Add the remote GitHub repository with git remote add
origin 'copied link', then check the status with git status.
git status
git branch -- we will get list of all the branches that are present in our repository.
git push -u origin master -- Finally, push your committed changes to the remote GitHub
repository with git push -u origin master.
Refresh your GitHub page to see the updated files!
Branching
Our code is present in main/master branch right !
So we can now deploy or redeploy our code by creating duplicate branches.
How to create a branch?
>git branch //here we can see newly created branch
>git checkout -b "Copy1" //Switch branches . if that branch is not present it creates
new branch.
>git status
>git merge <branch-name> - Used to Merge branches
Now insert a file into newly created branch let our file be 'Example.txt'
>git add Example.txt
>git status
>git commit -m "comment"
>git push - -set- upstream origin copy1
In copy1 branch we find extra files than master branch in order to merge both files we use
'compare and pull request'.
after that if you need leave a comment later on, click on 'merge pull request' and 'confirm
merge'.
Now we can see that the files in copy1 branch is in master branch also.
In order to make some changes to already existing repository we don't have direct
permissions to modify it.so we use fork to dump that code into our own server.
so to get the folder into our space click on code and copy 'https://' link
now create new folder and open terminal
>git clone 'copied http link' //now that file is added to our folder
Now make changes to our files
>git status
push that file into repository and pull request if they like modified code,then they will
merge your code to global server.
GitHub
Clone
2
Account 1
3
Fork
A web-based platform for version control and collaboration using Git.
Pushing and pulling from git-hub
" git remote add origin <repo-url> - Connect to GitHub
" git push origin <branch> - Push changes
" git pull origin <branch> - Pull latest changes
Remote operations
`git push` - uploads local repository content to remote repository content.
`git pull` - Fetches and merges changes from a remote repository into your local branch.
`git fetch` - Downloads changes from a remote repository without automatically
merging them into your own work.
Commands to interact with remote repositories.
Git Workflow and Best Practices
1 2 3
Branch Commit Pull
Use branching strategies. Commit often with messages. Pull remote changes regularly.
" Use meaningful commit messages
" Always pull before pushing
" Use .gitignore to avoid unnecessary files