11 unusual git commands that will blow your mind
Everyone has that one colleague that has an ace up his sleeve if we are talking about git commands. Out of nowhere he solves complicated problems with a commit, merge, or pull request. He clearly without any hesitation can blow your mind.
Maybe you are that guy? Maybe you want to be that guy?
Below you can find 11 git commands that are not so obvious and can make your co-workers jealous.
git add -p
Sometimes you don’t want to add the entire file to commit. Maybe you changed only a few values in the dictionary, but some of them are experimental. This command gets you covered.
Adding -p to well-known git add opens new possibilities. Now, all changes need to be reviewed in short hunks of code. You can shorten and enlarge them adding only necessary fragments of code to your commit. Check this out!
$ git add -p Python/parser-squash.py diff --git a/Python/parser-squash.py b/Python/parser-squash.py index 540f8ab..c7591b8 100644 --- a/Python/parser-squash.py +++ b/Python/parser-squash.py @@ -183,6 +183,8 @@ def get_decathlon_data(URL): try: price_box = soup.find("span", id="real_price").get("content").strip() + price_box = soup.find("span", id="real_price").get("content").strip() + except AttributeError: # print("Error Decathlon" + str(error)) price_box = -1 Stage this hunk [y,n,q,a,d,e,?]? ? y - stage this hunk n - do not stage this hunk q - quit; do not stage this hunk or any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk or any of the later hunks in the file e - manually edit the current hunk ? - print help @@ -183,6 +183,8 @@ def get_decathlon_data(URL): try: price_box = soup.find("span", id="real_price").get("content").strip() + price_box = soup.find("span", id="real_price").get("content").strip() + except AttributeError: # print("Error Decathlon" + str(error)) price_box = -1 Stage this hunk [y,n,q,a,d,e,?]?
git rebase -i <commit_id>
One of the git commands that I use the most. Every time when you are working on your feature branch this is the one, the most important one.
When you are in flow, making many changes, many commits…it’s a big mess. Some of the commits need to be squashed, some edited or rewritten. git rebase -i is the answer to your problem.
This command can take few commits from the HEAD of your branch, you can edit commit, and then that taken commits will be appended to the top. All of the options and text showed in the terminal after executing this command is below:
pick 5b5ac43 Removing warnings pick 05fa0ac Fixing warnings, JSP, HTML5 pick 4093736 Still fighting with warnings... # Rebase 118ffd7..4093736 onto 118ffd7 (3 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # # Note that empty commits are commented out
git cherry-pick <commit_id>
That’s the clever one. You developed some changes that need to be committed to two branches -> dev and bugfix. How can I do that? It’s easy.
After committing and pushing changes to your dev branch copy the commit id. Then switch to your bugfix branch. Enter git cherry-pick <commit_id>. Commit will be copied from the dev branch with the same id and changes. No duplicates, everything is done neatly.
git push –force-with-lease
Push and force in one command? Wow, is it safe? That’s the only fully legitimate force. If you are using this command, you are the boss.
So executing git push –force-with-lease add one more step before pushing code to the remote repository. git make sure that code in your local repository is the newest. That ensures remote and local repository is in the same state and your local one has all new commits and changes.
git merge –no-ff -e -m „”
Merge without fast forward, what does it mean? It’s a clever way to merge changes without an auto-generated commit message. Executing this command opens an editor where you can specify a commit message describing the type of changes, Redmine issue number, etc.
git remote update origin –prune
After being for a very long time in one project it’s time to little clean up in your local repository. Executing the above command will delete all branches that are no longer present on the remote repository. Fast and easy, right?
git diff-tree –no-commit-id –name-only -r <commit_id>
This useful command shows changes present in commit with provided commit id. As a result, you will see a list of file paths that were added to this commit.
git checkout @^ — <file_path>
Sometimes we mess up and our changes need to be overwritten. git checkout @^ — <file_path> restores file from the latest commit and overwrite current working copy.
git push origin experimental_branch:remote_branch
Have you ever wanted to push something to an unexisting branch with a different name than your local one? Maybe your local branch name was inappropriate or you were making bugfix from your dev branch?
Been there, done that. With the above command using a colon and providing <local_branch>:<wanted_remote_name> we and our crazy ideas are covered.
git reset @^
I made the commit, but now I don’t want it. Ups, what can I do? I need to remove this commit, but without discarding my changes. git reset @^ and all of your local changes are saved.
git clean -fdx
This powerful and dangerous command will clear your local repository on a few different levels. It will delete all files that are not tracked by git and all ignored files specified in .gitignore.
Maybe your app creating many artifacts as logs, temporary files that are taking up precious disk space. With this command, all of it will be deleted (if it’s untracked or in .gitignore).
git config –global rerere.enabled 1
That’s twelve one…but I will classify it as a configuration switch, not a git command.
If you are sick of resolving the same conflicts again and again this switch is for you. It will enable auto-resolving conflicts that you resolved before. Git will remember how you did it and write it down in his digital notepad.
The same, recurring conflicts after enabling rerere will be resolved automatically without your help. He has notes and will use them!
I hope you didn’t know a few or more of the above git commands. There were gathered and noted in a few years of my professional experience. They helped me many times on a day to day basis. Some of them probably will apply to your daily workflow, am I right?
If you know any of the other useful or clever git commands let me know in the comment section. I’m waiting for your reply.
PS. I’ve got one more useful command :)
git commit –amend
This command modifies the most recent commit. It adds staged changes to the last commit and also provides an opportunity to edit the commit message. Every time you forget about some files or make mistake in the commit message this is the command to go.