Towardsdatascience by Jeff Hale, Heart of the Machine by Shemeng Li and Chita.

This article discusses various Git commands that developers, data scientists, or product managers should know about. You’ll learn how to check, delete, and clean up with Git. We’ll also show you how to evade Vim to save time with Bash aliases and Git editor configurations.

If you’re not familiar with basic Git commands, check out my previous article on Git workflow before reading this article.

Address: towardsdatascience.com/learn-enoug…

This article describes 10 common commands and some of their parameters. Each command can be linked to the Atlassian Bitbucket guide for that command.

check

Learn how to check for changes first.

  • Git diff — View changes to all local files. To change only one file, you can add the file name after the command.

  • Git log — View all commit history. Also available for files with git log -p my_file, enter q to exit.

  • Git Blame My File — Find out who made what changes to my_file and when.

  • Git reflog — Displays the change log for the local code base HEAD. This command is great for finding lost work.

Checking with Git is not a hassle. Git, by contrast, has a lot of deleting and undoing commits and file changes.

undo

You can use git reset, Git checkout, and Git Revert to undo changes you make in your code base, and these commands can be a little confusing.

Git Reset and Git Checkout can be used for both commits and changes to individual files, while Git Revert can only be used at the commit level. If you only need to process local commits that have not been incorporated into collaborative remote work, you can use any of these three commands. If you are working collaboratively and need to undo a commit in a remote branch, use Git Revert.



Each of these commands takes multiple arguments. Here are some common uses:

  • Git reset — hard HEAD — Unsets changes made to staging and non-staging areas since the most recent commit.

Specify a different commit than HEAD to undo changes made since this commit. — Hard refers to undoing changes to staging and non-staging areas.

Make sure that you do not undo the commit of the remote branch on which the partner depends.

  • Git checkout my commit — Undo non-staging changes from my_commit.

HEAD is often used in my_commit to undo changes made to the local working directory since the last commit.

Checkout is best used for locally only undo. It does not break the commit history of the remote branch on which your partner depends.

If you use checkout for a branch instead of a commit, HEAD will switch to the specified branch and update to the matching working directory. This is a more common use of the checkout command.

  • Git revert My COMMIT — Undo the changes in my_COMMIT. When you undo a change with Revert, it produces a new commit.

For collaborative projects, Revert is safe because it does not overwrite history that other user branches might depend on.

Revert is very safe.

Sometimes you just want to delete untraced files in your local directory. For example, maybe you’re running code that creates many different types of files in the repository that you don’t need. You can clear them with one click!

  • Git clean -n — Deletes untraced files in your local working directory.

  • – n indicates a trial run. Nothing is deleted during the trial run.

  • -f deletes a file.

  • -d deletes untraced directories.

Untraced files in.gitignore are not deleted by default, but this behavior can be changed.



Now that you know the commands used to undo Git, let’s take a look at the two commands used to arrange files in order.

finishing

  • Git commit — amend — adds the changes to the staging area to the last commit.

If there is nothing in the staging area, you can use this command to edit the latest submission information. Use this command only if the commit has not been consolidated into the remote main branch!

  • Git Push my Remote — tags — sends all local tags to a remote repository. Applicable to version changes.

If you use Python and want to change the package you build, Bump2Version automatically creates labels for you. Once you push the tag out, you can use it in your own version. Here is my guide to completing your first OSS Python package. Stick with me to make sure you don’t miss the version control part!

  • Guide: https://towardsdatascience.com/build-your-first-open-source-python-project-53471c9942a7?source=friends_link&sk=576540dbd 90cf2ee72a3a0e0bfa72ffb

Help! I’m trapped in Vim and I can’t get out!

With Git, you may occasionally find yourself stuck in a Vim session. For example, if you try to submit without submitting information, Vim will automatically turn it on. If you don’t know Vim, it’s pretty bad — over 4,000 votes were cast under this Stack Overflow answer on how to get out of this mess.

The following four steps are used to escape from Vim in saved files:

1. Press I to enter the insert mode

2. Enter your submission information in the first line

3. Press the exit key — Esc

4. Enter x. Don’t forget the colon.

Change the default editor

To avoid using Vim, you can change the default editor in Git. Here is the command to change the default value of Atom, the editor I use:

git config --global core.editor "atom --wait"Copy the code

Assuming you have Atom installed, you can now solve Git problems in Atom.

Shortcuts to create Git commands

Add the following alias command to.bash_profile to add shortcuts to the Git command:

alias gs='git status 'alias ga='git add 'alias gaa='git add -A 'alias gb='git branch 'alias gc='git commit 'alias gcm='git commit -m 'alias go='git checkout 'Copy the code

You can adjust the above commands to add shortcuts to any Git command.

If you don’t have a.bash_profile, you can create one on macOS with the following command:

touch ~/.bash_profileCopy the code

To open this file:

open ~/.bash_profileCopy the code

More about. The following information, please refer to: stackoverflow.com/a/30462883/…

Now you type gs on the terminal and git status has the same effect. Note that in the terminal you can enter a shortcut key followed by other tags.

You can also use Git aliases, but this requires you to type Git before you enter the shortcut command. Who needs those extra buttons?

conclusion

This article covers many of the key Git commands and shows you how to configure your environment, which will save you some time. Now that you have Git and GitHub in place, are you ready to take the next step?

Want a deeper understanding, please refer to the Bitbucket Git tutorial: www.atlassian.com/git/tutoria…

Here is a story about Git branch of interactive guide: learngitbranching.js.org/

Work with others, learn from each other, and answer each other’s questions.

The original link: towardsdatascience.com/10-git-comm…