DEV Community

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

Posted on Edited 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 
Enter fullscreen mode Exit fullscreen mode
  • To add origin
git remote add origin --remote repo URL-- 
Enter fullscreen mode Exit fullscreen mode
  • 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-- 
Enter fullscreen mode Exit fullscreen mode

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" 
Enter fullscreen mode Exit fullscreen mode
  • set commit email
git config --global user.email "imabtiwari@gmail.com" 
Enter fullscreen mode Exit fullscreen mode
  • 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>" 
Enter fullscreen mode Exit fullscreen mode

Kneel before git

to clone a repo

git clone --remote repo url-- 
Enter fullscreen mode Exit fullscreen mode

branch creation

  • to create a new branch
git checkout -b _branchname_ 
git checkout -b _branchname-1 _
Enter fullscreen mode Exit fullscreen mode
  • to switch branch from one to another (presently the branch is branchname-1)
git checkout _branchname_
Enter fullscreen mode Exit fullscreen mode
  • to track changes (it will show modified files)
git status
Enter fullscreen mode Exit fullscreen mode
  • to add a specific file
git add filename, filename2
Enter fullscreen mode Exit fullscreen mode

to add all files at once

git add .
Enter fullscreen mode Exit fullscreen mode
  • to commit on your local repo
git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

to push changes on origin

git push origin _branchname_ 
Enter fullscreen mode Exit fullscreen mode
  • Checkout to another branch to merge the changes in the branch
git checkout _branchname-2_
Enter fullscreen mode Exit fullscreen mode
  • before the merge, we will take the latest pull from this remote branchname-2
git pull origin master
Enter fullscreen mode Exit fullscreen mode
  • now merge the branchname into branchname-2
git merge branchname-2
Enter fullscreen mode Exit fullscreen mode
  • push changes back to the master branch
git push origin branchame-2
Enter fullscreen mode Exit fullscreen mode

branch deletion

  • delete the branch from the local system
git branch -d _branchname_
Enter fullscreen mode Exit fullscreen mode
  • delete the branch from a remote system
git push origin --delete _branchname_
Enter fullscreen mode Exit fullscreen mode

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

  • details
git log  
Enter fullscreen mode Exit fullscreen mode
  • oneliner details
git log --oneline 
Enter fullscreen mode Exit fullscreen mode

Commit details

  • It will show you changes of a specific commit
git show commitID (hashcode)
e.g. git show 785767e
Enter fullscreen mode Exit fullscreen mode
  • it points to the latest commit
git show HEAD 
Enter fullscreen mode Exit fullscreen mode
  • to see head to the previous commit
git show HEAD~1 
Enter fullscreen mode Exit fullscreen mode
  • to previous to the previous commit
git show HEAD~2 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

We have only one branch -master for this repo

  • create a branch dev
git checkout -b dev
Enter fullscreen mode Exit fullscreen mode
  • create a file named as git-basic
touch git-basic.txt
Enter fullscreen mode Exit fullscreen mode
  • create write in file - "I am a git ninja" and save (ctrl+s or cmd +s) this files

  • check this file to track the changes

git status 
Enter fullscreen mode Exit fullscreen mode
  • the git-basic file is not staged right now, so we will add the file in the stage area
git add git-basic.txt
Enter fullscreen mode Exit fullscreen mode
  • we will commit the changes
git commit -a "first git commit"
Enter fullscreen mode Exit fullscreen mode
  • now checkout to master branch to merge the changes
git checkout master 
Enter fullscreen mode Exit fullscreen mode
  • add remote repo (Github account repo)
git remote add origin -- remote repo url-- 
Enter fullscreen mode Exit fullscreen mode
  • pull the latest update from the origin in this branch
git pull origin master
Enter fullscreen mode Exit fullscreen mode
  • its time to merge the dev branch in master
git merge dev
Enter fullscreen mode Exit fullscreen mode
  • push the changes in the remote directory
git push origin master
Enter fullscreen mode Exit fullscreen mode

Hope, you like my post!

Thanks,
Abhishek Sharma

Top comments (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
 
nishikaushik2 profile image
Nishi kaushik •

Very helpful and beneficial content

Collapse
 
prempurswani3 profile image
Prem Purswani •

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

Collapse
 
utkarsh26642788 profile image
Utkarsh Soni • • Edited

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