Author's image
Have you ever wondered what you could do to look like a professional in front of your manager when using Git? In this post, we will learn about 10 advanced Git techniques and shortcuts that will make you more efficient when controlling versions, maintaining, and sharing code.
You probably already know the basics of commit, push, pull, and branching with Git. But there are many lesser-known commands and functions that can improve your skills. After reading this, you'll have some clever tricks up your sleeve to impress your coworkers with your Git mastery.
You've added and committed files this way several times, but what if I told you it can be done on one line with the `-am` flag?
$ git add .
$ git commit -m "new project"
Try this instead, 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(-)
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 this, you must add the file changes and then commit using the `–amend` flag. To preserve 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(-)
If you want to push a local commit and override remote history without having to troubleshoot, you can use the `–force` flag. However, it is important to note that using the force indicator is not recommended and should only be used when you are absolutely sure of what you are doing. Please note 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
To undo a commit in Git, you can use the `rollback` 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'll use the `log` with the `–oneline` flag to see the commit history in a shorter form.
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.
Do you want to increase your productivity on GitHub? With GitHub Code Spaces, you can now edit and run your code right in your browser.
To access this feature, simply navigate to your favorite repository, press the period (“.”) key on your keyboard and you will be redirected to the VSCode user interface.
Author's image
You can make changes to the code and push them to your remote repository. However, if you want to run code in the terminal, you must run Codespace in the cloud. The free version offers a great option to run your Python code in your browser. Isn't that amazing? I just found out today.
Author's image
When you work 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 easily save your work using the `stash` command. When you use `stash`, you save your current progress without adding it to the staging area or confirming it. This allows you to save your progress and restore it whenever you need.
We will save our current progress by providing a name and saving 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 recover it.
stash@artificial intelligence: On master: new-idea
Our stash of “new ideas” is saved at index 0. To retrieve it, use this command:
On branch master
Your branch is up to date with 'origin/master'.
You have the option to change the name of your default branch to something more appropriate. In this case, we will change the name from “master” to “principal”.
You can verify the changes using the following command:
On branch main
Your branch is up to date with 'origin/master'.
If you want to see a detailed history of all commits made to the current repository, you can use the `git log` command. However, the result may be difficult to read. To make it more readable, you can use the `graph`, `decorate` and `oneline` flags. This will show changes made to multiple branches and how they are merged.
$ git log --graph --decorate --oneline
On several occasions I changed branches and forgot the name of the previous branch. Consequently, I had to use the `git branch -a` command to see the list of branch names. However, there is an easier way to revert to the original branch by using the dash “-” after the `git checkout` command.
First we will create the new “neo” Git branch.
We will switch to the “neo” branch.
To go back to the original branch, we will use the following command:
Switched to branch 'main'
We have learned how to override the remote repository. Let's learn how to override local repository using remote repository.
We will use the `fetch` command to get the latest changes from the remote repository.
Then, we will use the 'reset' command with the 'hard' flag to override any local changes with the remote version. 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 deleted using the following command:
I was inspired to write this article after seeing a Youtube video by fire ship. I admire the creator for his ability to explain complex topics in a simple way. By following his method, I learned a lot about Git features.
In this article, we cover advanced Git techniques that are crucial for data scientists and software engineers working on a collaborative data project. Knowing these techniques can help you avoid accidents and resolve problems much faster.
I hope you found this blog useful. Let me know if you want to read more posts with byte-sized information on the most used tools in the world of data science.
Abid Ali Awan (@1abidaliawan) is a certified professional data scientist who loves building machine learning models. Currently, he focuses on content creation and writing technical blogs on data science and machine learning technologies. Abid has a Master's degree in technology Management and a Bachelor's degree in Telecommunications Engineering. His vision is to build an artificial intelligence product using a graph neural network for students struggling with mental illness.