Git notes

From raju

Tips

frequently used

    git pull --rebase --autostash -v origin
    

git pull autostash

    git pull --rebase --autostash
    

The --autostash works only with --rebase. It was introduced in git 2.6 (2015-09-28).

rebasing using git gui

Q. How to do git pull --rebase using git gui?

A. The trick is to add a custom command in git gui:

 Tools -> Add
 Name: Pull with Rebase
 Command: git pull --rebase origin
 Add globally -> check
 Add

The new command will appear under Tools menu.

git diff N last commits

    git diff HEAD~N
    

Generate diff with no context

git diff -U0

In general, to generate a diff with <n> lines of context, use

git diff -U<n>

or

git diff --unified=<n>

The default is 3 lines of context.

show modified files in the current directory only

    git status .
    

or more generally

    git status /path/to/dir
    

works from git 1.7 onwards. Does not work with git 1.6.

time zone of the committer

    git cat-file -p <commit-id>
    

Will show the entire commit object. It will contain two lines like this:

    author Foo Bar <foo@bar.com> 1398017575 -0400
    committer Foo Bar <foo@bar.com> 1398017575 -0400
    

As you can see, the timezone info follows the Unix-epoch timestamps.

Note that the timezone is something set by the user himself on the client. So it can be easily spoofed by the user if he desires to.


number of commits

To get a commit count for a revision (HEAD, master, a commit hash):

    git rev-list --count <revision>
    

To get the commit count across all branches:

    git rev-list --count --all
    

Ref :- https://stackoverflow.com/questions/677436/how-to-get-the-git-commit-count

multiple repositories

The mr tool in myrepos is very handy when working with multiple git repositories. See http://myrepos.branchable.com/ for more details.

Author of myrepos - Joey Hess. His contact info is at https://joeyh.name/contact/

Diff word by word

To diff word by word

    git diff --color-words
    

To set up an alias for it

    git config --global alias.wdiff "diff --color-words"
    

which will add the following lines to ~/.gitconfig

    [alias]
        wdiff = diff --color-words
    

Once done, "git wdiff" will do the same thing as "git diff --color-words"

delete branches

To delete a remote branch

    git push <remote_name> -d <branch_name>
    

To delete a local branch

    git branch -d <branch_name>
    

Ref:-

Migrate from sourceforge to github

The instructions to move a git repository from sourceforge to github are clearly laid out in https://help.github.com/articles/importing-a-git-repository-using-the-command-line/ .

In particular, to migrate the sampleusage project from sourceforge to github, I did

Login into github website
create a new repository called sampleusage: https://github.com/KamarajuKusumanchi/sampleusage
git clone --bare ssh://kamaraju@git.code.sf.net/p/sampleusage/git sampleusage-git
cd sampleusage-git
git push --mirror https://github.com/KamarajuKusumanchi/sampleusage.git
cd ..
rm -rf sampleusage-git

To get a local copy

git clone git@github.com:KamarajuKusumanchi/sampleusage.git
cd sampleusage
git config user.name "KamarajuKusumanchi"
git config user.email "kamaraju@gmail.com"

Sample work flow once it is all done

git add dummy_file
git commit -m "dummy commit message"
git push -u origin master

After making sure that everything on github is working properly, I submitted a ticket to delete the original sourceforge project.

git and vimdiff

viewing diffs

merge a development branch into master

gitlab

pruning

To list the branches that are merged into master

    git branch --merged master
    

To list the branches that are merged into HEAD (i.e. tip of the current branch)

    git branch --merged
    

To list the branches that were not merged

    git branch --no-merged
    

By default this only applies to local branches. Use -a to show both local and remote branches; -r to show only remote branches.

To delete all local branches that have been merged into master

    git branch --merged | grep -v '\* master' | xargs -n1 git branch -d
    

To delete all local branches that have been merged into the current branch

    git branch --merged | grep -v '^\*' | xargs -n1 git branch -d
    

check commits where a file has changed

tags | show commits on a single file

To list the last 3 commits that changed a file

    git log -n3 --follow -- filename
    

Ref:- https://stackoverflow.com/questions/3701404/list-all-commits-for-a-specific-file

what files changed between two branches

To compare the current branch against master

    $ git diff --name-status master
    

To compare any pair of branches

    $ git diff --name-status firstbranch..secondbranch
    

Change last commit message

    git commit --amend
    

Add a file to the last commit

    git add missed-file.txt
    git commit --amend
    

Remove a file that was accidentally committed

external links

git commands usage

git stash

git stash drop - Remove a single stash.

git stash clear - Remove all the stash entries.

Documentation - https://git-scm.com/docs/git-stash

git cherry-pick

    git cherry-pick <Commit1> <Commit2> <...>
    

Ref:-

git log

Check commits between two branches

    git log branch1..branch2
    

tags | show commits between two branches

branches related

rename a local branch

To rename the current branch

    git branch -m <newname>
    

To rename a branch while in another branch

    git branch -m <oldname> <newname>
    

By default when you push the new branch to the remote, it will add it as a new branch and will not delete the old branch. See https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch if you want to rename the branch on the remote while doing the push.


Configuration related

Configure http proxy

 git config --global http.proxy http://login:password@your.proxy.server:port

git config settings

To check which config comes from which file

    git config --list --show-origin
    

Related commands

    git config --system -l
    

cache username and passwords

for HTTPS authentication

    git config --global credential.helper wincred
    

This works. But how is it different from

    git config --global credential.helper manager
    

?

where to put git ignore file

workflow

bring feature branch up to date with development branch

Situation:

  • User 1 raised a merge request to merge feature1 to development branch
  • Before the merge request is accepted, development branch is updated by User 2

Task:

  • Bring the feature1 branch up to date with the latest changes in development branch

Solution:

    git checkout feature1
    git merge development
    git push
    

Note:- The "git merge development" command works even if you have local changes not committed to feature1.

rebase to another branch

To take BranchB and rebase it on top of BranchA

1) Update BranchA to the latest version if needed

    git checkout BranchA
    git pull --rebase --autostash -v origin
    

2) Merge changes from BranchA into BranchB

    git checkout BranchB
    git rebase --autostash -v BranchA
    

This effectively makes BranchB look as if it was branched off of BranchA.

You can also use 'git merge' instead of rebase but that will create a merge commit.

    git checkout BranchB
    git merge BranchA
    

dummy

git bash

To download git bash, use either https://gitforwindows.org/ or https://git-scm.com/downloads

"Git for Windows" used to be developed using the development environment called "msysGit", but roughly coinciding with Git 2.1, msysGit was superseded by a new development environment.

Ref:- https://github.com/git-for-windows/git/wiki/FAQ -> "What is the relationship between Git for Windows and msysGit?"

detaching HEAD

Detaching HEAD just means attaching it to a commit instead of a branch. Say we have this beforehand:

HEAD -> master -> C1

git checkout C1

and now it is

HEAD -> C1

relative refs

  • Move upwards one commit at a time with ^
  • Move upwards a number of times with ~<num>

branch forcing

    git branch -f master HEAD~3
    

moves (by force) the master branch to three parents behind HEAD.

reversing changes

Two primary ways: git reset, git revert.

Sample command using reset

git reset HEAD~1

Since git reset "rewrites history", you can use it for local branches but not for remote branches.

To reverse changes and share those reversed changes with others, use git revert. For example

git revert HEAD

interactive rebase

All interactive rabse means is using the rebase command with the -i option. Sample command

git rebase -i HEAD~4

Exclude .git directory from grep searches