Initialize a local repo
Run the following command in the folder where you would like
to initialize a git repo.
git init
Get status
It is a good practice to frequently run check on status
during development. The following command points out the changes between the
previous commit and current state of the folder.
git status
Add content
To add an untracked file named ‘text.txt’ to the staging
area, execute the following command.
git add text.txt
Syntax: git add <filename>
Commit changes
To commit changes made to the folder, execute the following
command. The message will be used as a commit message to associate this
check-in with the message.
git commit –m “Add text.txt to the code base.”
Syntax: git commit –m “<Commit message>”
Add using wild card
To add multiple files using a wild card character, execute
the following command.
git add ‘*.txt’
Syntax: git add ‘<wildcard_character+string>’
Check history
Review commit history using the following command.
git log
Add Remote repository
To push the local repo to the remote git server, we need to
add a remote repository.
git remote add origin http://<remote_host>/<repo_name>/<repo+name>.git
Pushing remotely
The push command tells Git where to put our commits. The
name of our remote is origin and the
default local branch name is master.
The -u option allows git to remember
the parameters for subsequent pushes.
git push -u origin master
Pulling remotely
Pull the latest changes from the remote repository to your
local repository. All changes since your last push will be pulled down to the
local repo.
git pull origin master
Identifying differences
The following command shows the differences between our last
commit and the current state of the remote repo. HEAD refers to our recent
commit.
git diff HEAD
Staged differences
The diff command can also be used to identify changes within
the files that have already been staged.
git diff --staged
Resetting the staged additions
Sometimes, we may choose to un-stage changes that have not
been pushed to the repo.
git reset <repo_name>/<file_name>
Undo
Files can be changed back to the way they were at the last
commit. When this command is executed, any files that were not committed will
be removed from the local folder.
git checkout -- <filename_to_be_removed>
Creating a branch
git branch <branch_name>
Display branches
View list of all branches using the following command.
git branch
Switching branches
To switch to a branch, execute the following.
git checkout <branch_name>
Removing
Delete one or more files from the branch or master.
git rm ‘*.txt’
git rm text.txt
Committing branch changes
git commit –m “<message_text>”
Switching back to master
To merge or copy your changes made in the branch to your
master, first switch over to the master branch.
git checkout master
Perform merge
Prepare to merge changes from branch to master.
git merge <branch_name_to_copy_from>
Checkout theirs or ours
git checkout --theirs myscript.py
git checkout --ours myscript.py
Overwrite master branch with another branch
[Not a recommended practice]
git checkout fix-tests
git merge -s ours master
git checkout master
git merge fix-tests
Clean up branch
git branch –d <branch_name_to_cleanup>
List all remotes
git remote -v
Revert local changes
git checkout .
git reset
Delete last remote commit
git reset --soft HEAD~1
git stash
git push -u -f origin master
git stash
git push -u -f origin master
Rebase
git rebase -i <commit_hash>
pick/drop hashes as needed
Resolve any merge conflicts and git add *
git rebase --continue
git push -u origin master
pick/drop hashes as needed
Resolve any merge conflicts and git add *
git rebase --continue
git push -u origin master
Remove untracked files
git clean -f
Remove untracked files and directories
git clean -fd
Tagging
git tag <tag_name>
git push -u origin <branch> --tags
git push -u origin <branch> --tags
Git log readable one line
git log --pretty=oneline
git log --pretty=oneline -5 (last five commits)
Git sparse-checkout subdirectory
git clone --filter=blob:none --no-checkout --depth 1 --sparse <project-url>
cd <project>
git sparse-checkout add <folder1> <folder2>
git checkout
Git enable LFS
git lfs install
git lfs track "*.py" (for example)
git add .gitattributes
git commit -m "Adding .gitattributes"
git push
Git enable long filename
git config --system core.longpaths true