Table of contents
- Launching an EC2 instance
- Installing Git
- Creating user
- Git initialization
- Pushing into local repository i.e. [.git]
- Creating a GitHub repository
- Create token
- Pushing into the remote repository [GitHub]
- gitignore
- Git Branch
- Git Merge
- Merge Conflict
- Git Stash
- Git Reset
- Git Restore
- Git Revert
- Git Rebase
- Git Fetch
- Git Cherry-pick
- Git Clone
Launching an EC2 instance
Launch an Ubuntu instance using all free-tier resources.
Connect to your instance using ssh.
Go to the directory where you have stored your ssh key, then use the ssh command.
Installing Git
Switch to root: sudo su
Check git: which git
Install git: apt install git
Check the git version: git --version
Creating user
git config --global
user.name
"wroopaq"
git config --global
user.email
"
wroopaq@ultron.com
"
git config --list
Git initialization
Create a directory: mkdir project-eternals
Go into the directory: cd project-eternals/
Git initialize: git init
Check the hidden git directory: ls -a
Pushing into local repository i.e. [.git]
Working in the git workspace
Adding to staging: git add .
or git add <filemane>
Check status: git status
Commit: git commit -m "first draft script"
Check log: git log --oneline
Creating a GitHub repository
Create token
Pushing into the remote repository [GitHub]
Add remote repository: git remote add origin
https://github.com/Rupak-Shrestha/Project-Eternals.git
Check the connected repository: git remote -v
Set URL with token so that it doesn't ask for username and password every time you push: git remote set-url origin
https://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@github.com/Rupak-Shrestha/Project-Eternals.git
Syntax: git remote set-url origin https://<your-token>@<your-github-repo-url>
Push to the central git repository: git push origin master
Check the GitHub repo status
gitignore
Create a .gitignore file and add the files you want to ignore in it.
Push .gitignore to your local repo: git add .gitignore
and git commit -m "added gitginore and few more files"
Created a few files with some of them mentioned in the .gitignore file.
Pushed to remote repo: git push origin master
Git Branch
To create a branch: git branch dev
Create and enter into the branch instantly: git checkout -b test
List branches: git branch
Switch branches: git checkout dev
To delete branch: git branch -d test
Git Merge
Created a new branch: git checkout -b writer
Worked in a file: echo "this is ironman5 script written by writer." > ironman5.txt
Git add: git add .
Git commit: git commit -m "ironman5 script by writer"
Switched to master branch: git checkout master
Merged the writer branch: git merge writer
Check log: git log --oneline
Merge Conflict
Creating a merge conflict:
Go to the writer branch: git checkout writer
Make some changes: echo "some changes by writer" >> ironman5.txt
Add and commit to repo: git add .
and git commit -m "changes by writer"
Go to the master branch: git checkout master
Make some changes: echo "I'm the boss, my changes should come first" >> ironman5.txt
Add and commit to repo: git add .
and git commit -m "my script my rule"
Merge the writer branch: git merge writer
Error observed:
Auto-merging ironman5.txt
CONFLICT (content): Merge conflict in ironman5.txt Automatic merge failed; fix conflicts and then commit the result.
To resolve conflict: Open the file from master using any text editor and remove the following lines, then define the order of content.
Add and commit again: git add .
and git commit -m "fixed merge conflict"
Git Stash
Created a new file: echo "captain america script" > captain-america.txt
Added to staging and checked status.
Sent staged files to stash: git stash
Checked status; the working tree was clean
Check the stash list: git stash list
Adding black widow script while captain america script is in stash:
Getting back captain-america.txt to workspace from stash: git stash apply stash@{0}
Adding some changes: echo "changes to cap-am" >> captain-america.txt
Add and commit: git add .
and git commit -m "adding captain america"
Check log: git log --oneline
To clear the stash list: git stash clear
Git Reset
Create a file.
Add to staging.
Reset whatever is in staging: git reset .
or git reset <filename>
File is back to workspace. It can clear commit history as well.
Git Restore
Added avengers.txt in the staging and restore it with: git restore --staged avengers.txt
It will only get back the files from staging. Comparatively, safer to use than git reset.
Git Revert
Can remove the commit.git revert <commit id>
Upon pushing the files to remote repo after reverting the commits, all the commit that were revert are not available in the remote repository.
Git Rebase
Switch to writer branch: git checkout writer
Check log: git log --oneline
Pull all the commit from master branch: git rebase master
All commits are now available in the log of writer branch.
In case of git merge writer
, all the changes made by writer branch is merged into master branch but in git rebase master
, the writer branch pulled all the commit made by master branch or all the commit available in master branch at the moment.
Git Fetch
Create a test branch in GitHub repository.
A new hulk.txt file is added inside the test branch.
Initially, there was no test branch in local repo.
There was no hulk.txt file as well.
Upon using: git fetch
It pulled whatever was in remote repository.
Now, I can go inside test branch with: git checkout test
And the new hulk.txt file is also available but the local files were not changed during this process which makes it different from git pull./
Git Cherry-pick
Switched to writer branch: git checkout writer
Created file gog.txt: echo "Guardians of Galaxy" > gog.txt
Add and commit gog.txt: git add gog.txt
and git commit -m "script of guardians of galaxy"
Created file spiderman.txt: echo "Spiderman script" > spiderman.txt
Add and commit spiderman.txt: git add spiderman.txt
and git commit -m "script of spiderman"
Check log: git log --oneline
Now, I want to add the commit of script of guardians of galaxy
to my master branch but not everything.
So, I'll use cherry-pick.
Go to master branch: git checkout master
Checked the log.
Pick particular commit: git cherry-pick 23ea995
i.e. git cherry-pick <commit-id>
On checking log again, commit with the given commit id is now available at master branch.
Git Clone
Go to remote repository and copy the repository link, then clone the repo with: git clone
https://github.com/Rupak-Shrestha/crud-laravel.git
git clone <repo-url>
Thank you for reading! Happy learning!