Install git in ubuntu
sudo apt-get install git
Check git installation
git --version
Configure preferences
git config --global user.name "alejandrobernalcollazos" git config --global user.email alejandro@alejandrobernalcollazos.com git config --global core.editor vim git config --global merge.tool kdiff3
In these configurations
- Editor : vim
- Merge tool : kdiff3
- must be installed with this command
- sudo apt-get install kdiff3
- must be installed with this command
To check the preferences
git config --list
Disable push to multiple branches
git config --global push.default simple git config --global rerere.enabled true
Initialize a repository
git init
Add new files to the staging area
git add .
Check status
git status
Short version of git status
git status -s
Commit changes to the staging area
git commit -m "First commit"
Change last commit message
git commit --amend
Ignore files
There are several ways to ignore files, one of them is with the .gitignore file, but there are others and there is also a level of precedence, so for more information please check this reference
Creating a .gitignore file
This method is used to share the "patterns" which should be version-controlled and distributed to other repositories via clone
1. Create a file within the repository's folder called .gitignore (it could be one or more files, however a single file at the root of the repository is good enough)
user@repositoryRoot $ touch .gitignore
2. Add the lines (could be patterns) that specify which sort of files you want to ignore, as a sample lets ignore all the files with .bin extension
*.bin
Define Patterns at $GIT_DIR/info/exclude file
This method is used to exclude auxiliary files that live inside the repository but are specific to one user’s workflow
1. Edit the file $GIT_DIR/info/exclude, where $GIT_DIR is the path to the git repository
vim $GIT_DIR/info/exclude
2. Add the lines (could be patterns) that specify which sort of files you want to ignore, as a sample lets ignore all the files with .bin extension
# no .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore all files in the build/ directory build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .pdf files in the doc/ directory doc/**/*.pdf
Commit with -a
The -a option will allow us to commit all staged and unstaged changes in our local repository
git commit -a -m "Message"
Checkout
This command will pull a given resource (file or files) from the HEAD of the repository
Differences
To see the difference of the changes at the working directory compared to the local repository
git diff
To see the difference of the changes at the staging aread compared to the local repository (staged or cached)
git diff --staged
also
git diff --cached
To see the difference between remote repository branch and local branch
Suppose we have
- Remote repo name in localhost : origin
- Remote repo branch : branch1
- Local branch : branch1
git diff origin/branch1 ^branch1
Check status of your local git repository
git status
Skipping the staging area
The -a parameter allow us to skip the staging area, making a commit from the working area to the local repository directly
git commit -a -m "Some commit"
Note : Only already tracked files will be commited, if there are file untracket those need to be added first to the staging area and commited to earn the tracked status
Removing Files
Once the next command is executed the "file to be removed" will be gone and no longer tracked
git rm <file to be remoted>
If there are changes in the staging area, in order to remove a file we must
git rm -f <file to be removed>
To remove a file (someFile.txt) but keep it in the working area
git rm --cached someFile.txt
To remove a set of things (This will remove all .log files under log folder)
git rm log/\*.log
Rename a file
git mv file_current_name file_new_name
Logs
git log
Show difference at each commit, this will print all commit
git log -p
Show difference at the last two commits
git log -p -2
Show difference summarizing the changes
git log --stat
Show the logs in one line
git log --pretty=oneline
Configure an upstream reference to a remote repository
HTTP method
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
SSH method
git remote add upstream github@github.com:ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Remove remote repo
git remote rm upstream
Patch
Make a patch file from a commit
git format-patch -1 <sha>
Apply a patch file
git apply file.patch
Disallow push to a remote repository
Suppose we have a remote repository reference called upstream
git remote set-url --push upstream disallowed
Stash
Save the currently unstaged and uncommited changes to a temporary directory
git stash
Pop the last stashed change into the currenty directory
git stash pop
Clear the stash queue
git stash clear
Checkout remote branches
Sample
git checkout -b test origin/test
Clean the working directory
Revert changes
git checkout .
Revert a change already commited
git revert ...
Remove untracked files
git clean -f
Remove untracked files and directories
git clean -fd
Private Repository
Clone a private repository through https
- username : abernalc
- path : /accout/repoName
git clone https://abernalc@github.com/accout/repoName
Extract a subfolder from a Repository into an independent Repository
1. Clone the original repository
2. Using filter-branch to just have the files of the subfolder
- folder path : FOLDER-PATH
- branch : BRANCH-NAME
git filter-branch --prune-empty --subdirectory-filter FOLDER-PATH BRANCH-NAME
3. Add the upstream repository
- username : ABERNALC
- repository : REPOSITORY
git remote add upstream https://github.com/ABERNALC/REPOSITORY.git
4. Push the code to the new upstream repository
git push -u upstream BRANCH-NAME
5. If you want to return to the current state of the original branch
git checkout <BRANCH> git reset --hard <COMMIT NUMBER>
Create a new repository on the command line
echo "# alejandrobernalcollazos" >> README.md git init git add README.md git commit -m "first commit" git remote add origin https://github.com/abernalc/alejandrobernalcollazos.git git push -u origin master
Push an existing repository from the command line
git remote add origin https://github.com/abernalc/alejandrobernalcollazos.git git push -u origin master