Git is distributed version control system It is very powerful tool and has a lot of features. Also there’re a lot GUI tools for working with git. But in this post I’ll show how to work with git from terminal (console) in Linux or using git-bash in Windows.
The list of commands will be updated periodically.
To add user name and email globally:
git config --global user.name "John Doe" git config --global user.email email@example.com
To specify text editor:
git config --global core.editor vim
To add specific user or/and email for the project (global config will be overridden). From the project folder run the specified above commands without
git config user.name "Holms Doe" git config --global user.email Holms.firstname.lastname@example.org
Rename a branch
Let’s assume we have a branch named
Issue-1 and we want to rename it to
Issue1. To do so just use the following command:
git branch -m Issue1
To delete branch which has already been merged:
git branch -d Issue1
Currently checked out branch could not be deleted.
To delete branch which has NOT been merged yet:
git branch -D Issue1
To delete remote branch:
git push origin --delete Issue5
Undo last commit
In case when we need to modify the last commit, e.g. to add missed file(-s) or to change the commit message we can use the following command:
git commit --amend
If we haven’t made any changes since last commit this command will change only the commit message. To add missed files to the last commit:
- add missed files to the staging
git add missed_file_name.txt
git commit --amendcommand
After modifying the last local commit into such way, we won’t be able to push these changes to the remote branch. Changes will be rejected with the error like:
! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'email@example.com:oneils/git-demo.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Force push to the remote branch
One of the approaches to push rejected commits to remote is to
force push to the remote branch into the following way:
git push origin Issue2 --force
This will push local branch to remote and will override all changes into the remote branch. This command should be used very carefully.