5

I have been tired of doing git add & git commit everyday I know in Linux you can just edit your bashrc using 1 line command But since I use windows I am having a hard time creating an alias for

 Fucntion gcd {
    Insert("git commit")
}
Set-Alias gcm gcd

But I get an error

Set-Alias : Alias is not writeable because alias gcm is read-only or constant and cannot be written to.

I also tried without using Insert Keyword even without functions still getting an error

1
  • gcm is already a powershell alias for Get-Command, so you can't override it. Commented Jul 7, 2022 at 13:35

3 Answers 3

3

You don't need to rely on your specific shell for aliases, since Git already supports them out of the box:

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.

Git aliases are defined in the .gitconfig file, either globally or for a particular repository.

In your case, you can create a global alias for git commit by saying:

git config --global alias.gcd commit
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you it really helped my Since I Posh-git is not working I don't know why
And also once i run this command I don't have to run this on every terminal session right? it will be permanent right
Yes—if you set it in Git's global .gitconfig file, it will be available in all sessions for the current user.
2

You can create PowerShell profile that autorun when open PowerShell First open PowerShell and create PowerShell profile (If you don't already have one):

New-Item -Type file -Path $PROFILE -Force

This command will create directory in Documents call WindowsPowerShell inside this directory will create file call Microsoft.PowerShell_profile.ps1

If you want to know the full path of Microsoft.PowerShell_profile.ps1 write this command:

$PROFILE

Open it from PowerShell with this command

powershell_ise.exe $PROFILE

And write all commands that you want to alias it for example:

function gn{git init}
# @args you can pass multi arguments for example
# ga fileName1 fileName2 
function add{git add @args}
function commit { git commit -m @args }
function gac{git add .;git commit -m @args}
function gpm{git push origin master}
function pull{git pull origin master}
function gl{git log}
function glo{git log --oneline}
function gch{git checkout @args}

# @args is optional to add argument
function gb{git branch @args}
function gs{git status}
function gd{git diff}

Save it And close PowerShell, Open it again and call alias commands by function name for example initialize git repository write this command:

gn

To add files write this commands

add fileName1

See GIF image aliases git commands from PowerShell

To show content of alias commands write this command:

Get-Content $PROFILE

Comments

1

Why don't you just try tu use posh-git

posh-git is a PowerShell module that integrates Git and PowerShell by providing Git status summary information that can be displayed in the PowerShell prompt, e.g.:

C:\Users\Keith\GitHub\posh-git [main ≡ +0 ~1 -0 | +0 ~1 -0 !]>

posh-git also provides tab completion support for common git commands, branch names, paths and more. For example, with posh-git, PowerShell can tab complete git commands like checkout by typing git ch and pressing the tab key. That will tab complete to git checkout and if you keep pressing tab, it will cycle through other command matches such as cherry and cherry-pick. You can also tab complete remote names and branch names e.g.: git pull or ma tab completes to git pull origin main.

2 Comments

ok but can posh-git be installed along with oh-my-posh, because I already have oh-my-posh installed on powershell... i am saying can I have both?
Yes I've got both. on my Windows 10, Jus be carrefull how you manage your $Profile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.