Home

Git Handy Commands

January 28, 2019

Everything you need to know about Git

Setting up a repo

$ git init
will initialize git repository in current directory
(or)
$ git clone ssh://vinay@example.com/path/to/my-project.git
$ cd my-project

$ git remote -v
to know about remote repos

$ git remote add origin https://github.com/vinkrish/repo-name.git
to push local repo to GitHub server...takes remote name and repository URL

$ git remote rm upstream (or) origin
to remove upstream/origin

$ git remote show origin

Checkout Remote Branch

$ git branch
lists local branches: master and new clean_up branch

$ git branch --all
lists are remote branches as well

$ git branch branchName
new branch created

$ git checkout branchName
will start using new branch branchName

$ git checkout -b branchName
Create new branch locally and switch to new branch

$ git fetch
Will load all the remote branches locally so now you can simply access any remote branch via it’s name.
$ git checkout branchName

If you have local branch that’s also called branchName?
$ git fetch origin
$ git checkout –-track origin/branchName

If you have multiple remotes? specify the remote name
$ git checkout -b branchName upstream/branchName

Making changes

$ git status
to see current state of project

$ git add file.txt
to start tracking changes made to file.txt...added to staging area

$ git commit -m "just added the file to repository"
moving file from staging area to repository

$ git add -all
(or) $ git add .
adds all new or modified files

$ git add *.txt
adding files of same type in current directory

