Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
135 views

Git Cheat Sheet: Working With Files

The document provides information on various Git configuration commands and aliases that can be used to customize Git commands. It lists commands to list configuration settings, get a single setting value, set aliases for common commands like "git commit" and "git add", add and remove files from the repository, save and reapply stashed changes, and view repository status.

Uploaded by

Lahari Deevi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

Git Cheat Sheet: Working With Files

The document provides information on various Git configuration commands and aliases that can be used to customize Git commands. It lists commands to list configuration settings, get a single setting value, set aliases for common commands like "git commit" and "git add", add and remove files from the repository, save and reapply stashed changes, and view repository status.

Uploaded by

Lahari Deevi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Other useful configuration command

> Working With Files


List all key-value configuration s

$ git config --lis


Get the value of a single ke y
Adding and removing file
Saving staged and unstaged changes to stash for a later use (see
below for the explanation of a stash)

$ git config --get <key>

$ git stas
Add a file or directory to git for trackin g

Git Cheat Sheet Setting aliases for common command s

$ git add <filename_or_dir


Stashing staged, unstaged and untracked files as well

$ git stash -
If you find yourself using a command fre quently, git lets you set an alias for that command to surface it more quickl Add all untracked and tracked files inside the current directory to gi t

$ git add
Stashing everything (including ignored files)

Learn Git online at www.DataCamp.com


“git commit” command

Create an alias named gc for the


$ git stash --al
Remove a file from a working directory or staging area

$ git config --global alias.gc commit


$ git rm <<filename_or_dir>

$ gc -m “New commit Reapply previously stashed changes and empty the stash

$ git stash po
Saving and wor king with change
Create an alias named ga for the “git add” command
Reapply previously stashed changes and keep the stash

$ git config --global alias.ga add See changes in the local repository
$ git stash appl
What is Version Control? $ git statu
Dropping changes in the stas h

Saving a snapshot of the staged changes with a custom message


$ git stash dro
$ git commit -m “[Commit message]
Version control systems are tools that manage changes made to files and directories in a
Show uncommitted changes since the last commit

at is a Branch?
Staging changes in all tracked files and committing with a message
$ git dif
project. They allow you to keep track of what you did when, undo any changes you decide
$ git add -am “[Commit message]
you don't want, and collaborate at scale with others. This cheat sheet focuses on one of the > Wh Show the differences between two commits (should provide the
Editing the message of the latest commit
commit IDs)

most popular one, Git.

$ git commit --amend -m “[New commit message]” $ git diff <id_1> <id_2>

Branches are special “copies” of


the code base which allow you to Continue working on
Main branch
work on different parts of a project
the main channel
A note on stashe s

> Key Definitions and new features in an isolated


 
Git stash allows you to temporarily save edits you've made to your working copy so you can

Stashing is especially useful when you are not yet ready to commit
return to your work later.
environment. Changes made to the
changes you've done, but would like to revisit them at a later time. 
Throughout this cheat sheet, you’ll find git-specific terms and jargon being used. Here’s a run-down of files in a branch won’t affect the

all the terms you may encounter

“main branch” which is the main Merge the


new
project development channel. Branches
Main branch Work on 
 experiment
Basic definition
a new

Local repo or repository: A local directory containing code and files for the projec experiment List all branche s
Rename the current branch to <new_name>

Remote repository: An online version of the local repository hosted on services like GitHub, GitLab, and BitBucke $ git branch
$ git branch -m <new_name
Cloning: The act of making a clone or copy of a repository in a new director
$ git branch --list

