- The
git init
command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new empty repository. - git add
git add ‘.c’ // Add all files with ‘.c’ extension.
git add -all // Add all files to stagged git push -u origin master
Name of remote isorigin
and the default local branch name ismaster
. The-u
tells Git to remember the parameters, so that next time we can simply rungit push
and Git will know what to do.Maps ‘local’ branch to ‘remote’ branch
git push horeku local:remote
Push tag v4 to remote
git push --tags
git pull origin master
Check for changes on repository and pull down any new changes.git pull
does agit fetch
followed by agit merge
.
git pull --rebase
This command will automatically sync latest server changes (with a fetch + merge) and will place your commit at the top in git log.git diff HEAD
Diff of most recent commit can be refered using theHEAD
pointer.git diff
with the--staged
option to see the staged changes.git diff --staged
- Unstage files by using the
git reset
command.git reset octofamily/octodog.txt.
To reset the stagging one level before the commt, use below command i.e. roll back last commit
git reset --soft HEAD^
Undo last commit and all changes
git reset --hard HEAD^
git reset --hard HEAD^
^ // Blow away last two commit - Files can be changed back to how they were at the last commit by using the command:
git checkout -- <target>
Create a new branch DEV and then switch to it.git checkout -- octocat.txt.
git checkout -b DEV
git checkout tagname
, checkout code at commit
Revert the changes only in current working directory, use
git checkout -- .
- Create a branch called
clean_up
,git branch clean_up
Use
git branch -d <branch name>
to delete a branch.git branch -d clean_up
List all the remote branches
git branch -r
M
erge theclean_up
branch into current working branchgit merge clean_up
git rm
command which will not only remove the actual files from disk, but will also stage the removal of the files for us.git rm '*.txt'
- To amend the files to previous commit
git commit –amend git remote -v
, list the remote server.
git remote show origin
, show remote origin branch details
git remote prune origin
, delete remote branch referencegit tag
, lists all the tag
git tag -a v3.3 -m 'New tag
‘, creates new tag v3.3- To find out who changed a file, run
git blame
against a single file, and you get a breakdown of the file, line-by-line, with the change that last affected that line. It also prints out the timestamp and author information as well. - To exclude file from git branch, use
.git/info/exclude
or.gitignore.
git-rebase
reapplies commits on top of another base tip
git checkout
slave
git rebase
master
git rebase -i HEAD~3
// Three commit before the head
git rebase continue
// Start commiting from the last point where it stoppedgit log
shows commits from newest to oldest- Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.
git stash save
git stash save --keep- index
// Stagging area will not be stashed
git stash save --include-untracked
// Save untacked files to stash
git stash list
// Shows list of stashes with names
git stash apply
// Restore last uncommited changes
git stash apply
<Stash name>
git stash drop
// Remove the most recent stash
git stash pop
// Equivalent to ‘apply’ + ‘drop’
git clear
// Remove all the stash - git-clone – Clone a repository into a new directory
git-log
– Show commit logs
One of the more helpful options is -p, which shows the difference introduced in each commit. You can also use -2, which limits the output to only the last two entries.
git log -p -2
To see some abbreviated stats for each commit, you can use the –stat option.
git log --stat
--pretty
option changes the log output to formats other than the default.oneline
option prints each commit on a single line
git log --pretty=oneline