Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Wednesday, January 11, 2023

How to Use .gitconfig's includeIf - Git 2.13 conditional config on windows - git set username and email for separate project - Is it possible to have different Git configuration for different projects?

.gitconfig is usually stored in the user.home directory.
I use a different identity to work on projects for Company A and something else for Company B (primarily the name / email). How can I have two different Git configurations so that my check-ins don't go with the name / email?
As of git version 2.13, git supports conditional configuration includes. In this example we clone Company A's repos in global directory anywhere in my computer, and Company B's repos in some other specific folder like D:/Mine/Projects. For company B, we will keep all our repo's inside D:/Mine/Projects
File Location For Windows : "C:\Users\${USER_NAME}\.gitconfig", follow below steps to make it work:
  • Open .gifconfig in a editor
  • Paste below contents as example:
    [include]
      path = .gituser-default
    [includeIf "gitdir:D:/Mine/Projects/"]
      path = .gituser-company-b	
    [credential]
    	helper = wincred
    
  • Now edit .gituser-default with below contents - this configuration will use for anywhere in your computer
    [user]
    	name = Pritom Kumar
    	email = pritomkucse@gmail.com
    
  • Now edit .gituser-company-b with below contents - this configuration will use for Company B
    [user]
    	name = Pritom Kumar
    	email = pritomku.test@gmail.com
    
  • Now clone an repo into the /D/Mine/Projects directory and navigate to the project directory
  • Now run command git config user.email to check the configuration, you will see as below:

How to fix - git@github.com permission denied (publickey). fatal could not read from remote repository and Support for password authentication was removed. Please use a personal access token instead - Git: How to solve Permission denied (publickey) error when using Git - Fix "Permission denied (publickey)" error when pushing with Git

So, the problem arised when I tried to clone a Git repository from github using below command:
git clone git@github.com:user-name/repo-name.git

But I failed with below error:

Cloning into 'repo-name'...
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Possible solution is Create SSH key pair
The process is as below:
1. Open Git Bash and run command ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
2. Accepts the default file location (using Enter key)
3. Set SSH key passphrases or hit Enter button twice - will generate private and public key.
4. Your identification has been saved in /c/Users/user-name/.ssh/id_rsa... if you don't change location.
5. Copy the SSH key to your clipboard using command clip < /c/Users/HP/.ssh/id_rsa.pub.
6. Time to set key to github account, go to https://github.com/settings/profile
7. Then go to SSH and GPG keys and click on New SSH key button and paste SSH key to the big text box (Make sure you remove your email address from SSH key at end):
8. You are done from github end.
9. Now you can clone your GitHub repo and it will not complain about git@github.com permission denied (publickey). fatal could not read from remote repository
10. It's time to try again to clone repo using Git Bash.
11. Execute command git clone git@github.com:user-name/repo-name.git and let's see what happen
12. So it's done, repo clonned successfully:
Benefits of this approach
  • You do not need to supply your GitHub Username and password
  • More secured way to connect with GitHub
Drawback
  • The process of generating SSH keys is little troublesome
  • If you switch your laptop/desktop then you need re-generate the SSH keys again and perform the same steps onto your new laptop/desktop

Thursday, April 30, 2020

How to save Username and Password in GIT Bash

First Run Below Command From GIT Bash (Remove --global if you want to save credentials for current project only)


git config --global credential.helper store
Now run below command and provide username/password (which will be remembered)
git pull
Provide a username and password and those details will then be remembered later. The credentials are stored in a file on the disk, with the disk permissions of "just user readable/writable" but still in plaintext.

Wednesday, November 27, 2019

Git Bash | Changing a remote's URL | How to Change a Git Remote's URL | How Save Username/Password/Credentials For Git Repository in Git Bash

Got to the directory where the repository is located using Git Bash:

cd /path/to/repository
Run git remote to list the existing remotes and see their names and URLs:

git remote -v

The output will look something like this:

origin https://pritomkucse@bitbucket.org/pritomkucse/demo_project.git (fetch)
origin https://pritomkucse@bitbucket.org/pritomkucse/demo_project.git (push)
For example, to change the URL of the origin to some other repo you would type:

git remote set-url origin https://pritomkucse:your_password@bitbucket.org/pritomkucse/demo_project_2.git
Verify that the remote URL has changed using the same command git remote -v

Sunday, February 11, 2018

git: Your branch and 'origin/master' have diverged - how to throw away local commits

I have the following message in git:

