DEV Community

Temitope Ayodele
Temitope Ayodele

Posted on

Git commands

Here is a list of some useful git commands.

Initialize empty Git repository

git init
Enter fullscreen mode Exit fullscreen mode

Stage a file

git add filepath
Enter fullscreen mode Exit fullscreen mode

Stage all files

git add .
Enter fullscreen mode Exit fullscreen mode

Commit file changes

git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

Check for staged and unstaged files

git status
Enter fullscreen mode Exit fullscreen mode

Push committed changes to master branch

git push
Enter fullscreen mode Exit fullscreen mode

Push committed changes to a particular branch

git push origin branchname
Enter fullscreen mode Exit fullscreen mode

Force push committed change

git push -f
Enter fullscreen mode Exit fullscreen mode

Create a new branch

git checkout -b branchname
Enter fullscreen mode Exit fullscreen mode

Switch to an existing branch

git checkout branchname
Enter fullscreen mode Exit fullscreen mode

Delete a branch locally

(first ensure that you are not on the branch you intend to delete)

to delete branch only if it has already been pushed and merged with the remote branch

git branch -d localBranchName  
Enter fullscreen mode Exit fullscreen mode

delete the branch only if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet

git branch -D localBranchName 
Enter fullscreen mode Exit fullscreen mode

Delete a branch remotely

(first ensure that you are not on the branch you intend to delete)

git push origin --delete remoteBranchName
Enter fullscreen mode Exit fullscreen mode

Update current branch with changes from a remote repository

git pull
Enter fullscreen mode Exit fullscreen mode

Clone a repository

git clone <url>
Enter fullscreen mode Exit fullscreen mode

Manage connections to remote repositories

show URLs of remote repositories

git remote -v
Enter fullscreen mode Exit fullscreen mode

create a new connection to a remote repository

git remote add <shortname> <url>
Enter fullscreen mode Exit fullscreen mode

disconnect the remote from the local repository

git remote remove <name>
Enter fullscreen mode Exit fullscreen mode

rename a remote connection

git remote rename <old-name> <new-name>
Enter fullscreen mode Exit fullscreen mode

Fix last commit message

git commit --amend -m "correct commit message"
Enter fullscreen mode Exit fullscreen mode

Top comments (8)

Collapse
 
fcojavier0099 profile image
FcoJavier0099 • Edited

Hello, when you clone a repository, then the command:

  • git push

Works as spected, becase the remote repository is configured with the master brach by default.
If isn´t the case you would configure it with:

  • git push -u origin master # after this you could use 'git push' like shortcut

In general: git push -u [name of remote] [name of branch]
at this manner when you are in any configured branch, 'git push' makes a push from the current branch to the remote.

Thanks for share

Collapse
 
peter279k profile image
peter279k

I also use following command to delete remote branch:

  • git push origin :remote_branch_name
Collapse
 
temmietope profile image
Temitope Ayodele

Hmm. This git command actually pushes your branch to a remote branch. I guess you forgot to add --delete .

Collapse
 
peter279k profile image
peter279k

No. I've added the : in front of my remote branch.

This feature is available since Git 1.5 version is released :).

Thread Thread
 
temmietope profile image
Temitope Ayodele

Yes.. TIL. Thanks!

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

Best thing you can do for your git game, is using ZSH that has a built-in git plugin. Check this out :) github.com/ohmyzsh/ohmyzsh/blob/ma...

Collapse
 
raphink profile image
Raphaël Pinson • Edited

And my two favorite aliases:

  • commend = commit --amend --no-edit
  • please = push --force-with-lease

I use these all the time in feature branches

Collapse
 
temmietope profile image
Temitope Ayodele

Yh, Nice! 👍🏽