GIT USEFUL COMMANDS
GIT USEFUL COMMANDS
Notes taken while doing some research:
GIT GENERAL CONFIGURATION SETTINGS:
git config --global user.name "Name Lastname"
git config --global user.mail "namelastname@gmail.com"
Visualize changes in configuration:
git config --global --list
cat ~/.gitconfig
GIT STARTING COMMANDS:
Fresh start:
/*creates empty folder for project only with .git directory inside*/
git init git-demo
Start in existing project:
/*execute in root folder*/
git init
EXPLANATION ABOUT GIT, ADDS, COMMIT AND PUSH:
As long as we are just writing and saving code, we are working in our local files.
When we execute git status and files appear in red within "untracked files" section, it means that these files are not being tracked by git. We are just working locally with them, but not yet in the staging area.
By the time we execute a "git add", we are adding the new files and changes into the staging area
When files appear in green it means that the are now in the staging area waiting to be part of a new commit.
ONE SHORTCUT TO ADD AND COMMIT IN JUST ONE STEP:
/*only if changes are done in a file already tracked by git*/
git commit -a
git commit -am "description" // add m for adding a comment
SHOW LIST OF COMMITS:
git log
OR
git log --oneline --graph --decorate --color
DISCARD FILE FROM STAGING AREA:
If after an "add" we realize that one of the files should be omitted in the future commits, we can remove it from the staging are with:
git reset HEAD <README.md>
DISCARD CHANGES IN MY WORKING DIRECTORY:
The file returns to the way it was in the version last committed
git checkout -- <file>
CHANGES ON FILES ALREADY COMMITTED:
/*send order to remove file from last commit but does not execute the deletion, this order remains in the staging area*/
git rm <file>
/*so in order to make this change permanent, and affect our git repository, we need to commit the deletion*/
git commit -m "removing log-file"
OR
/*Alternatively we can remove the file outside of git's knowledge by using unix native command "rm". It makes a working directory change*/
rm <file>
/*So git detects that the file has been deleted but it has not being staged for the commit yet*/
git add -u
/*-u recursively update git's staging area for deleted/moved files outside of git. We have just added our deletion to our staging area, now we can commit*/
MOVE A FILE USING GIT:
git mv <file> <new location>
git commit -m "Moving index.html file to web folder"
IGNORING FILES TO GIT
.gitignore file records files we do not wan to include in our commit, we can create it and add patterns.
vim .gitignore
We should add 1 pattern per line
A pattern can a be either a directory or a file
SETTING UP SSH AUTHENTICATION:
/*create a directory, underneath the user main folder, where to save ssh keys*/
mkdir .ssh
/*create a public key*/
ssh-keygen -t rsa -C "rcnavarrop@gmail.com"
/*additionally, create a passphrase*/
/*copy the public key and paste it in github.com > settings > SSH keys */
cat id_rsa.pub
/*connect terminal to the list of known hosts*/
ssh -T git@github.com
GIT REMOTES AND GITHUB
/* we are going to set a remote reference */
/* the following command add a remote repository. Origin is the name and then the remote url is specified */
git remote add origin https://github.com/xxx-x/git-demo.git
/* check if we have now a remote repository associated with origin */
git remote -v
/*now we are going to push our changes up to populate the remote repository */
/*-u stablish an upstream link between our remote repository and our local repository with our master branch */
/*origin reference to remote repository and master to our branch*/
git push -u origin master
SETTING UP SSH AUTHENTICATION:
/*create a directory, underneath the user main folder, where to save ssh keys*/
mkdir .ssh
/*create a public key*/
ssh-keygen -t rsa -C "rcnavarrop@gmail.com"
/*additionally, create a passphrase*/
/*copy the public key and paste it in github.com > settings > SSH keys */
cat id_rsa.pub
/*connect terminal to the list of known hosts*/
ssh -T git@github.com
GIT REMOTES AND GITHUB
/* we are going to set a remote reference */
/* the following command add a remote repository. Origin is the name and then the remote url is specified */
git remote add origin https://github.com/xxx-x/git-demo.git
/* check if we have now a remote repository associated with origin */
git remote -v
/*now we are going to push our changes up to populate the remote repository */
/*-u stablish an upstream link between our remote repository and our local repository with our master branch */
/*origin reference to remote repository and master to our branch*/
git push -u origin master
Comments
Post a Comment