# Your branch and 'origin/master' have diverged,
# and have 3 and 8 different commits each, respectively.
# (use "git pull" to merge the remote branch into yours)
I would like to throw away the 3 local commits, and pull the 8 remote commits at origin/master.

(Merging is going to be too difficult, I'd rather make the 3 local commits again once master is up to date.)
To erase your latest local commit use the following:

git fetch origin
git reset --hard origin/master

You will get following message:
HEAD is now at 76b2d560 .......................

Friday, January 26, 2018

How to revert a merge commit that's already pushed to remote branch | Revert a merge after being pushed | Undo a git merge that has been pushed to the server | Git HowTo: revert a commit already pushed to a remote repository

You could follow these steps to revert the incorrect commit(s) or to reset your remote branch back to correct HEAD/state
1. git checkout development
2. Use git log -5 to show latest 5 commits, which will result as below:
$ git log -5
commit 6e10182080307215e70d5d843d29ddfb302d776f (HEAD -> development, origin/development, origin/HEAD)
Author: Pritom K Mondal <pritomkucse@gmail.com>
Date:   Thu Jan 25 20:53:19 2018 +0600

    Test

commit f262e20f51171d2d24d9f15cd2abcf73a641ac43
Author: Pritom K Mondal <pritomkucse@gmail.com>
Date:   Thu Jan 25 20:52:34 2018 +0600

    Test

commit f808058d419e9d71b9f4bc27bf272241c0bf9971
Author: Pritom K Mondal Kumar <pritomkucse@gmail.com>
Date:   Thu Jan 25 14:45:54 2018 +0000

    Repository created
3. git reset f262e20f51171d2d24d9f15cd2abcf73a641ac43 (i.e. your commit number where you want to go)
4. run the git status to show all the changes that were part of the wrong commit, if you don't want to see untracked files run git status --untracked-files=no or as shortcut run git status -uno
5. simply run git reset --hard to revert all those changes
7. force-push your local branch to remote and notice that your commit history is clean as it was before it got polluted.
git push -f origin development
8. But by any chance if you need to abort reset you need to revert above reset option as git reset 'HEAD@{1}' as short answer.
9. Long answer is type git reflog and go to desired location.
$ git reflog
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to HEAD
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{1}: reset: moving to HEAD
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{2}: reset: moving to 1123175d5b42a8f459aff1cd7bb59082e0087775
b1110db HEAD@{3}: merge remotes/origin/branch1: Merge made by the 'recursive' strategy.
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{4}: commit: Test from master
d81337f HEAD@{5}: commit: Test from master

Saturday, December 2, 2017

GIT | View Changed Details For Files By Commits | View List Of Files Changed During Different Commits

This below command will show changes made during last 3 commits for specific file defined
git diff HEAD~3..HEAD -- project/location/file.name
This command will list files changes made during last 3 commits
git diff --name-status HEAD~3..HEAD
This command will list files changes made during last 3 commits with changes
git diff HEAD~3..HEAD
Below command will show history of commits:
git log -3
git log -2
Below command will show difference between two commits:
git diff old_commit new_commit
git diff 5300b....4ea31a 754e8d....c33
git diff --name-status 5300b....4ea31a 754e8d....c33

Thursday, June 15, 2017

GIT: Create and push a new local branch to a remote Git repository and tracking information | GIT Delete Local Branch | GIT Delete Remote Branch

What we have to do first need to create a local branch from current working branch using below command:

git checkout -b my_branch

And then have to execute below command to push newly created branch to git repository:

git push -u origin my_branch

And then you need to set your upstream branch to pull changes:

git branch --set-upstream-to=origin/my_branch

Below command is to delete a local branch:

git branch -D my_branch

Below command is to delete a remote branch:

git push origin --delete my_branch

Saturday, May 27, 2017

Git Merge Working Branch With Remove Branch

At first you nee to update your local branch using below command. It may better your have no local changes.

git pull

Then execute following command to update current branch with specific remote branch:

git merge remotes/origin/remote_branch

Sometimes it may prompt the following dialog:



Then you have to follow the below steps:

1. Press `i` to enable edit mode
2. Use down cursor go to the last line and introduce new line
3. Put some comments
4. Press `Esc` then `:wq!` and hit Enter key
5. You are done with merge
6. And finally execute "git push" to push changes

Sometime it may told you that some conflict occurred with the list of files, then you need to merge them before do merge.

After conflict occurred executed command "git merge --abort" to abort current merge request and then merge conflict before try again merge.

The simplest solution is fetch conflict file from the branch from where you want to merge using below command:

git checkout origin/branch_name app/directory/file.name

and push all files and then try again, i think it helps.

Sometimes it may need to stash your local changes before execute "git merge ..." command:

git stash

And after execute "git merge ..." execute below command to unstash changes:

git stash apply stash@{0}

To reset your current working branch:

git reset --hard (will move to previous commit)

And finally if you want to clear all untracked files execute:

git clean -fd


Friday, April 7, 2017

Git Bash Inside PhpStorm

It would be very helpful if we can use Git Bash inside PhpStorm itself.
The fact is, though, that it's a terminal client and that's how PhpStorm thinks of it. To implement it, you have to go to:

>> File -> Settings -> Tools -> Terminal

Once you're there, here are the steps necessary to enable Git Bash in the Terminal dialogue. 
Now you can browse git.sh file from your git installation location. 
That would be like below path:
C:\Program Files\Git\bin\sh.exe and it may differ in your machine

And then need to bound the path by double quotation like:
"C:\Program Files\Git\bin\sh.exe" 

And add the following line after that "-login -i"

And the final output be like:
"C:\Program Files\Git\bin\sh.exe" -login -i

Your set-up is completed, now press Alt+F12 to see git window inside PhpStorm.

And your are done.

Tuesday, November 15, 2016

GIT: Updates were rejected because a pushed branch tip is behind its remote

git push -f

The -f makes this a "forced push" and overwrites the branch on the working branch on the server. That would be gone wrong when you are working with other team members. But you can force push when you sure that your local state is correct then it should be fine. You risk losing commit history if that is not the case.

GIT: How to show changed made on files on branch by commit

It will display file changes between two commits (file names only):
git diff OLDER_COMMIT RECENT_COMMIT --name-status

It will display file changes between two commits:
git diff OLDER_COMMIT RECENT_COMMIT

It will display file changes between local changes and specific commit:
git diff COMMIT_NUMBER

Monday, November 14, 2016

Git, see list of latest commits

To see list of commits:
git log

List of commits with file name & status:
git log --name-status

List of previous n commits
git log --name-status -n 2

List of commits by author name:
git log --name-status --author=Pritom (Case sensitive)

List of commits search by file name:
git log --name-status -n 2 --author=pritom | grep "Search"

GIT: How to show the changes on specific commit

Type the following to git bash to show all changes:
git show COMMIT_NUMBER

To show difference between a specific file type:
git show COMMIT_NUMBER file_location/file_name

Type following to show file list changed on the commit:
git show COMMIT_NUMBER --name-status

Will output following:
M       location/file_name (Modified)

A       location/file_name (Added New)

Git list all commits for a specific file

Type to git bash:
git log --follow filename

and to quit from list type:
q

Friday, November 11, 2016

Git - fatal: Unable to create '/path/my_project/.git/index.lock': File exists

Execute the command below:

rm -f ./.git/index.lock

If you have no other git processes running (which is the normal case), go ahead and delete that file.

Untrack files from git using git bash

This will start ignoring the changes to the file:
git update-index --assume-unchanged path/to/file 

When you want to start keeping track again: 
git update-index --no-assume-unchanged path/to/file 

Now list files that are ignored: 
git ls-files -v 

And with a search options: 
git ls-files -v | grep "file_name_part"

Wednesday, November 9, 2016

Git commit single or multiple files

git commit project/config/app.php project/env -m "Test"

Marked yellow are two files, you add more files one by one.

Marked orange is message for this commit.

Git clone with user name and password

1. Use: 
git clone https://user:password@server.org/username/repo.git

2. Or (It will prompt you for your password):
git clone https://user@server.org/username/repo.git

Git: How to checkout a file from another git branch


1. First go to your project location using git bash

2. Use below command to go to project location:

3. Command:: cd /c/User/pritom/Git_Project/my_project

3. Now go to the branch from where you want to update file to 
current branch

3.1 You have to update the branch from which you want to 
checkout file

4. Command:: git checkout another_branch

5. Command:: git pull

6. Command:: git diff --name-only another_branch~1 
(It will list files changed last 1 commit)

7. Now go to working branch using:

8. Command:: git checkout current_branch

9. First pull to update current branch using:

10. Command:: git pull 

11. Now checkout files from another_branch using:

12. Command:: git checkout another_branch app/data/some_data.name

13. Now you file updated with another_branch

14. Command:: git status (It will show files changes)