Git Basics

Yosuva ArulanthuGitLeave a Comment

Git is a version-control system for tracking changes in computer files and coordinating work on those files among multiple people. This page contains a few useful git commands

Most common flow of commands

$ git add .
$ git status # Lists all new or modified files to be committed
$ git commit -m "Second commit"
$ git push -u origin master
  1. Create a repository on Github
  2. Go to your local project folder and git init.
git init
  1. Add README.md file using echo "Hello" >> README.md
  2. Add .gitignore file manually and add the below content if your project is an eclipse maven project
# Eclipse
.classpath
.project
.settings/
# Intellij
.idea/
*.iml
*.iws
# Mac
.DS_Store
# Maven
log/
target/
  1. Add remote repository using git remote add origin https://github.com/ayosuva/test.git
git remote add origin https://github.com/ayosuva/test.git
  1. create branch using git branch -M master
git branch -M master
  1. Add .gitignore file alone using git add .gitignore and then commit using git commit -m ".gitignore commit"
git add .gitignore
git commit -m ".gitignore commit"
  1. push the commits using git push -u origin master use git pull origin master --allow-unrelated-histories if you have initialized the repo in GitHub and also committed locally
git push -u origin master
git pull origin master --allow-unrelated-histories
  1. Now add the all others files using git add . – Adds all the files in the local repository and stages them for commit.
git add .
  1. commit using git commit -m ".gitignore commit"
git commit -m ".gitignore commit"
  1. push the commits using git push -u origin master
git push -u origin master
  1. To clone the existing report use git clone repository path
git clone https://github.com/ayosuva/SeleniumBDD.git
  1. To pull the latest code from the current repo
git pull
  1. To know the git version
git --version
  1. To add and list git config
$ git config --global user.name "YOUR_USERNAME"

$ git config --global user.email "atest@test.com"

$ git config --global --list # To check the info you just provided
  1. To see what files are staged
git status
  1. Uncommit Changes you just made to your Git Repo
$ git reset HEAD~1
# Remove the most recent commit
# Commit again!

18. See the Changes you made to your file

$ git diff # To show the files changes not yet staged

19. Revert back to the last committed version to the Git Repo

$ git checkout .

OR for a specific file

$ git checkout -- <filename>

20. View Commit History

$ git log

Leave a Reply

Your email address will not be published.