Skip to content

Latest commit

 

History

History
582 lines (376 loc) · 11.3 KB

cheatsheet.md

File metadata and controls

582 lines (376 loc) · 11.3 KB

Common Commands using git

Commit History

Show all commits, starting with the newest
git log
Commit history of specific file
git log -p <file_name>

# EXAMPLE
git log -p README.md
Commit history by user
git blame <file_name>

#Example
git blame README.md

Branches

List all local branches
git branch
List all local and remote branches
git branch -av
Switch to an existing branch
git checkout <branch_name>

# Example
git checkout szabolcsthedeveloper/testBranchName
Create a new branch
git checkout -b <brach_name>

# Example
git checkout -b szabolcsthedeveloper/myNewBranchName
Delete the local branch
git branch -d <branch_name>

# Example
git branch -d szabolcsthedeveloper/myNewBranchName

## Force delete if not merged
git branch -D szabolcsthedeveloper/myNewBranchName
Delete remote/origin branch
git push origin --delete <branch_name>

# Example
git push origin --delete szabolcsthedeveloper/myNewBranchName

Pull and Push

Get the latest from remote

pull: gets latest pushed by someone to the branch

git pull
Push local changes

push: push local changes to the remote branch

git push -u origin <branch_name>

# Example
git push -u origin szabolcsthedeveloper/myNewBranchName

Git Init

  • 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/

Committing files

Status

git status

Git Add

git add .

# OR

git add --all

# OR

git add hello-world.js

# OR

git add dir/

Check status

git status

Commit

git commit -am "hello world changes"

-am : All commits with message

Push

git push -u origin branchName

# OR

git push # pushes the current branch to the configured upstream

Remove the last commit/commits using Reset

Steps to remove the last commit/commits

Step 1 Checkout to master

git checkout master

Note: This could be done in any branch. For this example, the master branch is used.

Step 2 - Get the commits history

git log

You will end up with a list of commits that you made as follows.

logs

Step 3 - Reset

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

Step 3 - Force push to the repository

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.

Setup git push with SSH

Generate SSH KEY with ssh-keygen

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]-----+

Github SSH connection setup

1. Copy the ssh from local machine

 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

Github Settings

3. Click on New SSH key

4. Add title and paste the copied ssh key from the local

5. Test SSH

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.

6. Update the Remote URL of the repository with the ssh url in place of https

  • Copy the url from the clone repository button with ssh

  • Copy Clone with ssh URL - Clock Use SSH as in first screenshot

click_use_ssh copy_ssh_url

 git remote set-url origin [email protected]:szabolcsthedeveloper/Git-Mastery.git

7. Test pushing with ssh

git add --all

git commit -am "testing the changes"

git push -u origin master

DONE with Github

Create Branch

Checkout to the upstream branch

git checkout develop

Pull Latest

git pull

Create a new branch

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.

GIT REBASE

rebase: re-write history commits in a different place

git rebase <commit-hash>

GIT REVERT

revert: inverse the changes from history and create a new commit

git revert <commit-hash>

Deploy and update git repository on a hosted server

These steps assume that the origin of the repository is on a web-based hosting service such as GitHub or BitBucket.

Step 1 Clone a copy of the repository to the server

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

Step 2 Update with local repo 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

Single branch for production and development

Consider master branch as the single branch for both prod and dev

Steps to create a branch and merge the branch to master

Step 1 checkout to master and create a branch

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**

Step 2 - As you are done with some changes to the code, follow

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 - Merge the newly created branch to master

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

GIT MERGE

Merging changes from feature_branch to develop

  1. 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

  1. Merge feature_branch to develop
git checkout develop

git merge --no-ff origin feature_branch

GIT RESET

  1. Soft Reset
git reset <commit_hash> --soft
  1. Mixed Reset
git reset <commit_hash>

#OR

git reset <commit_hash> --mixed
  1. Hard Reset
git reset <commit_hash> --hard

Switching remote URLs from HTTPS to SSH

List existing URLs

  • List your existing remotes to get the name of the remote you want to change.
git remote -v

O/P

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

  • 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