Git setup and command

6 minute read

What is Git?

Git is a distributed version-control system for tracking changes in source code during software development.It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

How to setup git?

  1. Generate SSH Key, Run below command and enter path and passphrase.
    ssh-keygen -t rsa
    
  2. Initiate git and configure user
    git init
    git config  --global user.name "name"	
    git config  --global user.email "user@email.com"
    
  3. Get git configuration list
    git config --list
    
  4. Update git configuration, In case you wish to update default editor
    git config --global core.editor "/usr/bin/vim"
    
  5. View git configuration
    cat ~/.gitconfig
    
  6. List .git directory contents
    ls .git
    

    HEAD - Refers to current branch we working on.
    Config - Details about the developers user name and emaills.
    hooks - Hooks are customization scripts used by various Git commands.
    index - The current index file for the repository.
    info - Additional information about the repository is recorded in this directory.
    logs - Commits and changes logs.
    refs - References are stored in subdirectories of this directory.

Frequently used git commands

  1. Download project from github
    git clone <git repos>
    example.  
    git clone https://github.com/rajnathsah/AutoResumeUpload
    
  2. Add new file to repository for commit
    git add <file name/directory name>
    
  3. Exclude files and directory from repository using .gitignore.
    touch .gitignore
    git add .gitignore
    echo "build/*" >> .gitignore
    
  4. Commit the changes
    git commit -m "<comments for commit>"
    
  5. Push local changes to github repository
    • Push changes from all local branches to matching branches the origin remote.
      git push origin
      
    • Push changes from the local master branch to the remote master branch.
      git push origin master
      
    • Push changes from the local master branch to the remote staging branch if it exists.
      git push origin master:staging
      
  6. Pull/Update the changes from git repository to local
    git pull
    
  7. Fetch all branch for a git repo
    git fetch --all
    
  8. Create branch in git
    git branch <branch name>
    
  9. List braches
    git branch
    
  10. Checkout remote branch
    git checkout <remote branch>
    
  11. Add a new remote for your branch
    git remote add [name_of_your_remote] [name_of_your_new_branch]
    
  12. Push changes from your commit into your branch
    git push [name_of_your_new_remote] [url]
    
  13. Update your branch when the original branch from official repository has been updated
    git fetch [name_of_your_remote]
    
  14. Apply to merge changes if your branch is derivated from develop you need to do
    git merge [name_of_your_remote]/develop
    
  15. Delete a branch on your local filesystem
    git branch -d [name_of_your_new_branch]
    
  16. To force the deletion of local branch on your filesystem
    git branch -D [name_of_your_new_branch]
    
  17. Delete the branch on github
    git push origin :[name_of_your_new_branch]
    

Git branch with example

  1. Creating Master and Feature Branches.
    Here is the scenario we will create:
    Git Branch
    In the above example, we are taking the following path:

Commit A: we add a.txt file in the ‘master’ branch
Commit B: we add b.txt file in the ‘master’ branch
At this stage, we create the branch ‘feature’ which means it will have a.txt and b.txt
Commit C: we add c.txt file in the ‘master’ branch
We go to the ‘feature’ branch
Commit E: we modify a.txt in ‘feature’ branch
Commit F: we modify b.txt in ‘feature’ branch
You can create a folder and run the following code inside the folder to create the above situation:

git init
touch a.txt
git add -A
git commit -m "Commit A: added a.txt"
 
touch b.txt
git add -A
git commit -m "Commit B: added b.txt"
git branch feature
 
touch c.txt
git add -A
git commit -m "Commit C: added c.txt"
git status
git checkout feature
 
echo aaa > a.txt
git add -A
git commit -m "Commit E: modified a.txt"
 
echo bbb > b.txt
git add -A
git commit -m "Commit F: modified b.txt"
  1. Simple Merge Let’s use the log command to check both branches.

Results for ‘master’:

git checkout master
Switched to branch 'master'

git log --oneline
2bbde47 Commit C: added c.txt
b430ab5 Commit B: added b.txt
6f30e95 Commit A: added a.txt
 
ls
a.txt    b.txt    c.txt
Results for ‘feature’:

git checkout feature
Switched to branch 'feature'
 
