DEV Community

Cover image for Swing with git basic
Abhishek S. Sharma
Abhishek S. Sharma

Posted on

Swing with git basic

Draw your git sword

Note- will start with the understanding of git commands

  • To see present origin repo

    git remote -v

  • To add origin

    git remote add origin --remote repo URL--

  • origin can be named anything but it should be remembered by you for future references

  • to set a new remote origin --remote repo url--

    git remote set-url origin --remote repo url--

Notes

  • set commit username (it sets globally, please change it if you want to change your name in other commits) > git config --global user.name "Abhishek Sharma"
  • set commit email

    git config --global user.email "imabtiwari@gmail.com"

  • set user name in the previous commit if it is from another user name, it will open in vi editor press i to insert words; to save it press- ESC ":wq" or to save without an edit press :q

    git commit --amend --author="Abhishek Sharma imabtiwari@gmail.com"

Kneel before git

  • to clone a repo

git clone --remote repo url--

branch creation

  • to create a new branch

    git checkout -b branchname
    git checkout -b branchname-1

  • to switch branch from one to another (presently the branch is branchname-1)

    git checkout branchname

  • to track changes (it will show modified files)

    git status

  • to add a specific file

    git add filename, filename2

  • to add all files at once

    git add .

  • to commit on your local repo

    git commit -m "commit message"

  • to push changes on origin

    git push origin branchname

  • Checkout to another branch to merge the changes in the branch

    git checkout branchname-2

  • before the merge, we will take the latest pull from this remote branchname-2

    git pull origin master

  • now merge the branchname into branchname-2

    git merge branchname-2

  • push changes back to the master branch

    git push origin branchame-2

branch deletion

  • delete the branch from the local system > git branch -d branchname
  • delete the branch from a remote system > git push origin --delete branchname

git never forgets, it will help others to find your mistake and they blame you

  • details > git log
  • onliner details > git log --oneline

Commit details

  • It will show you changes of a specific commit

git show commitID (hashcode)
e.g. git show 785767e

  • it points to the latest commit

git show HEAD

  • to see head to the previous commit

git show HEAD~1

  • to previous to the previous commit

git show HEAD~2

Let understand the real git process in the companies with an example-

  • create a GitHub account or log in to your account
  • Create a repo "git-basic" in your git account

  • create a repo in the local system

    mkdir git-basic
    cd git-basic

We have only one branch -master for this repo

  • create a branch dev

git checkout -b dev

  • create a file named as git-basic

touch git-basic.txt

  • create write in file - "I am git ninja" and save (ctrl+s or cmd +s) this files

  • check this file to track the changes

git status

  • the git-basic file is not staged right now, so we will add the file in the stage area

git add git-basic.txt

  • we will commit the changes

git commit -a "first git commit"

  • now checkout to master branch to merge the changes

git checkout master

  • add remote repo (Github account repo)

git remote add origin -- remote repo url--

  • pull the latest update from the origin in this branch

git pull origin master

  • its time to merge the dev branch in master

git merge dev

  • push the changes in the remote directory

git push origin master

** Hope, you like my post!**

Thanks,
Abhishek Sharma

Discussion (7)

Collapse
kartik_saxena14 profile image
Kartik Saxena

Really Helpful...

Collapse
rashigu82610047 profile image
Rashi Gupta

Quite beneficial

Collapse
itdeepa profile image
Deepa khandelwal

Very informative content

Collapse
prempurswani3 profile image
Prem Purswani

Very informative. Will use this as a cheatsheet for git commands

Collapse
nishikaushik2 profile image
Nishi kaushik

Very helpful and beneficial content

Collapse
utkarsh26642788 profile image
Utkarsh Soni • Edited on

Very informative and well explained
Thanks for the info.

Collapse
shubham2296 profile image
Shubham Kumar

Very well explained and quite handy information...
Thanks for sharing