Swing with git basic
Tags: git, github, devops, linux
Published: 2020-07-24 18:03:54

## 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  
```

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