Skip to content

Instantly share code, notes, and snippets.

@Sandip124
Last active April 21, 2021 03:06
Show Gist options
  • Save Sandip124/394fb40cbf3bb9beeb6ce57672896382 to your computer and use it in GitHub Desktop.
Save Sandip124/394fb40cbf3bb9beeb6ce57672896382 to your computer and use it in GitHub Desktop.
Git Snippets

Stage All Delete files

git rm $(git ls-files --deleted) 
OR
git add  `git ls-files --deleted`

Unstage File in git

git reset -- fileName
Example: git reset -- index.html

Show Files Hash (Tree View)

git ls-tree -l head

Show Contents of file using git Commit Hash

git show commit-hash
Example: git show 48ede0020211825ce6dcc25b79700c62476c41fd

Show diff of staged file

git diff --staged

Undo Commit and Go back To Previous version

git reset --hard  commit-hash
Example: git reset --hard 48ede0020211825ce6dcc25b79700c62476c41fd

1. Rename your local branch.
If you are on the branch you want to rename:

git branch -m new-name

If you are on a different branch:

git branch -m old-name new-name

2. Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:

git push origin -u new-name

Show git logs in graph

git log --oneline --graph

Create Branch in Tag

git checkout tags/v1.0 -b v1.0-branch

Create SSH Key

ssh-keygen -t ed25519 -C "youremail@mail.com"

Create Merge Request

git push -o merge_request.create

Run Git Commands for multiple directories

for i in */.git; do ( echo $i; cd $i/..; git pull; ); done

How can I undo the last commit?

First, before we bring the big guns in, let's make sure you really need them. Because in case you just want to edit your last commit, you can simply use Git's amend feature. It allows you to correct the last commit's message as well as add more changes to it. If that's what you want to do, read more about amend.

Undoing the Last Commit

However, of course, there a tons of situations where you really want to undo that last commit. E.g. because you'd like to restructure it extensively - or even discard it altogether!

In these cases, the "reset" command is your best friend:

$ git reset --soft HEAD~1

Reset will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone.

Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.

If you don't want to keep these changes, simply use the --hard flag. Be sure to only do this when you're sure you don't need these changes anymore.

$ git reset --hard HEAD~1

List all Git alias

git config --list

Set Global Git Alias

git config --global alias.co checkout

Git Aliases

Before we finish this chapter on basic Git, there’s just one little tip that can make your Git experience simpler, easier, and more familiar: aliases. We won’t refer to them or assume you’ve used them later in the book, but you should probably know how to use them.

Git doesn’t automatically infer your command if you type it in partially. If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using git config. Here are a couple of examples you may want to set up:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

This means that, for example, instead of typing git commit, you just need to type git ci. As you go on using Git, you’ll probably use other commands frequently as well; don’t hesitate to create new aliases.

This technique can also be very useful in creating commands that you think should exist. For example, to correct the usability problem you encountered with unstaging a file, you can add your own unstage alias to Git:

$ git config --global alias.unstage 'reset HEAD --'

This makes the following two commands equivalent:

$ git unstage fileA
$ git reset HEAD -- fileA

This seems a bit clearer. It’s also common to add a last command, like this:

$ git config --global alias.last 'log -1 HEAD'

This way, you can see the last commit easily:

$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel <dreamer3@example.com>
Date:   Tue Aug 26 19:48:51 2008 +0800

    Test for current head

    Signed-off-by: Scott Chacon <schacon@example.com>

As you can tell, Git simply replaces the new command with whatever you alias it for. However, maybe you want to run an external command, rather than a Git subcommand. In that case, you start the command with a ! character. This is useful if you write your own tools that work with a Git repository. We can demonstrate by aliasing git visual to run gitk:

$ git config --global alias.visual '!gitk'

Show all remote and local branches

git branch -av

--OR--

git branch -a

Change Remote of any git repo

git remote set-url origin https://yourLogin@github.com/yourLogin/yourRepoName.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment