Git hacks you should know about

Ankur Biswas on March 07, 2019

In this post, we're going discuss some very useful tricks on Git which literally can save your ass if you screwed up things on Git. Now, without ... [Read Full]
markdown guide
 

This is such a time saver...

Switching back and forth between two branches:

git checkout -
 

That's going to be very useful for me. Thanks for sharing this one.

 

- in general is wonderful. I sometimes forget it exists for git, cd, and others. It makes jumping around directories/branches so much faster.

 

Hey, thanks for sharing.

Couple of notes:

git branch -m relaese release

is a bit confusing i'd suggest changing to:

Rename branch locally

git branch -m old_branch new_branch

Delete the old remote branch

git push origin :old_branch

Push the new branch, set local branch to track the new remote

git push --set-upstream origin new_branch
 

Thanks for describing in detailed way 😁

 

I always forgot the command to push a delete branch on remote.

 
 

I'd like to add git add -A

This will add all of your change files to the commit. Otherwise, great list!

 
 

I like to use from time to time, to see where am I fetching/pushing.
Made few times a error where I would copy some dir to another dir and .git would get overwritten :)

git remote -v
(⎈ |k8s-b99)➜  mydir git:(kubernetes) git remote -v
origin  https://github.com/myuser/brooklyn99.git (fetch)
origin  https://github.com/myuser/brooklyn69.git (push)
 

I also like to use this to see where all my branches are tracking from:

git branch -vv

It tells me which branches currently exist on my machine and which remotes and branches they are tracking. This is useful if you work with a team and you add their remotes to pull their branches down. It can get confusing to keep track of which branches are pointing where so you know where code is going to land when you run git push or where the changes are coming from if you do a git pull.

>git branch -vv
  master                                            d5e2de1 [puppet/master] Merge pull request #297 from clairecadman/sqlserver_doc_edits
  release                                           bddc857 [puppet/release: behind 2] Merge pull request #295 from dylanratcliffe/MODULES-8685-removing-instances-doesnt-work
* tickets/master/MODULES-8610-invoke-agent-job-task bddc857 [bill/tickets/master/MODULES-8610-invoke-agent-job-task] Merge pull request #295 from dylanratcliffe/MODULES-8685-removing-instances-doesnt-work
  tickets/release/MODULES-8721-release-prep-2.4.0   8454979 [bill/tickets/release/MODULES-8721-release-prep-2.4.0] Release Prep 2.4.0

The branch with the asterisk is the one I currently have checked out.

 
 
 

What is very important, that these commands rewrite history. And when you rewrite history, you actually create new commits and the old ones stay intact. So if you've pushed already, then after using any of these commands you won't be able to push without --force. And once something is shared it cannot be unshared. Be careful and know what these commands lead to.

I wouldn't call these hacks or tricks, though. Most of these is a regular workflow stuff.

 
 
 

git bisect but that needs a whole article in itself. If you haven't heard of it, it's a way of quickly pinpointing at which point in history a commit was made, eg. you just noticed a bug, and want to find which code change caused it.

 
 

git commit --amend will add anything in the staging area (or index; the thing files get to when using git add), and let you change the commit message. If you only want to change the commit message leaving the index alone, you can use git commit --amend --only.

When you want to remove a file from the latest commit, after using git reset --soft and co. you can use git commit -C ORIG_HEAD so you don't have to enter the same commit message.

 
 

An easier way to remove a file that has been added to the last commit by mistake:

git rm --cached <wrong_file>
git commit --amend

This doesn't delete the file, though. Dropping '--cached' would delete it.

 
 

git commit --amend can be use to add new modifications to the previous commit.

You can edit a file and mark it to commit

    git add .

and then

    git commit --amend

to insert the modifications in the previous commit.

 
git log --oneline --graph --decorate

Did you use?

 

I use aliases for that :

git config --global alias.lga "log --graph --abbrev-commit --pretty=format:'%C(red)%h%Creset -%C(yellow)%d%Creset %s %C(green)(%cr) %C(bold blue)<%an>%Creset' --all"
git config --global alias.lg "log --graph --abbrev-commit --pretty=format:'%C(red)%h%Creset -%C(yellow)%d%Creset %s %C(green)(%cr) %C(bold blue)<%an>%Creset'"

git lga show the whole tree, git lg show just your branch. Can't work without it, I need to know the state of the commit tree in shell very often.

 

I don't use it in a daily basis. But it's very useful.

 
 
 

There is a short hand for renaming the current branch which isn't mentioned:

git branch -m new_branch

 
 

Renaming branches >>>

Thanks for the tips - def will save me some time in the future!!

 
 
 

I knew git amend but git reflog that's a cool one ... and git checkout - is a really handy one

 
 

This is a great list. I have used amend for the last commit more times than I might be prepared to admit :)

 
 
 
 
 
 

The non-intuitive nature of Git seems to be an opportunity for writing a full-featured GUI on top of the arcane command line wizardry required to do most anything other than a commit.

 

Hi Ankur, I wrote a continue tutorial for more advanced topic in a language accessible for beginners, but I can't get featured and reach people to read it. Can you feature it in your one?

 
code of conduct - report abuse