10 Advanced Git Techniques
Improve your version control skills to resolve issues and maintain a clean Git repository.
Image from Author
Ever wonder what you could do to look like a pro in front of your manager when using Git? In this post, we'll learn about 10 advanced Git techniques and shortcuts that will make you more efficient at versioning, maintaining, and sharing code.
You likely already know the basics of committing, pushing, pulling, and branching with Git. But there are many lesser-known commands and features that can level up your skills. After reading this, you'll have a few slick tricks up your sleeve to impress coworkers with your Git proficiency.
1. Add & Commit
You have added and committed the files this way multiple times, but what if I tell you it can be done in one line with the `-am` flag?
$ git add .
$ git commit -m "new project"
Try this instead, and it will add the file changes and create the commit using the message.
$ git commit -am "new project"
[master 17d7675] new project
4 files changed, 2 insertions(+), 1 deletion(-)
2. Amend
You can rename your current commit message using the `--amend` flag and write the new message. This will help you with accidental messages.
$ git commit --amend -m "Love"
[master 7b7f891] Love
Date: Mon Jan 22 17:57:58 2024 +0500
4 files changed, 2 insertions(+), 1 deletion(-)
You can include additional changes to the current commit before pushing it to the remote repository. To do so, you need to add the file changes and then commit using the `--amend` flag. To retain the previous commit message, simply use the `--no-edit` flag.
$ git add .
$ git commit --amend --no-edit
[master f425059] Love
Date: Mon Jan 22 17:57:58 2024 +0500
6 files changed, 2 insertions(+), 34 deletions(-)
3. Override Remote History
If you want to push a local commit and override the remote history without dealing with resolving issues, you can use the `--force` flag. However, it's important to note that using the force flag is not recommended and should only be used when you're absolutely sure of what you're doing. Keep in mind that using the force flag will rewrite the remote history.
$ git push origin master --force
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 357 bytes | 357.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/kingabzpro/VSCode-DataCamp.git
8f184d5..f425059 master -> master
4. Revert
To undo a commit in Git, you can use the `revert` command. However, this command does not remove any commits. Instead, it creates a new commit that undoes the changes made by the original commit.
We will use the `log` with `--oneline` flag to view commit history in a shorter form.
$ git log --oneline
f425059 (HEAD -> master, origin/master) Love
8f184d5 first commit
To revert to a previous commit, we use the `git revert` command followed by the commit ID. This creates a new commit with the changes from the previous commit.
$ git revert 8f184d5
5. Codespace
Do you want to increase your productivity on GitHub? With GitHub Code Spaces, you can now edit and run your code directly in your browser.
To access this feature, simply navigate to your favorite repository, press the period key (".") on your keyboard, and it will redirect you to the VSCode UI.
Image from Author
You can make changes to the code and push them to your remote repository. However, if you want to run the code in the terminal, you need to run the Codespace in the cloud. The free version offers an excellent option for running your Python code in your browser. Isn't that awesome? I just found out about it today.
Image from Author
6. Stash
When working on a project, you can add files to a staging area and then commit them to save your current progress. However, there is another way to save your work easily using the `stash` command. When you use `stash`, you save your current progress without adding it to the staging area or committing it. This allows you to save your progress and restore it whenever you need to.
We will save our current progress by providing a name and stashing it.
$ git stash save new-idea
Saved working directory and index state On master: new-idea
You can view your stash list and note the corresponding index to retrieve it.
$ git stash list
stash@{0}: On master: new-idea
Our stash of "new ideas" is saved at index 0. To retrieve it, use this command:
$ git stash apply 0
On branch master
Your branch is up to date with 'origin/master'.
7. Rename Branch
You have the option to rename your default branch name to something more appropriate. In this case, we will rename "master" to "main".
$ git branch -M main
You can verify the changes using the following command:
$ git status
On branch main
Your branch is up to date with 'origin/master'.
8. Decorate the Logs
If you want to view a detailed history of all the commits made in the current repository, you can use the `git log` command. However, the output can be difficult to read. To make it more readable, you can use the `graph`, `decorate`, and `oneline` flags. This will display the changes made in multiple branches and how they merge.
$ git log --graph --decorate --oneline
9. Switch Back Branch
On several occasions, I have switched to a new branch and forgotten the name of the previous branch. Consequently, I had to use the `git branch -a` command to view the list of branch names. However, there is a simpler way to return to the original branch by using the dash “-” after the `git checkout` command.
We will first create the new Git branch “neo”.
$ git branch neo
We will switch to the “neo” branch.
$ git checkout neo
Switched to branch 'neo'
To go back to the original branch, we will use the following command:
$ git checkout -
Switched to branch 'main'
10. Copy Remote Changes
We have learned about overriding the remote repository. Let's learn how to override the local repository using the remote repository.
We will use the `fetch` command to get the latest changes from the remote repository.
$ git fetch origin
Then, we will use the 'reset' command with the 'hard' flag to override any local changes with the remote version. Please note that this will permanently discard any local changes.
$ git reset --hard origin/master
HEAD is now at f425059 Love
If there are still untracked files, they can be removed using the following command:
$ git clean -df
Conclusion
I was inspired to write this article after watching a YouTube video by Fireship. I admire the creator for his ability to explain complex topics in a simple way. By following his method, I have learned a lot about Git features.
In this article, we have covered advanced Git techniques that are crucial for data scientists and software engineers who are working on a collaborative data project. Knowing these techniques can help you avoid accidents and resolve issues much faster.
I hope you found this blog useful. Please let me know if you would like to read more posts with byte-sized information about commonly used tools in the data science world.
Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who loves building machine learning models. Currently, he is focusing on content creation and writing technical blogs on machine learning and data science technologies. Abid holds a Master's degree in technology management and a bachelor's degree in telecommunication engineering. His vision is to build an AI product using a graph neural network for students struggling with mental illness.