$ git branch -a (shows remote branches as well Push a copy of local branch named branch to the remote repo

Commit: A snapshot of the project you can come back t


$ git push <remote_repo> branch
Branch: A copy of the project used for working in an isolated environment without affecting the main projec Create a new local branch named ne w_branch without checking 


Git Basics
Git merge: The process of combining two branches together

out that branc h


Delete a remote branch named branch (-d tag only works locally)

$ branch <new_branch $ push <remote_repo> :branch

>
git git

More advanced definition


$ git push <remote_repo> --delete branc
Switch into an existing branch named <branch>

.gitignore file: A file that lists other files you want git not to track (e.g. large data folders, private info, and any local
$ git checkout <branch Merging a branch into the main branch

files that shouldn’t be seen by the public. What is a repository ?


$ git checkout main

Staging area: a cache that holds changes you want to commit next A repository or a repo is any location that stores code and the necessary files that allow it to run without errors. A repo Create a new local branch and switch into i t
$ git merge <other_branch
Git stash: another type of cache that holds unwanted changes you may want to come back late can be both local and remote. A local repo is typically a directory on your machine while a remote repo is hosted on
$ git checkout -b <new_branch
Merging a branch and creating a commit message

Commit ID or hash: a unique identifier for each commit, used for switching to different save points. servers like GitHu b

Safe delete a local branch (prevents deleting unmerged changes)


$ git merge --no-ff <other_branch
HEAD (always capitalized letters): a reference name for the latest commit, to save you having to type Commit IDs.
$ git branch -d <branch
Creating local repositories
HEAD~n syntax is used to refer to older commits (e.g. HEAD~2 refers to the second-to-last commit). Compare the differences between two branche s

Clone a repository from remote hosts (GitHub, GitLab, DagsHub, etc. )


Force delete a local branch (whether merged or unmerged)
$ git diff <branch_1> <branch_2
$ git clone <remote_repo_url $ git branch -D <branch>

Installing Git
Compare a single <file> between two branches

z y
$ git diff <branch_1> <branch_2> <file>
> Initiali e git tracking inside the current director

$ git ini

Pulling changes
Create a git-tracked repository inside a new director y

On OS X — Using an installe On Linux


$ git init [dir_name <remote> without Merge the fetched changes if accepted

Download the installer for Ma $ sudo apt-get in stall gi t

Download all commits and branches from the

applying them on the local rep o


$ git merge <remote>/<branch
Follow the prompts

Clone only a specific branc h


$ git fetch <remote
On Window $ git clone -branch <branch_name> <repo_url A more aggressive version of fetch which calls fetch and merge

On OS X — Using Homebrew
Download the latest Git For Windows installe Only download the specified <branch> from the <remote>
simultaneousl y

$ brew install git

Follow the prompts


$ git fetch <remote> <branch>

$ git pull <remote>


Cloning into a specified director y

$ git clone <repo_url> <dir_name>


Logging and reviewing wor k
Check if installation successful (On any platform)

$ git --version A note on cloning

List all commits with their author, commit ID, date and message
mple value can be 4th of October,
Log commits after some date (A sa
There are two primary methods of cloning a repository - HTT PS syntax and $ git lo 2020- “2020-10-04” or keywords such as “yesterday”, “last
SSH syntax. While SSH cloning is generally considered a bit more secure month”, etc. )

S Up G
because you have to use an SSH key for authentication, HTTPS cloning is -n tag can be used to limit the number of
List one commit per line ( $ git log --oneline --after=”YYYY-MM-DD
commits displayed (e.g. -5))

much simpler and the recommended cloning option by GitHub.

> etting it $ git log --oneline [-n Log commits before some date (Both

can be used for date ranges )

--after and --before tags


HTT PS
Log all commits with diff information :
$ git log --oneline --before=”last a
ye r”

$ git clone https://github.com/your_username/repo_name.git

$ log --stat

king in a team on a single repo, it is important for others to know who made certain
git

If you are wor

changes to the code. So, Git allows you to set user credentials such as name, email, etc.. SS H

$ git clone git @github.com:user_name/repo_name.git Reversing changes


Set your basic informatio

Configure your email

$ gitconfig user.email [your.email@domain.com Checking out (switching to) older commit s


Discard all changes of the latest commit (no easy recovery )

$ checkout $ set --hard 1

Configure your name

git HEAD~ git re HEAD~


Managing remote repositorie
Checks out the third-to-last commit.

$ git config user.name [your-name]

List remote repo s


$ git checkout <commit_id n
I steadof HEAD~n, you can provide commit hash as well. Changes after
$ git remot that commit will be destroyed

Important tags to determine the scope of configurations


Undo the latest commit but leave the working directory
unchanged
Undo a single given commit, without modifying commits that come
Git lets you use tags to determine the scope of the information you’re using during setu Create a new connection called <remote> to a remote repository on servers like GitHub, GitLab, DagsHub, etc.

$ git reset HEAD~1

after it (a safe reset)

$ git remote add <remote> <url_to_remote $ git revert [commit_id]

Local directory, single project (this is the default tag You can undo as many commits as you want by changing the
$ git config --local user.email “my_email@example.com Remove a connection to a remote repo called <remote>
number after the tilde.
u n revert conflicts

May res lt i

All git projects under the current use r


$ git remote rm <remote
$ git config --global user.email “my_email@example.com
For all users on the current machine
Rename a remote connection

$ git config --system user.email “my_email@example.com”


$ git remote rename <old_name> <new_name>
Learn Data Skills Online at www.DataCamp.com

You might also like