Working with Git - Git LifeHacks22 Oct 2015
To add all the changes for commit :
git add -A
For commit :
git commit -m "Your commit message"
For commit with all the changes in a single line :
git commit -am "Your commit message"
With this, you don’t need to write
git add -A
To push from the remote :
git push origin branchName
Just for understanding,
originis an alias on your system for a particular remote repository. It’s not actually a property of that repository.
git push origin branchname
you’re saying to push to the origin repository. There’s no requirement to name the remote repository origin: in fact the same repository could have a different alias for another developer.
Remotes are simply an alias that store the url of repositories. You can see what url belongs to each remote by using
git remote -v
To show the changes that you made after the last commit, run :
To show the changes which have been staged, run :
git diff --cachedor
git status -v
To show all the changes from HEAD which have been staged and also which have been not committed yet, run :
git diff HEAD
To show all the previous commits in terminal, run:
To show all the previous commits messages only in terminal , run:
git log --oneline
Display commit with graph tree:
git log --oneline --graph
To show all commits with reference log, run:
To show only few of the previous commits in terminal, run:
git log -<number>i.e.
git log -2for last two commits.
To remove a file from git repo, run :
git rm filename
To move/rename a file, run :
git mv current/file/path new/path/of/file
To clean the current working directory from the last commit,
git clean -f
How to reference a commit to the issue (Using Smart commit) ?
When commit, just include
#commit IssueNowith the commit.
git commit -m "You commit message" #comment Issue-No
git commit -m "Issue-No: You commit message" #comment Issue-No
How to push a new, empty branch in remote?
If your repo is still empty, you might try and create an empty commit, to have something to push:
git commit --allow-empty -m "initial commit"
git push -u origin branchName
How to add a remote branch url?
To add remote branch:
git remote add origin <remote-url>
or, to add another remote branch alongside with the
git remote add app <remote-url>
appcan be any name.
To change the remote url:
git remote set-url <appname> <new-remote-url>
git remote set-url origin https://github.com/laravel/laravel.git
How to Delete a branch ?
git branch -d <branchname>
git branch -D <branchname>
How to create a new branch from the existing code ?
git checkout -b <newBranchName>
How to Delete Last Git Commit ?
If you have committed junk but not pushed,
git reset --soft HEAD~1will do the work.
If you want to get rid of any changes to tracked files in the working tree since the commit before head use
Now if you already pushed and someone pulled which is usually my case, you can’t use git reset. You can however do a git revert,
git revert HEAD
This will create a new commit that reverses everything introduced by the accidental commit.
git revertwill not remove your last commit. It will just create another new commit by fixing the last commit.
To delete the git commit from remote branch completely, you’ve to use
git rebase -i <commit-number>
How to remove a file from the git stage ?
If you do
git add filename, the file will be in stage. So, to remove the file from stage, run:
git reset HEAD filename
And then if you want to remove the changes of a single file, run:
git checkout filename
You’re in the middle of some changes but something comes up that you need to jump over to, like a so-urgent-right-now bugfix, but don’t want to commit or lose your current edits.
git stashis there for you.
git stashto make the working repository clean. And It will then leave you at the state of the last commit.
git stash popor
git stash applyto bring back the stashed code into the working directory.
git stash listto show the lists of stash items.