Git Pro Tips: Advanced Commands and Techniques
Tags: git, webdev
Published: 2022-12-26 15:29:49

Git is a powerful version control system that allows developers to track and manage changes to their codebase. In this blog post, we'll go over some advanced Git commands and tips and tricks that can help you boost your Git skills and improve your workflow.


1- `git stash`: To save your changes and temporarily remove them from your working directory, you can use the git stash command. For example:

```javascript
$ git stash

Saved working directory and index state WIP on master: 1234567 Initial commit
HEAD is now at 1234567 Initial commit
```

To apply your stashed changes later, you can use the `git stash apply` command. For example:

```javascript
$ git stash apply

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
```

2- `git cherry-pick`: To apply a specific commit from one branch to another, you can use the git cherry-pick command followed by the commit hash. For example:
 

```
$ git cherry-pick 1234567
[master 1234567] Initial commit
```

3- `git bisect`: To perform a binary search through your commit history to find the commit that introduced a bug, you can use the `git bisect` command. To start the binary search, you can use the `git bisect start` command, followed by the `git bisect bad` command to mark the current commit as bad and the `git bisect good` command to mark a known good commit. Git will then automatically checkout a commit in the middle of the range and you can use the `git bisect good` or `git bisect bad` commands to mark the commit as good or bad. Git will continue this process until it finds the commit that introduced the bug. For example:

```
$ git bisect start
$ git bisect bad
$ git bisect good 1234567
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[1234568] Add feature X
$ git bisect bad
Bisecting: 1 revision left to test after this (roughly 1 step)
[1234569] Fix bug Y
$ git bisect good
1234569 is the first bad commit
```

4- `git reset`: To undo commits and revert your repository to a previous state, you can use the `git reset` command followed by the commit hash. It is important to use this command with caution, as it can permanently delete commits. For example:

```
$ git reset 1234567
Unstaged changes after reset:
M       README.md

```

5- Use aliases: To create a Git `alias`, you can use the `git config` command followed by the alias option and the alias name and command. For example, to create an alias for `git commit` that automatically adds the `-a` flag to commit all changes, you can use the following command:

```
$ git config --global alias.ca 'commit -a'
```

To use the alias, you can simply use the alias name instead of the full command. For example:

```
$ git ca -m "Commit all changes"
```

6- Use a graphical interface: There are many different graphical Git clients available, such as GitHub Desktop, GitKraken, and SourceTree. These tools provide a graphical interface for visualizing and managing your repository and commits. For example, GitHub Desktop allows you to view and manage your repository, create and switch branches, and commit and push changes directly


By learning and using these advanced Git commands and tips and tricks, you can improve your productivity and streamline your workflow. Happy coding!