Git commands aren’t always intuitive. If they were, we would have these 10 commands at our disposal. They would be super useful for accomplishing common tasks like creating or renaming a git branch, removing files, and undoing changes.
For each git command in our wishlist, we’ll show you the commands that actually exist and you can use to accomplish the same tasks. If you’re still learning Git, this list reads like a tutorial and is worth keeping as a cheatsheet.
The fastest way to create a new branch is to actually do it from the git terminal. This way you don’t have to use GitHub UI, for example, if you use GitHub for version control.
This command actually exists in git, only in a different name – $ git checkout.
One-line command: $ git checkout -b <branch-name> master
-> commit-test git:(master) x git checkout -b feature-branch master
-> commit-test git:(feature-branch) x
Git tip: Just like with commit messages, having a naming convention for git branches is a good best practice to adopt.
You find out you’ve made changes that seemingly conflict with the upstream changes. At this point, you decide to overwrite your changes instead of keeping them, so you do a “$ git pull” and you get this error message:
-> commit-test git:(master) x git pull
Updating db40e41..2958dc6
error: Your local changes to the following files would be overwritten by merge:
README.md
hint: Please, commit your changes before merging.
fatal: Exiting because of unfinished merge.
The problem is that you don’t want to commit your changes, you want to overwrite them!
-> commit-test git:(master) x git stash
Updating db40e41..2958dc6
Saved working directory and index state WIP on master: d8fde76 fix(API): remove ‘test’ end-point
-> commit-test git:(master) x git pull
Auto-merging README.md
Merge made by the ‘recurive’ strategy.
README.md | 1 +
ENDPOINT.js | 3 ++–
2 files changes, 3 insertions(+), 1 deletions(-)
<code><h1></code>
Git tip: If you want to retrieve your changes just do: $ git stash apply
When having unnecessary files and dirs in your own local copy of a repository, and you want to delete those files, in opposed to just ignore them (with .gitignore), you can use git clean to remove all files which are not tracked by git.
-> commit-test git:(master) x git clean -n -d
Would remove dontTrackDir/untracked_file1.py
Would remove untracked_file2.py
-> commit-test git:(master) x git clean -f -d
Removing dontTrackDir/untracked_file1.py
Removing untracked_file2.py
Git tip: Instead of untracking files, a good practice is to prevent those files from being tracked in the first place by using .gitignore file.
When you’re adding files ($ git add) to the working tree, you are adding them to the staging area, meaning you are staging them. If you want Git to stop tracking specific files on the working tree, you need to remove them from your stage files (.git/index).
-> commit-test git:(master) x git rm –cached unstageMe.js
rm unstageMe.js
-> commit-test git:(master) x git reset
Git tip: you can also untrack files which already added to git repository based on .gitignore.
Sometimes you get in a situation (we’ve all been there) where you merged branches and realize you need to undo the merge because you don’t want to release the code you just merged.
-> commit-test git:(master) x git log –oneline
812d761 Merge pull request #524 from datreeio/DAT-1332-resolve-installation-id
b06dee0 feat: added installation event support
8471b2b fix: get organization details from repository object
-> commit-test git:(master) x git revert -m 1 812d761
Revert “Merge pull request #524 from datreeio/DAT-1332-resolve-installation-id”
#
This reverts commit 812d761272756157ffcd5d9aaf3c2950ed1a7113, reversing
changes made to 25071257930db8d4c8221cff776c17e0e0d157ea.
#
# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is up-to-date with ‘origin/master’.
#
# Changes to be committed:
# modified: README.md
-> commit-test git:(master) x git revert -m 1 812d761
[master 75b85db] Revert “Merge pull request #524 from datreeio/DAT-1332-resolve-installation-id”
1 file changed, 1 deletion(-)
-> commit-test git:(master) x git commit -m “revert merge #524”
-> commit-test git:(master) x git push
Git tip: Instead of reverting merge, working with pull requests and setting up or improving your code review process can lower the possibility of a faulty merge.
You wish to delete a file (or files) on remote, maybe because it is deprecated or because this file not supposed to be there in the first place. So, you wonder, what is the protocol to delete files from a remote git repository?
-> commit-test git:(delete-files) x git rm deleteMe.js
rm ‘deleteMe.js’
-> commit-test git:(delete-files) x git commit -m “removing files”
[delete-files 75e998e] removing files
1 file changed, 2 deletions(-)
delete mode 100644 deleteMe.js
-> commit-test git:(delete-files) x git push
Git tip: When a file is removed from Git, it doesn’t mean it is removed from history. The file will keep “living” in the repository history until the file will be completely deleted.
You made a commit but now you regret it. Maybe you committed secrets by accident – not a good idea – or maybe you want to add more tests to your code changes. These are all legit reasons to undo your last commit.
-> commit-test git:(undo-commit) x git commit -m “I will regret this commit”
[undo-commit a7d8ed4] I will regret this commit
1 file changed, 1 insertion(+)
-> commit-test git:(undo-commit) x git reset –soft HEAD^
-> commit-test git:(undo-commit) x git status
On branch undo-commit
Changes to be committed:
(use “git reset HEAD <file>…” to unstage)
#
modified: README.md
Git tip: Git pre-commit hook is a built-in feature that lets you define scripts that will run automatically before each commit. Use it to reduce the need to cancel commits.
When you are working with multiple git branches, it’s important to be able to compare and contrast the differences between two different branches on the same repository. You can do this using the $ git diff command.
-> commit-test git:(diff-me) x git diff master..diff-me
diff –git a/README.md b/README.md
index b74512d..da1e423 100644
— a/README.md
+++ b/README.md
@@ -1,2 +1,3 @@
# commit-test
-Text on “master” branch
+Text on “diff-me” branch
Git tip: diff-so-fancy is a great open source solution to make your diffs human readable.
In the case of a “buggy” release, you probably don’t want someone to accidentally use the release linked to this tag. The best solution is to delete the tag and remove the connection between a release and its co-related tag.
-> commit-test git:(delete-tag) x git push origin :refs/tags/v1.0.0
To github.com:datreeio/commit-test.git
– [deleted] v1.0.0
-> commit-test git:(delete-tag) x git tag -d v1.0.0
Deleted tag ‘v1.0.0’ (was af4d0ea)
Git tip: Not sure when or why to use tags? Read here to learn more (TL;DR: automatic releasing)
As I mentioned, having a branch naming convention a good practice and should be adopted as part of your coding standards, and it is especially useful in supporting automation of git workflows. But what to do when you find out your branch name is not aligned with the convention, after already pushing code to the branch? Don’t worry, you can still rename your branch.
-> commit-test git:(old-name) x git branch -m new-name
-> commit-test git:(new-name) x git push origin :old-name new-name
Total 0 (delta 0), reused 0 (delta 0)
To github.com:datreeio/commit-test.git
– [deleted] old-name
* [new branch] new-name -> new-name
-> commit-test git:(new-name) x git push origin -u new-name
Branch new-name set up to track remote branch new-name from origin.
Everything up-to-date
Git tip: Want to make sure all branch names will always follow your convention? Set a Git-enforced branch naming policy.