Most common awesome git commands

Git is an awesome version control system for software development that you definitely should know. Developed by Linux creator Linus Torvalds, using git in your work flow makes writing code or even managing configuration files or bash scripts really easy. You can for instance make changes and quickly roll back to previous versions when you need to. But the power of git is collaborative development. You can easily write code or documentation with your other friends making coding more social and fun.

Most developers like me have come to know about git via github, the popular social coding website. You can follow me on github although these days I mostly use bitbucket for private repositories and a self-hosted gitlab server on our local network for local company repos.

So lets dive into the most common git commands you'll most likely use;

create a new repository

mkdir my-site && cd my-site
echo "# my site" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:oquidave/my-site.git
git push -u origin master

push to an existing repository

git remote add origin git@github.com:oquidave/my-site.git
git push -u origin master

committing changes

git commit -am 'my first commit'
Sometimes you want to commit without a message, you can do;
git commit -a --allow-empty-message -m ''
To discard changes
git checkout file-to-discard-changes
delete files from local repo
git rm /path/to/file
remove the whole git repo
$ rm -rf .git

working with repo/file changes

show differences between two branches
git diff --name-status dev..stagging
show changes made to a file
git diff
remove the file from my staged and commit directories
git rm -r --cached some-directory
delete remote branch
git push origin --delete

working with branches

See the list of all local branches
git branch
Switch to existing local branch
git checkout branchname
Checkout current branch into a new branch, named new-branch-name
git checkout -b new-branch-name
merge branch called branchname
git merge --no-ff branchname
Hard branch delete, will not complain about nothing. Like rm -rf in bash
git branch -D branchname
Change the current branch to master. Sometime your current branch has all the latest changes you want, but you don't want to do a git merge maybe becauase you can run into merge conflicts. Simply;
git checkout betterbranch
git merge -s ours master
git checkout master
git merge better
branch

working with remote repos

show remote repo url we are pushing to
git remote -v
edit remote repo url
git remote set-url --delete origin https://github.com/oquidave/my-site
git remote set-url --add origin https://github.com/oquidave/mysite
Push current branch to remote branch (usually with the same name)
git push
You can specify remote branch like
git push origin branchname
pull remote repo
git pull

logs

show git commit history and logs
git log --graph --oneline --decorate --all

ignoring some files or directories

Sometimes you want to ignore some files such as log files, temporary files or some auto-generated stuff.
Create .gitignore file
Enter touch .gitignore to create a .gitignore file.
Commit it
git commit .gitignore -am 'ignore file'
run the following which will remove the files from the index
git rm --cached file-to-ignore
For directories, use the -r option
git rm -r --cached folder-to-ignore

Summary of commands
#show branches
    git branch
#show unstagged changes
    git status
#clone a repo
    git clone https://bitbucket.org/username/repo-name.git
#show repos you are pushing to 
    git remote -v 
#commit changes with a comment 
    git commit -am "modified readme" 
#push changes to remote repo with verbosity
    git push -v
#add a file to index for committing 
    git add test-file
#create a branch, switching to it 
    git checkout -b dev 
#create a new branch dev
    git branch dev
#switch to dev branch
    git checkout dev
#create a branch and switch to it
    git branch dev && git checkout dev
#soft delete a branch
    git checkout -b dev
#hard-delete a branch
    git checkout -D dev
#compare branches master and dev
    git diff --name-status master..dev
#compare test-file in master and dev branch
    git diff master..dev -- test-file
#merge changes from dev branch
    git merge --no-ff dev  
#see changes made to test-file
    git diff test-file
#see git logs of commits
    git log 
    git log --graph --oneline --decorate --all
#push to blueprints remote repo with verbosity
    git push -u origin blueprints -v 
#add to index files created, modified and deleted
    git add -A 
#merge only database/seeds/test-dir/test-seed from the dev branch
    git checkout dev database/seeds/test-dir/test-seed
#list files/dirs that you don't want to index
    .gitignore
#re-index files 
    rm -r --cached . && git add . && git commit -am 're-indexed files'
#roll back to specific commit 
  git reset --hard commit-id (from git log)
#View a specific Git commit
  Git show commit-id (from git log)
#Clone specific remote branch instead of master
git clone -b <branch> <remote_repo>

#discard unstaged changes
git clean -df  
git checkout -- .  

Final word. Git is like the linux terminal. There's something to discover each time. So keep discovering. My gotcha is that I usually write dig instead of git because I use DNS services quite a lot :)

David Okwii

David Okwii is a Ugandan-based Technology writer and System's Engineer.

Kampala Uganda http://www.davidokwii.com

Subscribe to oquidave@geek:~ #

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!