$ git add docs/*.txt
adds all txt files in docs directory

$ git add docs/
adds all files in docs directory

$ git add "*.txt"
adds all txt files in the whole project

$ git add -u
stage tracked files, including deleting the previously tracked files

$ git add -u .
stages tracked files in current path (including sub folders)

$ git add -u :/
stages whole working tree

$ git log
to browse what files are changed

$ git log -1
to check last commit

$ git log --graph --decorate --oneline

$ git branch -m <new_name>
to rename local branch

Fork the repo first
$ git remote rename origin upstream
$ git remote add origin URL_of_forked_repo
while working on forked repo change origin to point to forked repo

$ git remote prune origin
removes all stale branches

$ git push -u <name> <branch>
to push to remotes, is usually master

$ git push -u origin master
name of our remote is origin and default local branch name is master.
-u tells Git to remember the parameters and next time don't have to specify origin and branch

$ git pull origin master
pull down any new changes

$ git diff
shows unstaged differences since last commit

$ git diff HEAD
diff of our most recent commit, HEAD refers to last commit.

$ git diff --staged
$ git diff --cached
changes that are staged, to show diff after adding files.

$ git diff HEAD HEAD~2
$ git diff branch_name master
changes in branch compared to master

$ git revert commit_hash
creates a new commit that undoes the changes from a previous commit (adds new history to the project)

Reset moves the current branch and optionally copies data from repository to other areas
$ git reset HEAD directory/file.txt
$ git reset directory/file.txt
unstage files..removing

$ git reset --soft HEAD^
reset into staging, move to commit before HEAD

$ git reset --hard HEAD^
$ git reset --hard <previous commit hash>
undo last commit and all changes

$ git push -f origin main(or master)
To make changes to remote

$ git reset --hard commit_ref => copies data into Working Area & Index
$ git reset --mixed commit_name => copies data into Index, default behavior
$ git reset --soft commit => doesn't touch any

$ git commit -a -m "add changes from all tracked files"
If you want to skip the staging area and not add new files

$ git add forgottenFile.txt
$ git commit --amend
to update last commit without creating new commit, git does that for you $ git commit --amend -m "modify last commit with this new commit message"
add to the last commit

$ git checkout -- file.txt
changes back to how they were at the last commit for file.txt

$ git rm '*.txt'
in clean_up branch we can remove file or use wildcard

$ git commit -m "Remove all the files"
after removing we need to commit changes

$ git branch -d branch_name
delete local branch

$ git branch -D branch_name
delete local branch forcefully (irrespective of its merged status)

$ git push -d origin <branch-name>
$ git push origin --delete <branch_name> delete remote branch

$ git checkout master
after finishing with branch, switch back to master..copy or merge from clean_up branch to master branch

$ git checkout master
$ git merge clean_up
:wq
hit Enter to write(save) & quit

$ git branch -d clean_up
delete clean_up branch after merging changes to master

$ git checkout commit
instead of checkout branch, you can directly checkout commit which will result your HEAD in detached state

$ git push
to push files from now on

Different ways to pull remote branch to local

$ git pull origin branch_name
This is the default option where the changes from the remote branch are merged into local branch.

$ git pull --rebase origin branch_name
This option replays your local commits on top of the commits from the remote branch, creating a linear history.

$ git pull --ff-only origin branch_name
This option will only pull the changes if your local branch can be fast-forwarded to the remote branch. It fails if there are divergent changes.

Merge master branch into your branch

$ git merge master or $ git merge origin/master
this is fast-forward.

When there is conflict fix it manually, then add and commit without commit message.
$ git add conflicted_file
$ git commit

Abort merging:
$ git merge --abort

Merge local branch to local master branch

$ git rebase master or git rebase origin/master

When there are conflicts.
$ git add <resolved-files>
$ git rebase --continue

Abort rebasing:
$ git rebase --abort

Clean-up commit history

$ git rebase -i <base-commit>
$ git rebase -i HEAD~3

pick 2h33d4 First commit message
pick 1ha9se Second commit message
pick 3ada9s Third commit message

# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

mv old_name new_name
$ git add new_name
$ git add old_name
This removes old_name file from index and it does that by renaming coz content of both files are same.

$ git mv new_name old_name
built in way of renaming

Please move or remove them before you can merge:
$ git clean -d -f

Unrelated history issues
$ git pull origin master --allow-unrelated-histories
$ git push origin master

cherry-pick: Apply the changes introduced by some existing commits
$ git cherry-pick commit_hash

If new commit message is needed:
$ git cherry-pick --edit commit_hash

Hotfix Release

$ git tag
$ git checkout tags/release-tag-name hotfix-branch-name
$ git cherry-pick <commit-hash>

Resolve any conflicts
$ git add <file1> <file2>
$ git cherry-pick --continue
$ git push origin hotfix-branch-name

Tagging:

$ git tag
v0.0.1 v0.0.2
$ git checkout v0.0.1
$ git tag -a v0.0.3 -m "version 0.0.3"
$ git push --tags

$ git log --oneline --graph
$ git log --until=1.minute.ago
$ git log --since=1.day.ago
$ git log --since=1.hour.ago
$ git log --since=1.month.ago --until=2.weeks.ago
$ git log --since=2000-01-01 --until=2012-12-21

$ git reflog HEAD
$ git reflog refs/heads/master

$ git show commit_log
$ git show HEAD
$ git show HEAD^
$ git show HEAD^^
$ git show HEAD~2
$ git show HEAD~2^2 => 2nd parent of 2nd commit before HEAD

$ git blame file => which commit changed what
$ git blame index.html --date short

.git/info/exclude
experiments/
will exclude this folder from git

.gitignore
logs/*.log

Delete all branchs locally, can replace feature with whatever you want:

git branch -d `git branch | grep -E 'bugfix.*'`
git branch -d `git branch | grep -E 'feature.*'`

To delete a regex match of branches remotely:

git push origin --delete `git branch -r | grep -Eo 'bugfix.*'`
git push origin --delete `git branch -r | grep -Eo 'feature.*'`

To remove file from repo
$ git rm file.txt
-f force removal
--cached only removes from Index but keeps it in Working Area

$ git rm --cached file.txt
untracking files but not deleted from file system

git ls-files --deleted -z | xargs -0 git rm

git config -l

$ git config credential.helper store
$ git push https://github.com/repo.git

Username for 'https://github.com':
Password for 'https://USERNAME@github.com':

git config --global user.name "Vinay Krishna"
git config --global user.email "vinaykrishna1989@gmail.com"
git config --global color.ui true
git config --global pull.rebase true (If you prefer to always use rebase)
git config --list
git config --list --global
git config --list --system
git config --list --local
git config --edit --global
git config --edit --system
git config --edit --local

heroku create
git push heroku master

git reset should be used before push

$ git help log
$ git log --patch
$ git log --grep context_text --oneline
$ git help grep
$ git log -3 --oneline

Commands that move branches:
commit, merge, rebase, pull, reset
checkout changes repo coz it moves head pointer & copies data into Working Area and the Index

For large file:
brew install git-lfs
git lfs install
git lfs track "*.csv"
git add .gitattributes

git add file.csv
git commit -m "Add data file"
git push origin master

git help

usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
view raw git-commands.md hosted with ❤ by GitHub


Hi, I'm Vinay - a Programmer who "Read-Evaluate-Loop". You can find me on GitHub LinkedIn Twitter


© 2024, Built with Gatsby