Git and GitHub – Understanding Git Concepts (Part-10)

Posted by

Understanding Git Concepts

1. Git Branching and Merging

Branching allows you to create a separate line of development. It’s useful for working on different features or fixes independently.

Merging is the process of integrating changes from different branches.


# Create a new branch named 'feature-branch'
git checkout -b feature-branch

# Work on the feature, then commit changes
git commit -m "Added new feature"

# Switch back to master branch
git checkout master

# Merge changes from 'feature-branch' into 'master'
git merge feature-branch

2. Conflict Resolution (conf res)

Conflicts occur when changes from different branches clash. Git needs help to decide which changes to keep.


# When merging, if there's a conflict, Git will show the conflicting files
git merge feature-branch

# Open the file and manually resolve the conflict, then add the resolved file
git add conflicted-file.txt

# Complete the merge
git commit -m "Resolved merge conflict"

3. Cherry Picking

Cherry picking allows you to apply a specific commit from one branch onto another.


# Identify the commit hash you want to cherry-pick (e.g., abc1234)
git checkout master
git cherry-pick abc1234

4. Stash

Stashing allows you to temporarily save changes without committing them.

Stashing is useful for saving your work in progress without committing it. This is helpful when you need to switch branches without committing incomplete changes.


# Save current changes
git stash

# Work on something else, then retrieve the stashed changes
git stash apply

git stash list

5. Tag

Tags are used to mark specific points in history as important, typically used for releases.

Tags are used to mark specific points in the repository’s history. This is commonly used to mark release points.


# Create a new tag named 'v1.0'
git tag v1.0

# Push tags to the remote repository
git push origin --tags

6. .gitignore

The .gitignore file specifies which files and directories to ignore in a Git repository.

The .gitignore file specifies intentionally untracked files that Git should ignore. This is useful for excluding files like build artifacts and temporary files.

Example: Create a .gitignore file:

# Ignore all .log files

# Ignore the node_modules directory

7. Git Pull (pull = fetch + merge)

Pulling combines fetching changes from a remote repository and merging them into your current branch.


# Fetch changes from the remote repository and merge them into the current branch
git pull origin master

8. Concept of HEAD

HEAD is a pointer that refers to the latest commit in the current branch. It’s essentially a reference to where you are in your Git history.


  • If you are on the master branch, HEAD points to the latest commit on master.

Command to see HEAD:

git log -1

9. Git Undoing

Git provides several commands to undo changes:

  • Revert a commit:
git revert <commit-hash>

This creates a new commit that undoes the changes of a previous commit.

Reset changes:

git reset --hard <commit-hash>

This resets your current branch to the specified commit, discarding all changes after that commit.

Remove a file from staging:

git reset HEAD <file>

Discard local changes:

git checkout -- <file>

Explanation of Commit Line

To further explain 61c4cfc (origin/master, origin/HEAD) adding other user:

  1. Commit Hash: 61c4cfc
    • A unique identifier for this commit.
  2. Branch and Remote References:
    • origin/master: This commit is the latest on the master branch of the remote repository named origin.
    • origin/HEAD: This commit is also the HEAD of the remote repository, indicating the latest commit on the default branch.
  3. Commit Message: adding other user
    • Describes the changes made in this commit.

This commit signifies that the changes described (adding another user) are the latest in the remote repository’s master branch.


These concepts form the basics of working with Git, allowing you to manage your codebase effectively:

  • Branching and Merging for independent development and integration.
  • Conflict Resolution for handling merge conflicts.
  • Cherry Picking for applying specific commits.
  • Stash for temporary storage of changes.
  • Tags for marking important points.
  • .gitignore for ignoring specific files.
  • Pull for fetching and merging remote changes.
  1. Concept of HEAD:
    • Pointer to the latest commit in the current branch.
  2. Git Undoing:
    • Reverting commits: git revert <commit-hash>
    • Resetting commits: git reset --hard <commit-hash>
    • Removing files from staging: git reset HEAD <file>
    • Discarding local changes: git checkout -- <file>
  3. Git Branching and Merging:
    • Creating branches: git branch new-feature
    • Switching branches: git checkout new-feature
    • Merging branches: git merge new-feature
  4. Cherry Picking:
    • Applying specific commits to another branch: git cherry-pick <commit-hash>
  5. Stash:
    • Stashing changes: git stash
    • Listing stashes: git stash list
    • Applying stashes: git stash apply
  6. Tag:
    • Creating tags: git tag v1.0
    • Pushing tags: git push origin --tags
  7. .gitignore:
    • Ignoring files and directories.

Live Practical Examples:

26  git add
   27  git config "Jami Raj"
   28  git config ""
   29  git config --list
   31  git commit -m"Adding first commit"
   35  git show
   36  git show ba9fae72620636a02321a558cc71836bad99ca0f
   39  git status
   40  git add
   45  git status
   46  git commit -m"file1"
   47  git status
   48  git commit -m"all"
   49  git status
   53  git checkout ba9fae72620636a02321a558cc71836bad99ca0f
   55  git log
   56  git status
   57  git checkout 3d0f84561e39cdfc6724c77b2b153e245dc6bf22
   59  git checkout 3d0f84561e39cdfc6724c77b2b153e245dc6bf22
   61  git checkout master
   65  git push master
   66  git clone
   68  cd Git_demo/
   69  ls
   70  mkdir file3
   71  git add
   72  git add .
   73  git commit -m"all"
   78  touch file3
   79  git status
   80  git config :"RAj"
   81  git config ""
   82  git add
   83  git add --all
   84  git status
   85  git commit -m"adding other user"
   86  git status
   87  git log
   88  git push origin master
   93  git remote add origin
   94  git pull origin master
   97  git show head
   98  git log --oneline
   99  touch file5
  100  git clean -f -d
  101  touch file5
  102  git status
  103  git clean -f -d
  104  touch file5
  105  git add file5
  106  git status
  107  git reset file5
  108  git status
  109  touch good;git add --all;git commit -m"good"
  110  ls
  111  git status
  112  touch bad;git add --all;git commit -m"good"
  113  git status
  114  ls
  115  git log --oneline
  116  touch bad;git add --all;git commit -m"bad"
  117  git log --oneline
  118  ls
  119  ls
  120  cd Git_demo/
  121  ls
  122  touch good.txt ;git add --all;git commit -m"good"
  123  touch bad.txt ;git add --all;git commit -m"bad"
  124  git log --oneline
  125  git diff cd9fc7b 2c8439c
  126  git revert 2c8439c
  127  git log --oneline
  128  history
  129  git branch -a
  130  git branch fea1
  131  git branch -a
  132  git checkout fea1
  133  git branch -a
  134  touch file9.txt ;git add --all;git commit -m"file9"
  135  touch file10.txt ;git add --all;git commit -m"file10"
  136  git log --oneline
  137  git checkout master
  138  git merge fea1
  139  ls
  140  git merge fea1
  141  git checkout fea1
  142  touch cheery.txt ;git add --all;git commit -m"cherry"
  143  touch cheery2.txt ;git add --all;git commit -m"cherry2"
  144  git log --oneline
  145  git checkout master
  146  ls
  147  git cherry-pick be4435a
  148  ls
  149  touch aa1.txt
  150  touch aa2.txt
  151  git add .
  152  git status
  153  git stash
  154  git status
  155  git stash list
  156  git stash apply stash@{0}
  157  git status
  158  git tag --list
  159  git log
  160  git tag rel1.0
  161  git tag --list
  162  git show rel1.0
  163  git checkout rel1.0
  164  ls
  165  git push origin rel1.0
  166  git branch ramukaka
  167  git checkout ramukaka
  168  git add .
  169  git status
  170  git commit -m"ramukka"
  171  git status
  172  ls
  173  git branch
  174  git log
  175  git log --oneline
  176  history
  177  ls
  178  git log --oneline
  179  git checkout rel1.0
  180  ls
  181  git checkout master
  182  ls
  183  git log --oneline
  184  git checkout 61c4cfc
  185  ls
  186  git checkout ramukaka
  187  git merge ramukaka
  188  ls
  189  git checkout rel1.0
  190  ls
  191  git merge ramukaka
  192  ls
  193  git log --oneline
  194  git checkout rel1.0
  195  ls
  196  git merge rel1.0
  197  git merge ramukaka
  198  ls
  199  git merge rel1.0
  200  ls
  201  git log --oneline
  202  ls
  203  git checkout master
  204  ls
  205  git merge ramukaka
  206  ls
  207  git checkout rel1.0
  208  ls
  209  git checkout master
  210  git tag reli2.0
  211  git checkout reli2.0
  212  ls
  213  git push origin reli2.0
  214  ls
  215  git log --oneline
  216  git branch test
  217  ls
  218  git checkout test
  219  ls
  220  git checkout cherry2
  221  git branch
  222  git checkout ramukaka
  223  ls
  224  git checkout fea1
  225  ls
  226  git branch
  227  git branch test2
  228  ls
  229  history
  230  git branch
  231  git checkout test2
  232  ls
  233  mkdir merge.txt
  234  ls
  235  git status
  236  git status
  237  rm -f merge.txt/
  238  rm -rf merge.txt/
  239  ls
  240  touch merge.text
  241  git status
  242  git log --oneline
  243  git add .
  244  git log
  245  git status
  246  git commit -m"merge example"
  247  git status
  249  git branch test
  250  git checkout test
  252  git merge test2
  254  git merge test2
  255  git tag merge1.0
  256  git log --oneline
  257  git push origin test
  259  git checkout merge1.0
  261  git push origin merge1.0
  262  git log --oneline

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x