git log
git log -p <file_name>
# EXAMPLE
git log -p README.md
git blame <file_name>
#Example
git blame README.md
git branch
git branch -av
git checkout <branch_name>
# Example
git checkout szabolcsthedeveloper/testBranchName
git checkout -b <brach_name>
# Example
git checkout -b szabolcsthedeveloper/myNewBranchName
git branch -d <branch_name>
# Example
git branch -d szabolcsthedeveloper/myNewBranchName
## Force delete if not merged
git branch -D szabolcsthedeveloper/myNewBranchName
git push origin --delete <branch_name>
# Example
git push origin --delete szabolcsthedeveloper/myNewBranchName
pull: gets latest pushed by someone to the branch
git pull
push: push local changes to the remote branch
git push -u origin <branch_name>
# Example
git push -u origin szabolcsthedeveloper/myNewBranchName
- Create the directory
- Change to directory
- Initialize the directory with git and check for hiden folders in the directory
git init
, to initiate the current directory to connect to the version controlling system or any git server
mkdir learn-git/
cd learn-git/
git init
O/P : Initialized empty Git repository in
learn-git
directory
- After initializing the repository, it will create the hidden folder in the directory
/home/user/current-path/learn-git
. - To view hidden fine in direct
Ctrl + H
- Directory file structure looks like this
---
root
branch/
config
description
HEAD
hooks/
applypatch-msg.sample
commit-msg.sample
fsmonitor-watchman.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
prepare-commit-msg.sample
pre-push.sample
pre-rebase.sample
pre-receive.sample
update.sample
info/
exclude
objects/
info/
pack/
refs/
heads/
tags/
git status
git add .
# OR
git add --all
# OR
git add hello-world.js
# OR
git add dir/
git status
git commit -am "hello world changes"
-am
: All commits with message
git push -u origin branchName
# OR
git push # pushes the current branch to the configured upstream
git checkout master
Note: This could be done in any branch. For this example, the master branch is used.
git log
You will end up with a list of commits that you made as follows.
Step 3.1 - Copy the commit-hash that you want to reset
All the commits that top of the selected commit-hash (not including the entered commit-hash), will be deleted.
Step 3.2 - Hard reset to go back to the early stage
git reset <commit-hash> --hard
git push <remote> master --force
<remote>
can be origin
is the default.
NOTE: Be careful when removing the previous commits, there is no going back once you did these changes.
ssh-keygen -t rsa -C "[email protected]"
Note : In local machines we may have multiple ssh keys, better to create the specific rsa. Example : Enter file in which to save the key
(/home/ubuntu/.ssh/id_rsa): /home/ubuntu/.ssh/github_rsa
O/P:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): /home/ubuntu/.ssh/github_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/github_rsa.
Your public key has been saved in /home/ubuntu/.ssh/github_rsa.pub.
The key fingerprint is:
SHA256:vddx6bSnZdfSM0/6pImd5l+yiycMZRMetgp70NuCcRcM *******@outlook.com
The key's randomart image is:
+---[RSA 2048]----+
| .. . |
| .Eo . |
| o.... |
|.oo.. . |
|=.*. T . |
| *.+ . . |
| ..o.. . . ..B. |
| .+.. o oo .=@+o |
| +o o. oo..=@J |
+----[SHA256]-----+
cat /home/ubuntu/.ssh/github_rsa.pub
O/P ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/iLoHDq+6Bz5m5ED1TezxtCeW4U7eZueEqFX2eMo/BRQLVLzIMP7YyiYBR0xX57MgQ4cVodJV8pM0PYrSGSI1lQ5POSMrY4RDrH+KCVbLpifZAjaI94IKJtjnRm9eynk11g9DCg3z+OlxXmBBs1AO/zzqBXBoekfU753bD4u1yhycHiq6Iis9B2FHbV1Yov9ofswnZxh/xX7gXghLo4bdjpwfgDCRlUl4VUf7AeMY3ACwYsiEs1P6R0d1SUITgkP8D6pjmaxbroWLex43wkUxuS+nKJ9/kw7AmWnupBrUi0gfYzNwJI55vkOyhCF7 ********@outlook.com
2. Go to github account settings page
ssh -T [email protected]
O/P The authenticity of host 'github.com (192.30.253.113)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts. Hi szabolcsthedeveloper! You've successfully authenticated, but GitHub does not provide shell access.
-
Copy Clone with ssh URL - Clock Use SSH as in first screenshot
git remote set-url origin [email protected]:szabolcsthedeveloper/Git-Mastery.git
git add --all
git commit -am "testing the changes"
git push -u origin master
DONE with Github
git checkout develop
git pull
git checkout -b szabolcsthedeveloper/branchName
szabolcsthedeveloper
: the user name of the GitHub, to recognize the user by branch name easily when working with the team
branchName
: branchName for readability we maintain NamingConvension for naming the branch.
rebase: re-write history commits in a different place
git rebase <commit-hash>
revert: inverse the changes from history and create a new commit
git revert <commit-hash>
These steps assume that the origin of the repository is on a web-based hosting service such as GitHub or BitBucket.
Enter you password, and then navigate to the required folder location.
cd /path/to/folder
Clone the repository
git clone http://bitbucket.com/your-repository
Now that the repository is on the server, lets see how we would update it after making local changes
Let's add a new file to the local repo, commit the changes, and push to origin
cd ~/my-local/repo
touch new-file.txt
git add .
git commit -m "added new-file.txt"
git push orgin master
The new file is now on the BitBucket repo, but not on our web hosting sever. So, we need to ssh back into it, and pull the changes from origin.
ssh [email protected]
cd /path/to/repo
Now if we run 'git status' you will see that the repo is up to date. This is because we need to ge the latest commit details from origin
git remote update
And pull the changes
git pull
Consider master branch as the single branch for both prod and dev
git checkout master
git checkout -b szabolcsthedeveloper/Sample
szabolcsthedeveloper/Sample - branch name, choose as you like
NOTE: Good practice to follow some Naming Convention to create branch names but not mandatory szabolcsthedeveloper is the github user name and szabolcsthedeveloper/Sample is the branch name**
git status
git add --all
git commit -am "message related to changes done in the branch"
git status
git push -u origin szabolcsthedeveloper/Sample
-a = all, m = message, push all your changes to your remote or origin branch
Step 3.1 - Get the latest from the master, considering that contributors are more than one member
git checkout master
git pull
Step 3.2 - Merge master to your branch
git checkout szabolcsthedeveloper/Sample
git merge --no-ff origin master
You may get conflicts here if someone modified the same file which you modified
Need to resolve conflicts if any and repeat Step 2
Step 3.3 - Merge your branch to master
git checkout master
git merge --no-ff origin szabolcsthedeveloper/Sample
This completes the flow of basic branching
Merging changes from feature_branch to develop
- Get latest from develop and merge to feature_branch
git checkout develop
git pull
git checkout feature_branch
git merge --no-ff origin develop
Note : To be safe from conflict with develop(GOOD PRACTICE), we will resolve in feature_branch
- Merge feature_branch to develop
git checkout develop
git merge --no-ff origin feature_branch
- Soft Reset
git reset <commit_hash> --soft
- Mixed Reset
git reset <commit_hash>
#OR
git reset <commit_hash> --mixed
- Hard Reset
git reset <commit_hash> --hard
- List your existing remotes to get the name of the remote you want to change.
git remote -v
origin [email protected]:szabolcsthedeveloper/szabolcsthedeveloper.github.io.git (fetch)
origin [email protected]:szabolcsthedeveloper/szabolcsthedeveloper.github.io.git (push)
- Change your remote's URL from SSH to HTTPS with the
git remote set-url
command.
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git