Skip to content

Instantly share code, notes, and snippets.

@SaravananRajaraman
Forked from stuart11n/gist:9628955
Last active September 20, 2018 12:54
Show Gist options
  • Select an option

  • Save SaravananRajaraman/b3a5a93d44760f11ef478980fd007e2d to your computer and use it in GitHub Desktop.

Select an option

Save SaravananRajaraman/b3a5a93d44760f11ef478980fd007e2d to your computer and use it in GitHub Desktop.
Git

Git tips

Git Basics

First things first, Try this git basics challenges @ try.github.io

--

One time setup of the Username and Email ID

	$ git config --global user.name "John Doe"

	$ git config --global user.email johndoe@example.com

Change commit author at once after you commited

	$ git commit --amend --author="Author Name <email@address.com>"

Rename you git branch on both local and remote

	$ git branch -m old_branch new_branch         //# Rename branch locally    
	$ git push origin :old_branch                 //# Delete the old branch    
	$ git push --set-upstream origin new_branch   //# Push the new branch, set local branch to track the new remote 

Git Rebase VS Git Pull

If you follow the instructions in the Git message and pull:

	$ git pull origin master

Git actually does the following two commands:

	$ git fetch origin master
	$ git merge origin/master

And the following happens to the commit tree:

	C1 - C2 - C3 - C4 - C5 - C6 - C7 (master)
                  \               \
                   C5' - C6' - C7' - C8 (origin/master)

On the other hand, if you are to rebase:

	$ git fetch origin master
	$ git rebase origin

The following happens to the commit tree:

	 C1 - C2 - C3 - C4 - C5' - C6' - C7' - C5 - C6 - C7 (master)
                                  |
                                  (origin/master)

Squashing commits with rebase

Suppose you have four different commit, but you want to convert into single commit, Following example will helps to do.

You commits,

  1. List item
#1 01d1124  Adding license

#2 6340aaa Moving license into its own file
 
#3 ebfd367 Jekyll has become self-aware.
 
#4 30e0ccb Changed the tagline in the binary, too.
$ git rebase -i HEAD~4

p 01d1124 Adding license
s 6340aaa Moving license into its own file
s ebfd367 Jekyll has become self-aware.
s 30e0ccb Changed the tagline in the binary, too.

# Rebase 60709da..30e0ccb onto 60709da
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

This may prompt you to do an confirmation for commit, once you successfully rebase you can push with -f(force).


This gist was got inspired by https://gist.github.com/lttlrck/9628955 | http://mattsnider.com/git-rebase-versus-git-pull/ | http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment