RIPPLE: PRABIN SAHANI

Github is the go-to platform for version control and collaboration between developers. Whether you are working on open-source projects or managing a team of developers, understanding and mastering Git and Github commands can significantly improve your workflow. In this blog, we will walk through some of the most useful Git and GitHub commands that every developer should know to streamline their development process.

Git commands:

Most used commands:

  • git init: Before we can do anything git-related, we must initialize a repo first. Use git init to create a new git repository.
  • ⚠️ Warning
    Do not init a repo inside a repo!
  • git init
  • git status: It gives information on the current status of a git repository and its contents.
  • git status
  • git add: We use the git add command to stage changes to be committed.
  • git add file1 file2 # add individual files to stage
    git add . # add all files to stage
  • git commit: We use the git commit command to actually commit changes from the staging area.
  • git commit -m “your message regarding the task you performed”
  • git log: We use the git log command to see the history of commits to understand the project’s evolution or to find specific changes.
  • git log
    git log –oneline
  • git diff: We use the git diff command to view changes between commits, branches, files, our working directory, and more.
  • git diff # compares Staging Area and Working Directory
    git diff –staged # compares staging area and our last commit
    git diff –staged file1 file2 # for specific files
    git diff branch1..branch2 # compares branch1 and branch2
    git diff commit1..commit2 # compares commit1 and commit2
  • git stash: We use the git stash command to save changes that are not yet ready to commit. We can stash changes and then come back to them later.
  • git stash
    git stash pop # to remove the most recently stashed changes in your stash
    git stash apply # to apply whatever is stashed away, without removing it from the stash
    git stash list # to view all the stashes
    git stash apply stash@{1} # to apply the particular stash.
    git stash drop stash@{1} # to remove the particular stash.

Branches:

  • git branch: We use the git branch command to view existing branches.
  • git branch
    git branch <branch-name> # to make a new branch based upon the current HEAD
    git branch -d <branch-name> # to delete the branch
    git branch -D <branch-name> # to force delete the branch
    git branch -m <new-branch-name> # to move/rename the branch 
  • git switch: We use the git switch command to switch between branches.
  • git switch <branch-name>
  • git merge: We use the git merge command to merge changes from a specific branch into the current branch. To merge, follow these basic steps:
  1. Switch to the branch you want to merge the changes into (the receiving branch)
  2. Use the git merge command to merge changes from a specific branch into the current branch.
  • git switch main
    git merge bugfix
  • ⚠️ Warning
    Merge conflicts can occur when Git is unable to automatically reconcile differences between branches. If not resolved correctly, they can result in lost work or unwanted changes. Here are some key points to keep in mind:
  • Understand the Conflict: Open the conflicting files and review the changes made on both branches. Git will insert conflict markers (e.g., <<<<<<<, =======, >>>>>>>) to help you identify the areas of conflict.
  • Manually Resolve Conflicts: Edit the file to select which changes to keep (or create a new solution) and remove the conflict markers. Make sure your resolution makes sense within the context of the project.
  • Test Your Changes: After resolving the conflict, always test your changes to ensure everything works as expected.
  • Stage and Commit: Once you’re confident in your resolution, stage the resolved files and commit them with a clear message explaining how the conflict was resolved.

Undoing Changes:

  • git checkout: We can use git checkout to view a previous commit.
  • ⚠️ Warning
    git checkout results in a detached HEAD state. To return to your original branch, simply switch to your original branch.
  • git checkout <commit-hash>
    git checkout HEAD~1 # checkout commit 1 step below the current HEAD
  • git restore: We can use git restore to restore using HEAD as the default source, but we can change that using the –source option.
  •     git restore <file-name>
        git restore –source HEAD~1 <file-name> # will restore the contents of file to its state from the commit prior to HEAD
        git restore –staged . # will restore all staged files
  • git reset: We can use git reset to reset the repo back to a specific commit. The commits are gone.
  •         git reset <commit-hash>
            git reset –hard <commit-hash> # if we want to undo both the commits AND the actual changes in your files
  • git revert: It creates a brand new commit which reverses/ undos the changes from a commit. If you want to reverse some commits that other people already have on their machines, you should use revert.
  •         git revert <commit-hash>

Github commands:

Github commands:
Github is a hosting platform for git repositories. You can put your own Git repos in Github and access them from anywhere and share them with people around the world. Beyond hosting, Github also provides additional collaboration features that are not native to Git. It helps people share and collaborate on repos.

Most used commands:

- **git clone:** If you want to clone a repo, simply run **git clone <url>**. Git will retrieve all the files associated with the repository and will copy them to your local machine.

```sh
git clone <url>
```

- **git remote:** Before we can push anything up to Github, we need to tell Git about our remote repository on Github. We can use **git remote** to view any existing remotes in repository.

```sh
git remote
OR
git remote -v
git remote add <name> <url> # add a new remote
git remote rename <old> <new>
git remote remove <name>
```

- **git push:** We can use **git push** to push our local work up to Github.

```sh
git push <remote> <branch>
```
- **git fetch:** We can use **git fetch** to fetch branches and history from a specific remote repository. It only updates remote tracking branches.

```sh
git fetch <remote> <branch>
```

- **git pull:** To pull, we specify the particular remote and branch we want to pull. Whatever branch we ren it from is where the changes will be merged into.

```sh
git pull <remote> <branch>
```

> ⚠️ Warning
> **git pull** can results in merge conflicts.

**Semantic Versioning:** It is a standardized versioning system for software releases. It provides a consistent way for developers to give meaning to their software releases. Versions consist of three numbers separated by periods. eg. **2.3.1**
2: Major Release - Significant changes that is no longer backwards compatible. Features maybe removed or changed substantially.
3: Minor Release - New features or functionality have been added, but the project is still backwards compatible.
1: Patch Release - Small bug fixes that do not contain new features or significant changes.

- **git tag:** We can use **git tag** to list all the tags in the current repository.

```sh
git tag
git tag -l <pattern> # search for tag that match a particular pattern
git checkout <tag> # to view the state of a repo at particular tag
git tag <tagname> # to create a lightweight tag
git tag -a <tagname> # to create annotated tag
git tag <tagname> <commit> # to tag older commit
git tag -d <tagname> # to delete tag
```

- **git push tag:** We can use **git push tag** to push tag to Github repository.

```sh
git push <remote> <tagname>
git push --tags # to push many tags to Github at once
```

Leave a Reply

Your email address will not be published. Required fields are marked *