Git Documentation
Git Documentation
4. From time to time there are files you don't want git to track.So that you can ignore those files using
.gitignore file. Add selected files into the .gitignore file and add it to the gitconfig file.
To add this to your config, run git config --global core.excludesfile ~/.gitignore_global
* Record changes::
run, git commit ## records a snapshot of local changes.
git add . && git commit -a -m “Your commit message”
## this will add untracked file into git index and will commit all changes into the
intermediary branch origin/master. This will record a change and add it to the git database.This will not
update your code to the origin or git repository. Commit message is mendatory.
* Undo Changes::
git reset ## Undo changes and commit.
git reset HEAD ## undo the last commit and unstage the files
git reset --hard ##undo the last commit, unstage files AND undo any changes in the working
directory
* Override server files with your local::
git checkout [directoryname]
git checkout [filepath]
git pull
## fetch from a remote repo and try to merge into the current branch.This will update your local
files with server files.
*Inspection and Comparison::
git log
## Displays your commit history
git log --oneline ## commit history in one line
git log -p ## commit history with changes.
* Resolve Conflict ::
several options you have to resolve conflict.
# you can checkout previous update of the conflicted file.To do this,
run, git checkout [filename]
# you can reset your code base with server.To do this,
run, git reset --hard
git checkout --theirs [filename] ## get server update of the file
git checkout --ours [filename] ## update server with your changes.
then commit and push.
Git
# Command Description
3 Blame Describes the last modification to every line in the file. Shows
Revision, Author & Time.
4 Checkout This refers to the process in which any given commit is selected from
the repository and the state of the associated file and the directory
tree is recreated in the working directory.
5 Commit This is a single point in Git history which holds the information about
a changeset.
8 Fetch Fetching means to get latest changes in the branch and the
local/remote repos.
9 Fork By Forking the repository, you will be able to add Commits and
create Pull Requests.
1 Merge To bring out the content of another Branch in the current Branch.
3
Git Configuration
# Git Command Description
1 git config –global Set the username to be used for all actions
user.name
2 git config –global Set the email to be used for all the actions.
user.email
3 git config –global alias. Create a shortcut for the Git command.
4 git config –system Set the text editor for all the command actions.
core.editor
5 git config –global –edit Open global configuration file in the text editor for
manual editing.
2 git clone (Repo URL) Clone the repository from GitHub to the
project folder.
3 git clone (Repo URL) (Folder ) Clone the repository into a specific
folder.
8 git remote set-url origin (git URL) Changes the URL of the repository.
1 git add (file name) Add the current changes to the file to staging.
4 git rm (file_name) Removes the file and untracks (stop tracking) it.
7 git checkout <deleted file Recovers the deleted file and prepares it for
name> Commit
9 git ls-files –other –ignored Shows the list of all ignored files.
–exclude-standard
1 git diff –staged Shows file differences between staging and the
1 last file version.
Declare Commits
# Git Command Description
2 git commit -am Adds all changes to staging and commits them with a
“(message)” custom message.
5 git reset –hard Discard all the history and changes back to a given
Commit.
6 git reset –hard Head Discards all local changes in the working directory.
1 git log –follow List the history for the current file.
0 (file_name)
1 git blame (file_name) Shows all changes along with the name of the user.
1
1 git stash apply (stash id) Re-apply a specific stash content by ID.
7
Branching
# Git Command Description
7 git checkout -b Get a remote branch from origin to the local directory.
origin/
9 git merge Merge the current branch into the master (first checkout to
master)
1 git rebase Takes all the changes of the branch and restate on others.
0
1 git rebase Rebase the current branch onto the base. Base can be a Commit
1 ID or a branch name.
1 git pull –rebase Fetches the remote copy of the current branch and rebases it
4 into the local copy.
1 git push –all Push all the local branches to the specified remote repository.
5
Necessary Git Commands
● git submodule update --init --recursive --remote
Syncing a fork
● git remote add upstream https://github.com/otheruser/repo.git
● git remote -v
● git fetch upstream
● git checkout master
● git rebase upstream/master
● git push -f origin master (You only need to use the -f the first time after you've
rebased.)