git log --oneline
0286690 Commit F: modified b.txt
7c5c85e Commit E: modified a.txt
b430ab5 Commit B: added b.txt
6f30e95 Commit A: added a.txt
 
ls
a.txt    b.txt

Notice how the feature branch does not have Commit C

Now let’s run merge ‘feature’ branch with ‘master’ branch. You will be asked to enter a comment. In the comment, add “Commit G:” at the beginning to make it easier to track.

git checkout master
Switched to branch 'master'
 
git merge feature
Merge made by the 'recursive' strategy.
a.txt | 1 +
b.txt | 1 +
2 files changed, 2 insertions(+)
Results for ‘master’:

git checkout master
Already on 'master'
 
git log --oneline
d086ff9 Commit G: Merge branch 'feature'
0286690 Commit F: modified b.txt
7c5c85e Commit E: modified a.txt
2bbde47 Commit C: added c.txt
b430ab5 Commit B: added b.txt
6f30e95 Commit A: added a.txt
 
ls
a.txt b.txt c.txt
Results for ‘feature’:

git checkout feature
Switched to branch 'feature'
 
git log --oneline
0286690 Commit F: modified b.txt
7c5c85e Commit E: modified a.txt
b430ab5 Commit B: added b.txt
6f30e95 Commit A: added a.txt
 
ls
a.txt    b.txt

In the ‘master’ branch, you will notice there is a new commit G that has merged the changes from ‘feature’ branch. Basically, the following action has taken place:
Git Branch
In the Commit G, all the changes from ‘feature’ branch have been brought into the master branch. But the ‘feature’ branch itself has remained untouched due to the merge process. Notice the hash of each commit. After the merge, E (7c5c85e) and F (0286690) commit has the same hash on the ‘feature’ and ‘master’ branch.

  1. Merging with Rebasing Let’s repeat step 1 to create the ‘master’ and ‘feature’ branches again.

Results for ‘master’:

git checkout master
Switched to branch 'master'
 
git log --oneline
7f573d8 Commit C: added c.txt
795da3c Commit B: added b.txt
0f4ed5b Commit A: added a.txt
 
ls
a.txt    b.txt    c.txt
Results for ‘feature’:

git checkout feature
Switched to branch 'feature'
 
git log --oneline
8ed0c4e Commit F: modified b.txt
6e12b57 Commit E: modified a.txt
795da3c Commit B: added b.txt
0f4ed5b Commit A: added a.txt
 
ls
a.txt b.txt
Let’s rebase from the ‘feature’ branch.

git checkout feature
Switched to branch 'feature'
 
git rebase master
First, rewinding head to replay your work on top of it...
Applying: Commit E: modified a.txt
Applying: Commit F: modified b.txt
Then merge ‘feature’ into ‘master’.

git checkout master
Switched to branch 'master'
 
git merge feature
Updating 7f573d8..9efa1a3
Fast-forward
 a.txt | 1 +
 b.txt | 1 +
 2 files changed, 2 insertions(+)
Results for ‘master’ branch:

git checkout master
Already on 'master'
 
git log --oneline
9efa1a3 Commit F: modified b.txt
8710174 Commit E: modified a.txt
7f573d8 Commit C: added c.txt
795da3c Commit B: added b.txt
0f4ed5b Commit A: added a.txt
 
ls
a.txt    b.txt    c.txt
Results for ‘feature’ branch:

git checkout feature
Switched to branch 'feature'
 
git log --oneline
9efa1a3 Commit F: modified b.txt
8710174 Commit E: modified a.txt
7f573d8 Commit C: added c.txt
795da3c Commit B: added b.txt
0f4ed5b Commit A: added a.txt
 
ls
a.txt    b.txt    c.txt

Notice that after the rebase and merge both branches are the same. Also, the hashes for E and F have changed in both branches. Basically, in the rebase scenario, this is what happened:
Git Branch
That’s why there is no new commit. The E and F commits have been recalculated and latched to the end of the ‘master’ branch.

Rebasing is a useful tool when you want to clean up the history of your work. However, there is a danger which has given birth to the golden rule.

Golden Rule of Rebasing Never rebase a public branch.

Happy learning.

Tags: ,

Updated:

Leave a comment