Useful Git Commands: safely cancel commits, add a file, and others

Useful Git Commands: safely cancel commits, add a file, and others

Git is a powerful, albeit simple-looking, a tool that, if used carelessly, can make a big mess. Therefore, before trying to perform various tricks with Git, I recommend that you familiarize yourself with its main commands (init, clone, push, pull, status, log, etc.) and concepts (repository, branch, commit, etc. )

So, if you feel confident working with Git, here are some useful tricks that I know about.

GitHub

Reflog Cancel operations

git-reflog-operations

I remember that at the beginning of my acquaintance with Git, I was afraid to run teams. I thought I would ruin everything. For example, the first use git rebase caused me several exclamations like “Here *****! What have I just done ?! ” due to duplicate or completely disappeared commits. Soon after, I realized that I needed to move on.

Personally, I do not like to use the tool, not knowing what it does to achieve the result. It’s like driving a car without a steering wheel: everything is fine until you reach a turn. And then what? Well, you understand me.

So, in our business it is very important to understand the concepts. I read a lot about Git , went through several training courses, and my vision of the Git world has changed dramatically. Using it, I began to feel much calmer, and more importantly, I knew its true power. And one of the most powerful teams is git reflog.

Using reflog, you can go back in time, undoing almost any activity done in Git. Even if it were rebaseor reset. In addition, it is easy to use. To prove this, I will give a small example.

$ git reflog 1234571 HEAD@{0}: checkout: moving from one-branch to another-branch 1234570 HEAD@{1}: merge origin/feature-branch: Fast-forward 1234569 HEAD@{2}: commit: The commit message goes here. 1234568 HEAD@{3}: reset: moving to 2234567 1234567 HEAD@{4}: rebase finished: returning to refs/heads/one-branch

To change the desired state, you need to start checkoutusing the absolute link:

$ git checkout 1234568

or relative link:

$ git checkout HEAD@{3}

and you will find yourself in a detached HEAD state from which you can create a new branch.

That’s all. All operations after the above state are cancelled.

Revert Discard commit changes

git-revert

You must have encountered a situation where you needed to undo some changes. The team will come to the rescue revert:

$ git revert

What she does? It simply cancels the actions of past commits, creating a new one containing all the canceled changes. Why use it instead of other solutions? This is the only safe way, since it does not change the history of commits. It is commonly used in public branches where changing the story is undesirable.

Let’s take a look at other commands that can undo actions.

Rebase

$ git rebase --interactive

You can execute this command and just remove lines related to unnecessary commits. This is the most obvious and simplest solution, but it has a drawback because of which it cannot be used in public branches: it changes the history of commits. Therefore, after rebase, you may have a problem with push.

Reset

$ git reset --hard

This command deletes all commits above the specified one. It is better than the previous one in that it does not add new information to the story, but nonetheless also changes it.

Checkout

$ git checkout --

This command rolls back the selected file to a state in an earlier version of the specified commit. The canceled changes will be in the working directory, and they can be commited as you like.

The conclusion is that it git revert is the safest operation to undo. In addition, it should be noted that these teams are not interchangeable in 100% of cases.

Log. Better formatting

It’s git log simple with. You enter a command and see the history of commits in chronological order, and each item includes a hash of the commit, author, date, and an attached message.

Let’s take a look at the default log view:

$ git log commit 01ba45f789abcdef9876543210fedcba01234567 Author: First Last email@domain.com Date: Wed Apr 13 17:00:01 2016 +0300

The last commit message goes here.

commit 01cf4a6789abcdef9876543210fedcba01234568 Author: First Last email@domain.com Date: Wed Apr 12 16:24:03 2016 +0300

Another commit message goes here.

commit 01bf456789abcdef9876543210fedcba01234568 Author: First Last email@domain.com Date: Wed Apr 11 15:30:33 2016 +0300

Another commit message goes here.

I think it could be better.

I prefer simple things. Therefore, I use two options:

  • --oneline– shows each commit in one line. In addition, it shows only the commit ID prefix.
  • --decorate – Prints all relative names of the displayed commits.

Here’s what it looks like:

$ git log --oneline --decorate 01ba45f (HEAD -> origin/feature-branch, feature-branch) The last commit message goes here. 01cf4a6 Another commit message goes here. 01bf456 (origin/dev, dev) Another commit message goes here.

The option --oneline reduces the number of occupied lines, and the option --decorate adds relative names (local / remote branches + HEAD). Using these two options, we get a more compact and meaningful git log. Yes, we lose some things, but they are not particularly important. So much better, right?

However, sometimes we need to know the date and author of the commit. In such cases, I use the following:

$ git log --color --pretty=format:"%C(yellow)%h%C(reset) %s%C(bold red)%d%C(reset) %C(green)%ad%C(reset) %C(blue)[%an]%C(reset)" --relative-date --decorate

Wow! Let’s see how it looks:

$ git log --color --pretty=format:"%C(yellow)%h%C(reset) %s%C(bold red)%d%C(reset) %C(green)%ad%C(reset) %C(blue)[%an]%C(reset)" --relative-date --decorate 01ba45f The last commit message goes here. (HEAD -> origin/feature-branch, feature-branch) 3 seconds ago [First Last] 01cf4a6 Another commit message goes here. 1 day ago [First Last] 01bf456 Another commit message goes here. (origin/dev, dev) 2 days ago [First Last]

Still, entering these commands every time is quite inconvenient, right? Let’s create aliases. I believe that using aliases greatly improves productivity. To register them, add the following lines to “~ / .bash_profile” (of course, if you use * nix OS):

alias gl='git log --oneline --decorate' alias gh='git log --color --pretty=format:"%C(yellow)%h%C(reset) %s%C(bold red)%d%C(reset) %C(green)%ad%C(reset) %C(blue)[%an]%C(reset)" --relative-date --decorate'

Thus, we got not only a good looking commit history, but also a simplified way to access it using the gl and commands gh.

Diff Difference between commits

A simple command showing changes between objects in a project git diff. One of the main scenarios for its use is comparing two commits:

$ git diff ..

The other is the output of all changes not made to the index. To do this, do the following:

$ git diff --cached

Branch Check merge branches

I think you already know what the basic command does git branch (it displays a list of local branches without options – approx. Transl.) , So let’s get down to business. From time to time, you will need to remove old or unnecessary branches from your git “tree”.

Using the following:

$ git branch --merged

You will get a list of all branches connected to the current one.

You can and vice versa:

$ git branch --no-merged

So you get a list of all branches that are not connected to the current one.

Thus, you will always find out whether deleting a particular branch is safe.

Checkout Retrieving a file from another branch

You probably had to look for a specific version of the file that was in another, not yet attached branch. Remember the command checkoutabove? It looks like this:

$ git checkout --

The specified file will be cast to the version specified in the given commit. We used to use this to undo file changes. Conveniently, this commit does not have to be in the same branch as our file. Therefore, we can ask any file any version from any commit or branch.

I hope you find something useful here.

How to fix Git Commands mistakes)

The post Useful Git Commands: safely cancel commits, add a file, and others appeared first on Creador.

Did you find this article valuable?

Support Pawan Kumar by becoming a sponsor. Any amount is appreciated!