Languages
[Edit]
EN

Git - how push local changes into new branch?

13 points
Created by:
Veer-Ahmad
487

Using Git sometimes it is necessary to make new branch for locally changed files. In this article we want to show how to do it.

Quick solutuion:

git checkout -b "my_new_brach_with_local_changes"
git add -A
git commit -m "My first commmit to my new branch"
git push --set-upstream origin "my_new_brach_with_local_changes"

1. Branch creation steps

1.1. (Optionally) Check changes and list all branches

Run command:

git status

Output: 

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.txt

nothing added to commit but untracked files present (use "git add" to track)

Note: readme.txt file has been found with available only master branch (git branch -a returned nothing).

1.2. Create new brach:

Run command:

git checkout -b "my_new_brach_with_local_changes"

Output:

Switched to a new branch 'my_new_brach_with_local_changes'

1.3. (Optionally) Check active branch and changes:

Run command:

git status

Output:

On branch my_new_brach_with_local_changes

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.txt

nothing added to commit but untracked files present (use "git add" to track)

Note: active brunch name is my_new_brach_with_local_changes with listed changed/added files.

1.4. Add, commit and push changes:

Run commands:

git add -A
git commit -m "My first commmit to my new branch"

Output: 

[my_new_brach_with_local_changes (root-commit) 5e87717] My first commmit to my new branch
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt

Run command:

git push --set-upstream origin my_new_brach_with_local_changes

 Output:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 240 bytes | 120.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To C:/Users/qurek/Desktop/git test/TestRepository.git/
 * [new branch]      my_new_brach_with_changes -> my_new_brach_with_changes
Branch 'my_new_brach_with_changes' set up to track remote branch 'my_new_brach_with_changes' from 'origin'.

Notes:

  • git add -A added all created/changed files to list to send,
  • git commit -m "My first commmit to my new branch" sent added files to local repository (my_new_brach_with_changes),
  • git push --set-upstream origin my_new_brach_with_changes created remote repository with same name (my_new_brach_with_changes) and sent local repository changes into remote one. --set-upstream set up remote reposotory where in future all local changes will be send - next time just git push is needed to send changes. origin describes default remote repository saved inside .git/config file.

1.5. (Optionally) Check active branch and changes:

Run command:

git branch -a

Output:

  master
* my_new_brach_with_changes
  remotes/origin/my_new_brach_with_local_changes

 Run command:

git status

Output:

On branch my_new_brach_with_changes
Your branch is up to date with 'origin/my_new_brach_with_changes'.

nothing to commit, working tree clean

 Note: to switch branch to master git checkout master should be used.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Git

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join